Last active
August 10, 2017 15:10
-
-
Save wetherc/054dee878f3946852a4cf9b9a8de9ae8 to your computer and use it in GitHub Desktop.
Dynamic Tab Panels
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
library(shiny) | |
################################################################################ | |
# | |
# GLOBALS | |
# | |
################################################################################ | |
# On application start, we'll randomly choose whether | |
# the viewer can see our super-secret Old Faithful tab | |
IS_AUTHORIZED_TO_VIEW <- sample(c(TRUE, FALSE), 1, FALSE) | |
################################################################################ | |
# | |
# USER INTERFACE | |
# | |
################################################################################ | |
ui <- fluidPage( | |
titlePanel("The Magic Disappearing Tab!"), | |
sidebarLayout( | |
sidebarPanel( | |
# We'll only display this input if the user is | |
# authorized to view the Old Faithful tab | |
if(IS_AUTHORIZED_TO_VIEW == TRUE) { | |
sliderInput( | |
inputId = "bins", | |
label = "Number of bins:", | |
min = 1, | |
max = 50, | |
value = 30 | |
) | |
} | |
), | |
mainPanel( | |
# Note that the actual tab construction has been | |
# moved entirely to the server | |
uiOutput("tabsetPanel") | |
) | |
) | |
) | |
################################################################################ | |
# | |
# SERVER | |
# | |
################################################################################ | |
server <- function(input, output, session) { | |
output$faithfulPlot <- renderPlot({ | |
x <- faithful[, 2] # Old Faithful Geyser data | |
bins <- seq(min(x), max(x), length.out = input$bins + 1) | |
# draw the histogram with the specified number of bins | |
p <- hist(x, breaks = bins, col = 'darkgray', border = 'white') | |
return(p) | |
}) | |
# We will use this to dynamically create only the tabs which | |
# our user is authorized to view | |
tabConstructor <- reactive({ | |
# We start by initializing an empty list | |
myTabs <- list() | |
# We can now iterate through each tab with individual | |
# access permissions and only append it to our list | |
# if the current user should be allowed to view it. | |
if(IS_AUTHORIZED_TO_VIEW == TRUE) { | |
myTabs[[length(myTabs) + 1]] <- tabPanel( | |
"Old Faithful", | |
plotOutput("faithfulPlot") | |
) | |
} | |
# Any tabs that do not require special privileges to view | |
# can be appended without any conditional logic | |
myTabs[[length(myTabs) + 1]] <- tabPanel( | |
"F.A.Q." | |
) | |
return(myTabs) | |
}) | |
output$tabsetPanel <- renderUI({ | |
# Here we're actually creating the tabsetPanel | |
# object that will be rendered in the UI. The | |
# `do.call()` function call takes two arguments: | |
# the parent function we're calling (here, | |
# `tabsetPanel()`), and any additional arguments | |
# passed to that parent function (here, our list | |
# of tabPanels). This is then rendered in the UI | |
# file with a `uiOutput()` call. | |
do.call(tabsetPanel, tabConstructor()) | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment