Skip to content

Instantly share code, notes, and snippets.

@cecilialee
Last active February 24, 2018 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecilialee/b61fdbe06fe5db527fd820f3ccf7a69f to your computer and use it in GitHub Desktop.
Save cecilialee/b61fdbe06fe5db527fd820f3ccf7a69f to your computer and use it in GitHub Desktop.
Hide and show elements in Shiny. #r #shiny
# hide and show elements using conditional panel
library(shiny)
ui = basicPage(
checkboxInput("show", "Show datatable",
value = FALSE),
conditionalPanel("input.show == true",
dataTableOutput("mtcars"))
)
server = function(input, output) {
output$mtcars <- renderDataTable({
mtcars
})
}
shinyApp(ui, server)
# hide and show tab panels
library(shiny)
ui = fluidPage(
checkboxInput("show", "Show data tab", value = FALSE),
hr(),
tabsetPanel(id = "my_tabset",
tabPanel(
"Plot",
plotOutput("my_plot")
),
tabPanel(
"Data",
dataTableOutput("my_data")
)
)
)
server = function(input, output) {
output$my_plot <- renderPlot({
mtcars %>% ggplot() + geom_point(aes(x = mpg, y = drat, color = cyl))
})
output$my_data <- renderDataTable({
mtcars
})
observeEvent(input$show, {
if (input$show) {
showTab("my_tabset", "Data", select = TRUE)
} else {
hideTab("my_tabset", "Data")
}
})
}
shinyApp(ui, server)
# hide and show elements using if expression directly inside render*() function
library(shiny)
ui = basicPage(
checkboxInput("show", "Show datatable",
value = FALSE),
dataTableOutput("mtcars")
)
server = function(input, output) {
output$mtcars <- renderDataTable({
if (input$show) {
mtcars
}
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment