mcSamuelDataSci / CACommunityBurden

11 stars 11 forks source link

two conditionalPanel input lists with same inputID #33

Closed mcSamuelDataSci closed 6 years ago

mcSamuelDataSci commented 6 years ago

Zev-

How can I do something in the .ui file with conditionalPanel such that the input list varies depending on the condition BUT where the inputID stays the same? For example with issue #32, how could I have "selectInput" "choices" of c("A","B","California","D") for one condition and c("A","B","D") for another condition, BUT where the inputID is myLHJ for both conditions?

nteetor commented 6 years ago

If these changes are made in response to a detectable change in the application, you could try out updateSelectInput (or updateSelectizeInput).

You could begin by adding an observer to the shiny server function,

function(input, output, session) {

  # ... other code

  observeEvent(<detectable change/reactive>, {
    updateSelectInput(
      session = session,
      inputId = "<select input id>",
      choices = <expression returning the necessary set of choices>
    )
  })
}

This let's the input id remain the same, but the choices are dynamically updated depending on the state of the application.

zross commented 6 years ago

Yes, @mcSamuelDataSci this is important and I reference it in #32. You should update the element as needed using this strategy.

mcSamuelDataSci commented 6 years ago

Awesome, thank you both! Since the "original/main" selectInput is in the ui.R I assumed anything like this would need to be there, rather than in the server.R.
1) Is there any way you can help me understand this principle of it being in the server.R, or any simple reference on this you can suggest? 2) Related, any insight or simple reference you can share on the "session" perimeter in the beginning of the shinyServer function? This seems key, but I have essentially no understanding of its role.

nteetor commented 6 years ago
  1. Here is a small example of using updateSelectInput() in response to a reactive,
    
    library(shiny)

ui <- fluidPage( fluidRow( column( width = 6, tags$label( "Add dropdown options" ), div( actionButton( inputId = "addOption", label = "Add" ) ), helpText( "Add another option to the select input dropdown menu", "by clicking this button" ) ), column( width = 6, selectInput( inputId = "numberChoice", label = "Choose a number", choices = c() ), helpText( "This is empty to begin with, but clicking the button", "will add choices" ) ) ) )

server <- function(input, output, session) { observeEvent(input$addOption, { updateSelectInput( session = session, inputId = "numberChoice", choices = seq_len(input$addOption) ) }) }

shinyApp(ui, server)



2. The `session` object is on the same level as `input` and `output`, but handles meta information about your application. The object is particularly useful for shiny modules and is also required for update functions. You can read more about `session` here, https://shiny.rstudio.com/reference/shiny/latest/session.html. 
mcSamuelDataSci commented 6 years ago

thanks @nteetor. This was super helpful