# 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)
# 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)