Wednesday 29 June 2016

r - 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


Answer



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)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...