Skip to content

Instantly share code, notes, and snippets.

@stla
Created September 12, 2013 15:56
Show Gist options
  • Select an option

  • Save stla/6539861 to your computer and use it in GitHub Desktop.

Select an option

Save stla/6539861 to your computer and use it in GitHub Desktop.
Series of tabs with Shiny - test4
mytabsetPanel <- function (tabs, id = NULL, selected = NULL)
{
#tabs <- list(...)
tabNavList <- tags$ul(class = "nav nav-tabs", id = id)
tabContent <- tags$div(class = "tab-content")
firstTab <- TRUE
tabsetId <- as.integer(stats::runif(1, 1, 10000))
tabId <- 1
for (divTag in tabs) {
thisId <- paste("tab", tabsetId, tabId, sep = "-")
divTag$attribs$id <- thisId
tabId <- tabId + 1
tabValue <- divTag$attribs$`data-value`
if (!is.null(tabValue) && is.null(id)) {
stop("tabsetPanel doesn't have an id assigned, but one of its tabPanels ",
"has a value. The value won't be sent without an id.")
}
liTag <- tags$li(tags$a(href = paste("#", thisId, sep = ""),
`data-toggle` = "tab", `data-value` = tabValue, divTag$attribs$title))
if (is.null(tabValue)) {
tabValue <- divTag$attribs$title
}
if ((firstTab && is.null(selected)) || (!is.null(selected) &&
identical(selected, tabValue))) {
liTag$attribs$class <- "active"
divTag$attribs$class <- "tab-pane active"
firstTab = FALSE
}
divTag$attribs$title <- NULL
tabNavList <- tagAppendChild(tabNavList, liTag)
tabContent <- tagAppendChild(tabContent, divTag)
}
tabDiv <- tags$div(class = "tabbable", tabNavList, tabContent)
}
####
#### Server
####
shinyServer(function(input, output, session) {
Nodes <- reactive({ paste0("node", LETTERS[1:input$ntabs]) })
Texts <- reactive({
tt <- LETTERS[1:input$ntabs]
names(tt) <- Nodes()
tt
})
Selecteds <- reactive({
selecteds <- rep(NA, input$ntabs)
for(i in 1:input$ntabs){
selecteds[i] <- input[[paste0("sel",i)]]
}
selecteds
})
observe({
texts <- Texts()
nodes <- Nodes()
for(i in 1:input$ntabs){
output[[nodes[i]]] <- renderText({texts[input$tab0]})
}
output$selections <- renderTable({
data.frame(tab=tabnames[1:input$ntabs], selected=Selecteds())
})
})
tabnames <- paste0("tab", LETTERS)
output$twotabs <- renderUI({
tabs <- list(NULL)
nodes <- Nodes()
for(i in 1:input$ntabs){
tabs[[i]] <- tabPanel(tabnames[i],
h3(tabnames[i]),
selectInput(paste0("sel",i), "Select", choices=as.character(1:3), selected="1"),
textOutput(nodes[i]),
value=nodes[i])
}
tabs[[as.numeric(input$ntabs)+1]] <- tabPanel("Summary",
h3("Your selections:"),
tableOutput("selections"))
mytabsetPanel(
tabs
, id="tab0")
})
#
})
shinyUI(pageWithSidebar(
headerPanel("xxx"),
##
## sidebar panel
##
sidebarPanel(
selectInput("ntabs", "Select number of tabs", choices=as.character(1:5), selected="2")
),
##
## main panel
##
mainPanel(
uiOutput("twotabs")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment