Reputation: 504
I would like to populate many tables, but only show the ones the user requests. For example, in the minimal example below, I split mtcars into three tables and I have three checkboxes. I would like to display each table only if the appropriate check box is checked.
I tried using condiditionalPanel, but that is not working. The app does not respond to my inputs. Maybe conditionalPanel only works for GUI inputs.
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
checkboxInput("cyl4", "Show 4 cylinders", value=FALSE),
textOutput("box4"),
checkboxInput("cyl6", "Show 6 cylinders", value=FALSE),
textOutput("box6"),
checkboxInput("cyl8", "Show 8 cylinders", value=FALSE),
textOutput("box8"),
conditionalPanel(
condition = "input.cyl4 == TRUE",
tableOutput("data4")
),
conditionalPanel(
condition = "input.cyl6 == TRUE",
tableOutput("data6")
),
conditionalPanel(
condition = "input.cyl8 == TRUE",
tableOutput("data8")
)
)
server <- function(input, output) {
output$box4 <- renderText(input$cyl4)
output$box6 <- renderText(input$cyl6)
output$box8 <- renderText(input$cyl8)
output$data4 <- renderTable({mtcars[mtcars$cyl == 4, ]}, rownames = TRUE)
output$data6 <- renderTable({mtcars[mtcars$cyl == 6, ]}, rownames = TRUE)
output$data8 <- renderTable({mtcars[mtcars$cyl == 8, ]}, rownames = TRUE)
}
shinyApp(ui, server)
}
Currently the shiny app shows all three tables and doesn't respond to the check boxes. If only the cyl4 box is checked, I would like the app to show only table 'data4'.
Upvotes: 0
Views: 410
Reputation: 18551
The conditional argument has to be written in JavaScript where TRUE
is spelled true
. The below code should work.
library("shiny")
shinyApp(
ui = fluidPage(
checkboxInput("cyl4", "Show 4 cylinders", value=FALSE),
textOutput("box4"),
checkboxInput("cyl6", "Show 6 cylinders", value=FALSE),
textOutput("box6"),
checkboxInput("cyl8", "Show 8 cylinders", value=FALSE),
textOutput("box8"),
conditionalPanel(
condition = "input.cyl4 == true",
tableOutput("data4")
),
conditionalPanel(
condition = "input.cyl6 == true",
tableOutput("data6")
),
conditionalPanel(
condition = "input.cyl8 == true",
tableOutput("data8")
)
),
server =function(input, output) {
output$box4 <- renderText(input$cyl4)
output$box6 <- renderText(input$cyl6)
output$box8 <- renderText(input$cyl8)
output$data4 <- renderTable({mtcars[mtcars$cyl == 4, ]}, rownames = TRUE)
output$data6 <- renderTable({mtcars[mtcars$cyl == 6, ]}, rownames = TRUE)
output$data8 <- renderTable({mtcars[mtcars$cyl == 8, ]}, rownames = TRUE)
}
)
Upvotes: 1