简体   繁体   中英

shiny conditional panel not updating

I have an issue with conditional panel. I would like to show in sidebarPanel either DateRange OR SliderInput depending on the selected choice in the RadioButton, which is also in the sidebarPanel.

If you try to run the below example it will fail with following Error message:

Error : formal argument "condition" matched by multiple actual arguments

If you comment out any of the two conditions you can see that the example variable has value a or b depending on the options chosen.

I'm pretty sure I'm missing something, but I cannot figure out what. I did look around and I couldn't find anything helpful.

library(shiny)

# Server ---------------------------------------------

server = shinyServer(function(input, output){
  output$debug <- renderPrint({
    input$example
  })
})


# Ui -------------------------------------------------

ui = {
  fluidPage(
    sidebarPanel(
      radioButtons('example', 'Select Examples: ', choices = c('a', 'b'), selected = 'a'),
      conditionalPanel(
        condition = "input.example == 'a'",
        dateRangeInput('date', 'Select Date Range:',
                       start = Sys.Date() - 7,
                       end = Sys.Date(),
                       min = '2012-04-01',
                       max = Sys.Date()
                       )
        ,
        condition = "input.example == 'b'",
        sliderInput('date', 'Slide Date Range:', min = 1, max = 90, value = 14, step = 1)
       )
    ),
    mainPanel(verbatimTextOutput("debug")
    )
  )}


# App ------------------------------------------------

shinyApp(ui = ui, server = server)

Thanks

You need to specify two conditionalPanel objects, one for each condition.

library(shiny)

# Server ---------------------------------------------

server = shinyServer(function(input, output){
  output$debug <- renderPrint({
    input$example
  })
})


# Ui -------------------------------------------------

ui = {
  fluidPage(
    sidebarPanel(
      radioButtons('example', 'Select Examples: ', choices = c('a', 'b'), 
           selected = 'a'),
    conditionalPanel(
      condition = "input.example == 'a'",
      dateRangeInput('date', 'Select Date Range:',
                   start = Sys.Date() - 7,
                   end = Sys.Date(),
                   min = '2012-04-01',
                   max = Sys.Date()
    )
  ),
  conditionalPanel(
    condition = "input.example = 'b'",
    sliderInput('date', 'Slide Date Range:', min = 1, max = 90, value = 14, 
          step = 1)
  )
),
mainPanel(verbatimTextOutput("debug")
)
)}


# App ------------------------------------------------

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM