| 1 |
#' Dynamically insert/remove a tabPanel |
| 2 |
#' |
| 3 |
#' Dynamically insert or remove a [tabPanel()] (or a |
| 4 |
#' [navbarMenu()]) from an existing [tabsetPanel()], |
| 5 |
#' [navlistPanel()] or [navbarPage()]. |
| 6 |
#' |
| 7 |
#' When you want to insert a new tab before or after an existing tab, you |
| 8 |
#' should use `insertTab`. When you want to prepend a tab (i.e. add a |
| 9 |
#' tab to the beginning of the `tabsetPanel`), use `prependTab`. |
| 10 |
#' When you want to append a tab (i.e. add a tab to the end of the |
| 11 |
#' `tabsetPanel`), use `appendTab`. |
| 12 |
#' |
| 13 |
#' For `navbarPage`, you can insert/remove conventional |
| 14 |
#' `tabPanel`s (whether at the top level or nested inside a |
| 15 |
#' `navbarMenu`), as well as an entire [navbarMenu()]. |
| 16 |
#' For the latter case, `target` should be the `menuName` that |
| 17 |
#' you gave your `navbarMenu` when you first created it (by default, |
| 18 |
#' this is equal to the value of the `title` argument). |
| 19 |
#' |
| 20 |
#' @param inputId The `id` of the `tabsetPanel` (or |
| 21 |
#' `navlistPanel` or `navbarPage`) into which `tab` will |
| 22 |
#' be inserted/removed. |
| 23 |
#' |
| 24 |
#' @param tab The item to be added (must be created with `tabPanel`, |
| 25 |
#' or with `navbarMenu`). |
| 26 |
#' |
| 27 |
#' @param target If inserting: the `value` of an existing |
| 28 |
#' `tabPanel`, next to which `tab` will be added. |
| 29 |
#' If removing: the `value` of the `tabPanel` that |
| 30 |
#' you want to remove. See Details if you want to insert next to/remove |
| 31 |
#' an entire `navbarMenu` instead. |
| 32 |
#' |
| 33 |
#' @param position Should `tab` be added before or after the |
| 34 |
#' `target` tab? |
| 35 |
#' |
| 36 |
#' @param select Should `tab` be selected upon being inserted? |
| 37 |
#' |
| 38 |
#' @param session The shiny session within which to call this function. |
| 39 |
#' |
| 40 |
#' @seealso [showTab()] |
| 41 |
#' |
| 42 |
#' @examples |
| 43 |
#' ## Only run this example in interactive R sessions |
| 44 |
#' if (interactive()) { |
| 45 |
#' |
| 46 |
#' # example app for inserting/removing a tab |
| 47 |
#' ui <- fluidPage( |
| 48 |
#' sidebarLayout( |
| 49 |
#' sidebarPanel( |
| 50 |
#' actionButton("add", "Add 'Dynamic' tab"), |
| 51 |
#' actionButton("remove", "Remove 'Foo' tab") |
| 52 |
#' ), |
| 53 |
#' mainPanel( |
| 54 |
#' tabsetPanel(id = "tabs", |
| 55 |
#' tabPanel("Hello", "This is the hello tab"), |
| 56 |
#' tabPanel("Foo", "This is the foo tab"), |
| 57 |
#' tabPanel("Bar", "This is the bar tab") |
| 58 |
#' ) |
| 59 |
#' ) |
| 60 |
#' ) |
| 61 |
#' ) |
| 62 |
#' server <- function(input, output, session) { |
| 63 |
#' observeEvent(input$add, { |
| 64 |
#' insertTab(inputId = "tabs", |
| 65 |
#' tabPanel("Dynamic", "This a dynamically-added tab"), |
| 66 |
#' target = "Bar" |
| 67 |
#' ) |
| 68 |
#' }) |
| 69 |
#' observeEvent(input$remove, { |
| 70 |
#' removeTab(inputId = "tabs", target = "Foo") |
| 71 |
#' }) |
| 72 |
#' } |
| 73 |
#' |
| 74 |
#' shinyApp(ui, server) |
| 75 |
#' |
| 76 |
#' |
| 77 |
#' # example app for prepending/appending a navbarMenu |
| 78 |
#' ui <- navbarPage("Navbar page", id = "tabs", |
| 79 |
#' tabPanel("Home", |
| 80 |
#' actionButton("prepend", "Prepend a navbarMenu"), |
| 81 |
#' actionButton("append", "Append a navbarMenu") |
| 82 |
#' ) |
| 83 |
#' ) |
| 84 |
#' server <- function(input, output, session) { |
| 85 |
#' observeEvent(input$prepend, { |
| 86 |
#' id <- paste0("Dropdown", input$prepend, "p") |
| 87 |
#' prependTab(inputId = "tabs", |
| 88 |
#' navbarMenu(id, |
| 89 |
#' tabPanel("Drop1", paste("Drop1 page from", id)), |
| 90 |
#' tabPanel("Drop2", paste("Drop2 page from", id)), |
| 91 |
#' "------", |
| 92 |
#' "Header", |
| 93 |
#' tabPanel("Drop3", paste("Drop3 page from", id)) |
| 94 |
#' ) |
| 95 |
#' ) |
| 96 |
#' }) |
| 97 |
#' observeEvent(input$append, { |
| 98 |
#' id <- paste0("Dropdown", input$append, "a") |
| 99 |
#' appendTab(inputId = "tabs", |
| 100 |
#' navbarMenu(id, |
| 101 |
#' tabPanel("Drop1", paste("Drop1 page from", id)), |
| 102 |
#' tabPanel("Drop2", paste("Drop2 page from", id)), |
| 103 |
#' "------", |
| 104 |
#' "Header", |
| 105 |
#' tabPanel("Drop3", paste("Drop3 page from", id)) |
| 106 |
#' ) |
| 107 |
#' ) |
| 108 |
#' }) |
| 109 |
#' } |
| 110 |
#' |
| 111 |
#' shinyApp(ui, server) |
| 112 |
#' |
| 113 |
#' } |
| 114 |
#' @export |
| 115 |
insertTab <- function(inputId, tab, target = NULL, |
| 116 |
position = c("after", "before"), select = FALSE, |
| 117 |
session = getDefaultReactiveDomain()) { |
| 118 |
bslib::nav_insert( |
| 119 |
inputId, tab, target, |
| 120 |
match.arg(position), select, session |
| 121 |
) |
| 122 |
} |
| 123 |
|
| 124 |
#' @param menuName This argument should only be used when you want to |
| 125 |
#' prepend (or append) `tab` to the beginning (or end) of an |
| 126 |
#' existing [navbarMenu()] (which must itself be part of |
| 127 |
#' an existing [navbarPage()]). In this case, this argument |
| 128 |
#' should be the `menuName` that you gave your `navbarMenu` |
| 129 |
#' when you first created it (by default, this is equal to the value |
| 130 |
#' of the `title` argument). Note that you still need to set the |
| 131 |
#' `inputId` argument to whatever the `id` of the parent |
| 132 |
#' `navbarPage` is. If `menuName` is left as `NULL`, |
| 133 |
#' `tab` will be prepended (or appended) to whatever |
| 134 |
#' `inputId` is. |
| 135 |
#' |
| 136 |
#' @rdname insertTab |
| 137 |
#' @export |
| 138 |
prependTab <- function(inputId, tab, select = FALSE, menuName = NULL, |
| 139 |
session = getDefaultReactiveDomain()) { |
| 140 |
bslib::nav_prepend(inputId, tab, menu_title = menuName, select = select, session = session) |
| 141 |
} |
| 142 |
|
| 143 |
#' @rdname insertTab |
| 144 |
#' @export |
| 145 |
appendTab <- function(inputId, tab, select = FALSE, menuName = NULL, |
| 146 |
session = getDefaultReactiveDomain()) { |
| 147 |
bslib::nav_append(inputId, tab, menu_title = menuName, select = select, session = session) |
| 148 |
} |
| 149 |
|
| 150 |
#' @rdname insertTab |
| 151 |
#' @export |
| 152 |
removeTab <- function(inputId, target, |
| 153 |
session = getDefaultReactiveDomain()) { |
| 154 |
bslib::nav_remove(inputId, target, session) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#' Dynamically hide/show a tabPanel |
| 159 |
#' |
| 160 |
#' Dynamically hide or show a [tabPanel()] (or a |
| 161 |
#' [navbarMenu()])from an existing [tabsetPanel()], |
| 162 |
#' [navlistPanel()] or [navbarPage()]. |
| 163 |
#' |
| 164 |
#' For `navbarPage`, you can hide/show conventional |
| 165 |
#' `tabPanel`s (whether at the top level or nested inside a |
| 166 |
#' `navbarMenu`), as well as an entire [navbarMenu()]. |
| 167 |
#' For the latter case, `target` should be the `menuName` that |
| 168 |
#' you gave your `navbarMenu` when you first created it (by default, |
| 169 |
#' this is equal to the value of the `title` argument). |
| 170 |
#' |
| 171 |
#' @param inputId The `id` of the `tabsetPanel` (or |
| 172 |
#' `navlistPanel` or `navbarPage`) in which to find |
| 173 |
#' `target`. |
| 174 |
#' |
| 175 |
#' @param target The `value` of the `tabPanel` to be |
| 176 |
#' hidden/shown. See Details if you want to hide/show an entire |
| 177 |
#' `navbarMenu` instead. |
| 178 |
#' |
| 179 |
#' @param select Should `target` be selected upon being shown? |
| 180 |
#' |
| 181 |
#' @param session The shiny session within which to call this function. |
| 182 |
#' |
| 183 |
#' @seealso [insertTab()] |
| 184 |
#' |
| 185 |
#' @examples |
| 186 |
#' ## Only run this example in interactive R sessions |
| 187 |
#' if (interactive()) { |
| 188 |
#' |
| 189 |
#' ui <- navbarPage("Navbar page", id = "tabs", |
| 190 |
#' tabPanel("Home", |
| 191 |
#' actionButton("hideTab", "Hide 'Foo' tab"), |
| 192 |
#' actionButton("showTab", "Show 'Foo' tab"), |
| 193 |
#' actionButton("hideMenu", "Hide 'More' navbarMenu"), |
| 194 |
#' actionButton("showMenu", "Show 'More' navbarMenu") |
| 195 |
#' ), |
| 196 |
#' tabPanel("Foo", "This is the foo tab"), |
| 197 |
#' tabPanel("Bar", "This is the bar tab"), |
| 198 |
#' navbarMenu("More", |
| 199 |
#' tabPanel("Table", "Table page"), |
| 200 |
#' tabPanel("About", "About page"), |
| 201 |
#' "------", |
| 202 |
#' "Even more!", |
| 203 |
#' tabPanel("Email", "Email page") |
| 204 |
#' ) |
| 205 |
#' ) |
| 206 |
#' |
| 207 |
#' server <- function(input, output, session) { |
| 208 |
#' observeEvent(input$hideTab, { |
| 209 |
#' hideTab(inputId = "tabs", target = "Foo") |
| 210 |
#' }) |
| 211 |
#' |
| 212 |
#' observeEvent(input$showTab, { |
| 213 |
#' showTab(inputId = "tabs", target = "Foo") |
| 214 |
#' }) |
| 215 |
#' |
| 216 |
#' observeEvent(input$hideMenu, { |
| 217 |
#' hideTab(inputId = "tabs", target = "More") |
| 218 |
#' }) |
| 219 |
#' |
| 220 |
#' observeEvent(input$showMenu, { |
| 221 |
#' showTab(inputId = "tabs", target = "More") |
| 222 |
#' }) |
| 223 |
#' } |
| 224 |
#' |
| 225 |
#' shinyApp(ui, server) |
| 226 |
#' } |
| 227 |
#' |
| 228 |
#' @export |
| 229 |
showTab <- function(inputId, target, select = FALSE, |
| 230 |
session = getDefaultReactiveDomain()) { |
| 231 |
force(target) |
| 232 |
|
| 233 |
if (select) updateTabsetPanel(session, inputId, selected = target) |
| 234 |
inputId <- session$ns(inputId) |
| 235 |
|
| 236 |
callback <- function() { |
| 237 |
session$sendChangeTabVisibility( |
| 238 |
inputId = inputId, |
| 239 |
target = target, |
| 240 |
type = "show" |
| 241 |
) |
| 242 |
} |
| 243 |
session$onFlush(callback, once = TRUE) |
| 244 |
} |
| 245 |
|
| 246 |
#' @rdname showTab |
| 247 |
#' @export |
| 248 |
hideTab <- function(inputId, target, |
| 249 |
session = getDefaultReactiveDomain()) { |
| 250 |
force(target) |
| 251 |
inputId <- session$ns(inputId) |
| 252 |
|
| 253 |
callback <- function() { |
| 254 |
session$sendChangeTabVisibility( |
| 255 |
inputId = inputId, |
| 256 |
target = target, |
| 257 |
type = "hide" |
| 258 |
) |
| 259 |
} |
| 260 |
session$onFlush(callback, once = TRUE) |
| 261 |
} |