Home > Back-end >  Instantly updating conditionalPanels when condition has changed
Instantly updating conditionalPanels when condition has changed

Time:09-26

I have two conditionalPanels that I want to change based on the user's selection in a selectInput. However, the conditionalPanels only update when the user clicks the submitButton. This leads to a terrible UI functionality where you first have to submit values before you can see the correct labels for what you are submitting. Below is my code:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      
      selectInput("type", "Type", c(1, 2)),
      conditionalPanel(
        "input.type == 1",
        "Test 1"
      ),
      conditionalPanel(
        "input.type == 2",
        "Test 2"
      ),
      
      submitButton("Add", icon = NULL, width = NULL)
    ),
    
    mainPanel(),
  )
)

server <- function(input, output, session){}

shinyApp(ui, server)

I tried the same thing with observeEvent and renderUI, but it still only updates when you click submit. Is there any way to make the change instant?

CodePudding user response:

Use actionButton instead.

actionButton("Add", label="Add", icon=NULL, width=NULL)

Why? Because "Apps that include a submit button do not automatically update their outputs when inputs change, rather they wait until the user explicitly clicks the submit button. The use of submitButton is generally discouraged in favor of the more versatile actionButton()" - see https://shiny.rstudio.com/reference/shiny/latest/submitButton.html

  • Related