R Shiny: how to prevent duplicate plot update with nested selectors? -


i have dataset in every row contains data single person , every column contains different information him/her. let's looks (but more columns , rows in real data):

data <- data.frame(   height=runif(1000, 150, 200),   sex=sample(c("f","m"), 1000, replace=t),   heir=sample(c("blond", "black", "red", "brown"), 1000, replace=t),   eye=sample(c("blue", "green", "brown"to show histograms ), 1000, replace=t) ) 

i want make shiny app in select category (column) , value , draw histogram of height among people share value. example category = 'sex' , value = 'f' have histogram of height among woman. because each category have different set of values, after changing category have (automatically) update value selector too. the problem means 2 updates , each of them generates new histogram, while second 1 makes sense. here code:

server.r

shinyserver(function(input, output, session) {    data <- data.frame(     height=runif(1000, 150, 200),     sex=sample(c("f","m"), 1000, replace=t),     heir=sample(c("blond", "black", "red", "brown"), 1000, replace=t),     eye=sample(c("blue", "green", "brown"), 1000, replace=t)     )    # update value after category change   observe({     updateselectinput(session, "value", "value",                       choices=as.list(levels(data[,input$category])))     cat(paste("category update:",input$category, "\n"), file = stderr())   })    output$histogram <- renderplot({     cat(paste("hist:", "category =", input$category,               "value =",input$value, "\n"), file = stderr())     hist(data[data[, input$category] == input$value, "height"])   }) }) 

ui.r

shinyui(fluidpage(   sidebarpanel(         selectinput("category", "category", choices=c("sex", "heir", "eye"), selected="sex"),     selectinput("value", "value", c("f", "m"), selected="f")   ),    mainpanel(     plotoutput("histogram")   ) )) 

the cat expressions makes easier see going on. when run app default category ('sex') , value ('f') selected, console output is:

category update: sex  hist: category = sex value = f  

then, when choose different category using selector, example heir, got information:

category update: heir  hist: category = heir value = f  error in hist.default(data[data[, input$category] == input$value, "height"]):    invalid number of 'breaks' hist: category = heir value = black  

so @ first, app changes category , tries plot histogram new category = heir , old value = f, of course have fail. changes value , draws correct histogram. is there way automatically update value selector before plot drawn?

you can use isolateto avoid dependency of histogram on input$category:

  output$histogram <- renderplot({     cat(paste("hist:", "category =", isolate(input$category),               "value =",input$value, "\n"), file = stderr())     hist(data[data[, isolate(input$category)] == input$value, "height"])   }) 

your histogram no longer updated when category changes. changing category changes value in app, , histogram update when value changed.

more info on isolate here.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -