Last active
February 24, 2018 08:19
-
-
Save cecilialee/b61fdbe06fe5db527fd820f3ccf7a69f to your computer and use it in GitHub Desktop.
Hide and show elements in Shiny. #r #shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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