-
Notifications
You must be signed in to change notification settings - Fork 298
/
tabs.R
83 lines (78 loc) · 2.22 KB
/
tabs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#' A container for tab items
#'
#' @param ... Items to put in the container. Each item should be a
#' \code{\link{tabItem}}.
#'
#' @seealso \code{\link{menuItem}}, \code{\link{menuSubItem}},
#' \code{\link{tabItem}}. See \code{\link{sidebarMenu}} for a usage example.
#' @export
tabItems <- function(...) {
lapply(list(...), tagAssert, class = "tab-pane")
div(class = "tab-content", ...)
}
#' One tab to put inside a tab items container
#'
#' @param tabName The name of a tab. This must correspond to the \code{tabName}
#' of a \code{\link{menuItem}} or \code{\link{menuSubItem}}.
#' @param ... Contents of the tab.
#'
#' @seealso \code{\link{menuItem}}, \code{\link{menuSubItem}},
#' \code{\link{tabItems}}. See \code{\link{sidebarMenu}} for a usage example.
#' @export
tabItem <- function(tabName = NULL, ...) {
if (is.null(tabName))
stop("Need tabName")
validateTabName(tabName)
div(
role = "tabpanel",
class = "tab-pane",
id = paste0("shiny-tab-", tabName),
...
)
}
#' Change the selected tab on the client
#'
#' This function controls the active tab of \code{\link{tabItems}} from the
#' server. It behaves just like \code{\link[shiny]{updateTabsetPanel}}.
#'
#' @inheritParams shiny::updateTabsetPanel
#' @examples
#' ## Only run this example in interactive R sessions
#' if (interactive()) {
#'
#' ui <- dashboardPage(
#' dashboardHeader(title = "Simple tabs"),
#' dashboardSidebar(
#' sidebarMenu(
#' id = "tabs",
#' menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
#' menuItem("Widgets", tabName = "widgets", icon = icon("th"))
#' ),
#' actionButton('switchtab', 'Switch tab')
#' ),
#' dashboardBody(
#' tabItems(
#' tabItem(tabName = "dashboard",
#' h2("Dashboard tab content")
#' ),
#' tabItem(tabName = "widgets",
#' h2("Widgets tab content")
#' )
#' )
#' )
#' )
#'
#' server <- function(input, output, session) {
#' observeEvent(input$switchtab, {
#' newtab <- switch(input$tabs,
#' "dashboard" = "widgets",
#' "widgets" = "dashboard"
#' )
#' updateTabItems(session, "tabs", newtab)
#' })
#' }
#'
#' shinyApp(ui, server)
#' }
#' @export
updateTabItems <- shiny::updateTabsetPanel