0

I am developing a simple web app using the Interactive Document approach with R_Markdown and Shiny. I have a series of input UI objects that get the user options, each of these modifies the next in a chain, and the final one triggers the output (a chart). So I have

  1. A map, which when clicked defines the
  2. Seasons of data available, when one is selected it gives the
  3. Soil types in the data, when one or more are selected it gives the
  4. Elevation classes in the data, when one is selected we then calculate and draw the
  5. Output chart of the selected data.

So for example there are drop down lists to choose the season and soil(s):

  output$season_selector <- renderUI({
    cat(file=stderr(), paste("render season selector"), "\n")
    selectInput("season", h4("Season?"), my$season_here, selected=my$season_sel)
  })

  output$soil_selector <- renderUI({
    cat(file=stderr(), paste("render soil selector"), "\n")
    selectInput("soil", h4("Soils?"), my$soil_here, selected=my$soil_sel, 
                selectize=FALSE, multiple=TRUE)
  })

I observe input$season, and this triggers an update of the "soil" UI element.

observeEvent(input$season, {

...

# reset soil list and default soil selected
my$soil_sel <- my$soil_here

updateSelectInput(session, "soil", choices=my$soil_here, selected=my$soil_sel)
})

However, this does invalidate input$soil (although the UI element options and selection have changed, the user has not made a selection), and so the next step in the cascade doesn't trigger:

observeEvent(input$soil, {

...

# reset elev list and default elev selected
my$soil_sel <- my$soil_here

updateSelectInput(session, "soil", choices=my$soil_here, selected=my$soil_sel)
})

How do I achieve a cascade of reactive UI elements like this? Each reaction needs to trigger either when the UI options and selection changes or when the user makes a different selection. Thanks.

1 Answer 1

0

Ok, I think I've fixed it. I needed to use the following pattern, which resets variables further down the chain, and doesn't respond to user inputs unless the value has changed. This avoids duplicate or missing events.

  # reset selections
  my$soil_default <- my$soil_here
  my$soil <- my$soil_default 
  my$elev <- list(NA)

}) # end reaction to season changing

observeEvent(input$soil, {
  req(input$soil, input$soil!="NA")
  if (any(my$soil != input$soil)) {
    my$soil <- input$soil
    my$elev <- list(NA)
  }
})

#### react to change of soil ####
observeEvent(my$soil, {

  cat(file=stderr(), "\n")
  cat(file=stderr(), paste("change of my$soil = "))
  cat(file=stderr(), paste(my$soil), "\n")
  req(my$soil, my$soil!="NA")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.