| 1 |
#' @include utils.R |
| 2 |
NULL |
| 3 |
|
| 4 |
#' Create a Bootstrap page |
| 5 |
#' |
| 6 |
#' Create a Shiny UI page that loads the CSS and JavaScript for |
| 7 |
#' [Bootstrap](https://getbootstrap.com/), and has no content in the page |
| 8 |
#' body (other than what you provide). |
| 9 |
#' |
| 10 |
#' This function is primarily intended for users who are proficient in HTML/CSS, |
| 11 |
#' and know how to lay out pages in Bootstrap. Most applications should use |
| 12 |
#' [fluidPage()] along with layout functions like |
| 13 |
#' [fluidRow()] and [sidebarLayout()]. |
| 14 |
#' |
| 15 |
#' @param ... The contents of the document body. |
| 16 |
#' @param title The browser window title (defaults to the host URL of the page) |
| 17 |
#' @param theme One of the following: |
| 18 |
#' * `NULL` (the default), which implies a "stock" build of Bootstrap 3. |
| 19 |
#' * A [bslib::bs_theme()] object. This can be used to replace a stock |
| 20 |
#' build of Bootstrap 3 with a customized version of Bootstrap 3 or higher. |
| 21 |
#' * A character string pointing to an alternative Bootstrap stylesheet |
| 22 |
#' (normally a css file within the www directory, e.g. `www/bootstrap.css`). |
| 23 |
#' @param lang ISO 639-1 language code for the HTML page, such as "en" or "ko". |
| 24 |
#' This will be used as the lang in the \code{<html>} tag, as in \code{<html lang="en">}. |
| 25 |
#' The default (NULL) results in an empty string. |
| 26 |
#' |
| 27 |
#' @return A UI definition that can be passed to the [shinyUI] function. |
| 28 |
#' |
| 29 |
#' @note The `basicPage` function is deprecated, you should use the |
| 30 |
#' [fluidPage()] function instead. |
| 31 |
#' |
| 32 |
#' @seealso [fluidPage()], [fixedPage()] |
| 33 |
#' @export |
| 34 |
bootstrapPage <- function(..., title = NULL, theme = NULL, lang = NULL) { |
| 35 |
|
| 36 |
args <- list( |
| 37 |
jqueryDependency(), |
| 38 |
if (!is.null(title)) tags$head(tags$title(title)), |
| 39 |
if (is.character(theme)) { |
| 40 |
if (length(theme) > 1) stop("`theme` must point to a single CSS file, not multiple files.") |
| 41 |
tags$head(tags$link(rel="stylesheet", type="text/css", href=theme)) |
| 42 |
}, |
| 43 |
# remainder of tags passed to the function |
| 44 |
list2(...) |
| 45 |
) |
| 46 |
|
| 47 |
# If theme is a bslib::bs_theme() object, bootstrapLib() needs to come first |
| 48 |
# (so other tags, when rendered via tagFunction(), know about the relevant |
| 49 |
# theme). However, if theme is anything else, we intentionally avoid changing |
| 50 |
# the tagList() contents to avoid breaking user code that makes assumptions |
| 51 |
# about the return value https://github.com/rstudio/shiny/issues/3235 |
| 52 |
if (is_bs_theme(theme)) { |
| 53 |
args <- c(bootstrapLib(theme), args) |
| 54 |
ui <- do.call(tagList, args) |
| 55 |
} else { |
| 56 |
ui <- do.call(tagList, args) |
| 57 |
ui <- attachDependencies(ui, bootstrapLib()) |
| 58 |
} |
| 59 |
|
| 60 |
setLang(ui, lang) |
| 61 |
} |
| 62 |
|
| 63 |
setLang <- function(ui, lang) { |
| 64 |
# Add lang attribute to be passed to renderPage function |
| 65 |
attr(ui, "lang") <- lang |
| 66 |
ui |
| 67 |
} |
| 68 |
getLang <- function(ui) { |
| 69 |
# Check if ui has lang attribute; otherwise, NULL |
| 70 |
attr(ui, "lang", exact = TRUE) |
| 71 |
} |
| 72 |
|
| 73 |
#' Bootstrap libraries |
| 74 |
#' |
| 75 |
#' This function defines a set of web dependencies necessary for using Bootstrap |
| 76 |
#' components in a web page. |
| 77 |
#' |
| 78 |
#' It isn't necessary to call this function if you use [bootstrapPage()] or |
| 79 |
#' others which use `bootstrapPage`, such [fluidPage()], [navbarPage()], |
| 80 |
#' [fillPage()], etc, because they already include the Bootstrap web dependencies. |
| 81 |
#' |
| 82 |
#' @inheritParams bootstrapPage |
| 83 |
#' @export |
| 84 |
bootstrapLib <- function(theme = NULL) { |
| 85 |
tagFunction(function() { |
| 86 |
if (isRunning()) { |
| 87 |
setCurrentTheme(theme) |
| 88 |
} |
| 89 |
|
| 90 |
# If we're not compiling Bootstrap Sass (from bslib), return the |
| 91 |
# static Bootstrap build. |
| 92 |
if (!is_bs_theme(theme)) { |
| 93 |
# We'll enter here if `theme` is the path to a .css file, like that |
| 94 |
# provided by `shinythemes::shinytheme("darkly")`. |
| 95 |
return(bootstrapDependency(theme)) |
| 96 |
} |
| 97 |
|
| 98 |
# Make bootstrap Sass available so other tagFunction()s (e.g., |
| 99 |
# sliderInput() et al) can resolve their HTML dependencies at render time |
| 100 |
# using getCurrentTheme(). Note that we're making an implicit assumption |
| 101 |
# that this tagFunction() executes *before* all other tagFunction()s; but |
| 102 |
# that should be fine considering that, DOM tree order is preorder, |
| 103 |
# depth-first traversal, and at least in the bootstrapPage(theme) case, we |
| 104 |
# have control over the relative ordering. |
| 105 |
# https://dom.spec.whatwg.org/#concept-tree |
| 106 |
# https://stackoverflow.com/a/16113998/1583084 |
| 107 |
# |
| 108 |
# Note also that since this is shinyOptions() (and not options()), the |
| 109 |
# option is automatically reset when the app (or session) exits |
| 110 |
if (isRunning()) { |
| 111 |
registerThemeDependency(bs_theme_deps) |
| 112 |
|
| 113 |
} else { |
| 114 |
# Technically, this a potential issue (someone trying to execute/render |
| 115 |
# bootstrapLib outside of a Shiny app), but it seems that, in that case, |
| 116 |
# you likely have other problems, since sliderInput() et al. already assume |
| 117 |
# that Shiny is the one doing the rendering |
| 118 |
#warning( |
| 119 |
# "It appears `shiny::bootstrapLib()` was rendered outside of an Shiny ", |
| 120 |
# "application context, likely by calling `as.tags()`, `as.character()`, ", |
| 121 |
# "or `print()` directly on `bootstrapLib()` or UI components that may ", |
| 122 |
# "depend on it (e.g., `fluidPage()`, etc). For 'themable' UI components ", |
| 123 |
# "(e.g., `sliderInput()`, `selectInput()`, `dateInput()`, etc) to style ", |
| 124 |
# "themselves based on the Bootstrap theme, make sure `bootstrapLib()` is ", |
| 125 |
# "provided directly to the UI and that the UI is provided direction to ", |
| 126 |
# "`shinyApp()` (or `runApp()`)", call. = FALSE |
| 127 |
#) |
| 128 |
} |
| 129 |
|
| 130 |
bslib::bs_theme_dependencies(theme) |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
# This is defined outside of bootstrapLib() because registerThemeDependency() |
| 135 |
# wants a non-anonymous function with a single argument |
| 136 |
bs_theme_deps <- function(theme) { |
| 137 |
bslib::bs_theme_dependencies(theme) |
| 138 |
} |
| 139 |
|
| 140 |
is_bs_theme <- function(x) { |
| 141 |
bslib::is_bs_theme(x) |
| 142 |
} |
| 143 |
|
| 144 |
#' Obtain Shiny's Bootstrap Sass theme |
| 145 |
#' |
| 146 |
#' Intended for use by Shiny developers to create Shiny bindings with intelligent |
| 147 |
#' styling based on the [bootstrapLib()]'s `theme` value. |
| 148 |
#' |
| 149 |
#' @return If called at render-time (i.e., inside a [htmltools::tagFunction()]), |
| 150 |
#' and [bootstrapLib()]'s `theme` has been set to a [bslib::bs_theme()] |
| 151 |
#' object, then this returns the `theme`. Otherwise, this returns `NULL`. |
| 152 |
#' @seealso [getCurrentOutputInfo()], [bootstrapLib()], [htmltools::tagFunction()] |
| 153 |
#' |
| 154 |
#' @keywords internal |
| 155 |
#' @export |
| 156 |
getCurrentTheme <- function() { |
| 157 |
getShinyOption("bootstrapTheme", default = NULL) |
| 158 |
} |
| 159 |
|
| 160 |
getCurrentThemeVersion <- function() { |
| 161 |
theme <- getCurrentTheme() |
| 162 |
if (bslib::is_bs_theme(theme)) { |
| 163 |
bslib::theme_version(theme) |
| 164 |
} else { |
| 165 |
strsplit(bootstrapVersion, ".", fixed = TRUE)[[1]][[1]] |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
setCurrentTheme <- function(theme) { |
| 170 |
shinyOptions(bootstrapTheme = theme) |
| 171 |
} |
| 172 |
|
| 173 |
#' Register a theme dependency |
| 174 |
#' |
| 175 |
#' This function registers a function that returns an |
| 176 |
#' [htmltools::htmlDependency()] or list of such objects. If |
| 177 |
#' `session$setCurrentTheme()` is called, the function will be re-executed, and |
| 178 |
#' the resulting html dependency will be sent to the client. |
| 179 |
#' |
| 180 |
#' Note that `func` should **not** be an anonymous function, or a function which |
| 181 |
#' is defined within the calling function. This is so that, |
| 182 |
#' `registerThemeDependency()` is called multiple times with the function, it |
| 183 |
#' tries to deduplicate them |
| 184 |
#' |
| 185 |
#' @param func A function that takes one argument, `theme` (which is a |
| 186 |
#' [sass::sass_layer()] object), and returns an htmlDependency object, or list |
| 187 |
#' of them. |
| 188 |
#' |
| 189 |
#' @export |
| 190 |
#' @keywords internal |
| 191 |
registerThemeDependency <- function(func) { |
| 192 |
func_expr <- substitute(func) |
| 193 |
if (is.call(func_expr) && identical(func_expr[[1]], as.symbol("function"))) { |
| 194 |
warning("`func` should not be an anonymous function. ", |
| 195 |
"It should be declared outside of the function that calls registerThemeDependency(); ", |
| 196 |
"otherwise it will not be deduplicated by Shiny and multiple copies of the ", |
| 197 |
"resulting htmlDependency may be computed and sent to the client.") |
| 198 |
} |
| 199 |
if (!is.function(func) || length(formals(func)) != 1) { |
| 200 |
stop("`func` must be a function with one argument (the current theme)") |
| 201 |
} |
| 202 |
|
| 203 |
# Note that this will automatically scope to the app or session level, |
| 204 |
# depending on if this is called from within a session or not. |
| 205 |
funcs <- getShinyOption("themeDependencyFuncs", default = list()) |
| 206 |
|
| 207 |
# Don't add func if it's already present. |
| 208 |
have_func <- any(vapply(funcs, identical, logical(1), func)) |
| 209 |
if (!have_func) { |
| 210 |
funcs[[length(funcs) + 1]] <- func |
| 211 |
} |
| 212 |
|
| 213 |
shinyOptions("themeDependencyFuncs" = funcs) |
| 214 |
} |
| 215 |
|
| 216 |
bootstrapDependency <- function(theme) { |
| 217 |
htmlDependency( |
| 218 |
"bootstrap", |
| 219 |
bootstrapVersion, |
| 220 |
src = "www/shared/bootstrap", |
| 221 |
package = "shiny", |
| 222 |
script = c( |
| 223 |
"js/bootstrap.min.js", |
| 224 |
# Safely adding accessibility plugin for screen readers and keyboard users; no break for sighted aspects (see https://github.com/paypal/bootstrap-accessibility-plugin) |
| 225 |
"accessibility/js/bootstrap-accessibility.min.js" |
| 226 |
), |
| 227 |
stylesheet = c( |
| 228 |
theme %||% "css/bootstrap.min.css", |
| 229 |
# Safely adding accessibility plugin for screen readers and keyboard users; no break for sighted aspects (see https://github.com/paypal/bootstrap-accessibility-plugin) |
| 230 |
"accessibility/css/bootstrap-accessibility.min.css" |
| 231 |
), |
| 232 |
meta = list(viewport = "width=device-width, initial-scale=1") |
| 233 |
) |
| 234 |
} |
| 235 |
|
| 236 |
bootstrapVersion <- "3.4.1" |
| 237 |
|
| 238 |
|
| 239 |
#' @rdname bootstrapPage |
| 240 |
#' @export |
| 241 |
basicPage <- function(...) { |
| 242 |
bootstrapPage(div(class="container-fluid", list(...))) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
#' Create a page that fills the window |
| 247 |
#' |
| 248 |
#' `fillPage` creates a page whose height and width always fill the |
| 249 |
#' available area of the browser window. |
| 250 |
#' |
| 251 |
#' The [fluidPage()] and [fixedPage()] functions are used |
| 252 |
#' for creating web pages that are laid out from the top down, leaving |
| 253 |
#' whitespace at the bottom if the page content's height is smaller than the |
| 254 |
#' browser window, and scrolling if the content is larger than the window. |
| 255 |
#' |
| 256 |
#' `fillPage` is designed to latch the document body's size to the size of |
| 257 |
#' the window. This makes it possible to fill it with content that also scales |
| 258 |
#' to the size of the window. |
| 259 |
#' |
| 260 |
#' For example, `fluidPage(plotOutput("plot", height = "100%"))` will not |
| 261 |
#' work as expected; the plot element's effective height will be `0`, |
| 262 |
#' because the plot's containing elements (`<div>` and `<body>`) have |
| 263 |
#' *automatic* height; that is, they determine their own height based on |
| 264 |
#' the height of their contained elements. However, |
| 265 |
#' `fillPage(plotOutput("plot", height = "100%"))` will work because |
| 266 |
#' `fillPage` fixes the `<body>` height at 100% of the window height. |
| 267 |
#' |
| 268 |
#' Note that `fillPage(plotOutput("plot"))` will not cause the plot to fill |
| 269 |
#' the page. Like most Shiny output widgets, `plotOutput`'s default height |
| 270 |
#' is a fixed number of pixels. You must explicitly set `height = "100%"` |
| 271 |
#' if you want a plot (or htmlwidget, say) to fill its container. |
| 272 |
#' |
| 273 |
#' One must be careful what layouts/panels/elements come between the |
| 274 |
#' `fillPage` and the plots/widgets. Any container that has an automatic |
| 275 |
#' height will cause children with `height = "100%"` to misbehave. Stick |
| 276 |
#' to functions that are designed for fill layouts, such as the ones in this |
| 277 |
#' package. |
| 278 |
#' |
| 279 |
#' @param ... Elements to include within the page. |
| 280 |
#' @param padding Padding to use for the body. This can be a numeric vector |
| 281 |
#' (which will be interpreted as pixels) or a character vector with valid CSS |
| 282 |
#' lengths. The length can be between one and four. If one, then that value |
| 283 |
#' will be used for all four sides. If two, then the first value will be used |
| 284 |
#' for the top and bottom, while the second value will be used for left and |
| 285 |
#' right. If three, then the first will be used for top, the second will be |
| 286 |
#' left and right, and the third will be bottom. If four, then the values will |
| 287 |
#' be interpreted as top, right, bottom, and left respectively. |
| 288 |
#' @param title The title to use for the browser window/tab (it will not be |
| 289 |
#' shown in the document). |
| 290 |
#' @param bootstrap If `TRUE`, load the Bootstrap CSS library. |
| 291 |
#' @inheritParams bootstrapPage |
| 292 |
#' |
| 293 |
#' @family layout functions |
| 294 |
#' |
| 295 |
#' @examples |
| 296 |
#' fillPage( |
| 297 |
#' tags$style(type = "text/css", |
| 298 |
#' ".half-fill { width: 50%; height: 100%; }", |
| 299 |
#' "#one { float: left; background-color: #ddddff; }", |
| 300 |
#' "#two { float: right; background-color: #ccffcc; }" |
| 301 |
#' ), |
| 302 |
#' div(id = "one", class = "half-fill", |
| 303 |
#' "Left half" |
| 304 |
#' ), |
| 305 |
#' div(id = "two", class = "half-fill", |
| 306 |
#' "Right half" |
| 307 |
#' ), |
| 308 |
#' padding = 10 |
| 309 |
#' ) |
| 310 |
#' |
| 311 |
#' fillPage( |
| 312 |
#' fillRow( |
| 313 |
#' div(style = "background-color: red; width: 100%; height: 100%;"), |
| 314 |
#' div(style = "background-color: blue; width: 100%; height: 100%;") |
| 315 |
#' ) |
| 316 |
#' ) |
| 317 |
#' @export |
| 318 |
fillPage <- function(..., padding = 0, title = NULL, bootstrap = TRUE, |
| 319 |
theme = NULL, lang = NULL) { |
| 320 |
|
| 321 |
fillCSS <- tags$head(tags$style(type = "text/css", |
| 322 |
"html, body { width: 100%; height: 100%; overflow: hidden; }", |
| 323 |
sprintf("body { padding: %s; margin: 0; }", collapseSizes(padding)) |
| 324 |
)) |
| 325 |
|
| 326 |
if (isTRUE(bootstrap)) { |
| 327 |
ui <- bootstrapPage(title = title, theme = theme, fillCSS, lang = lang, ...) |
| 328 |
} else { |
| 329 |
ui <- tagList( |
| 330 |
fillCSS, |
| 331 |
if (!is.null(title)) tags$head(tags$title(title)), |
| 332 |
... |
| 333 |
) |
| 334 |
|
| 335 |
ui <- setLang(ui, lang) |
| 336 |
} |
| 337 |
|
| 338 |
return(ui) |
| 339 |
} |
| 340 |
|
| 341 |
collapseSizes <- function(padding) { |
| 342 |
paste( |
| 343 |
sapply(padding, shiny::validateCssUnit, USE.NAMES = FALSE), |
| 344 |
collapse = " ") |
| 345 |
} |
| 346 |
|
| 347 |
#' Create a page with a top level navigation bar |
| 348 |
#' |
| 349 |
#' Create a page that contains a top level navigation bar that can be used to |
| 350 |
#' toggle a set of [tabPanel()] elements. |
| 351 |
#' |
| 352 |
#' @param title The title to display in the navbar |
| 353 |
#' @param ... [tabPanel()] elements to include in the page. The |
| 354 |
#' `navbarMenu` function also accepts strings, which will be used as menu |
| 355 |
#' section headers. If the string is a set of dashes like `"----"` a |
| 356 |
#' horizontal separator will be displayed in the menu. |
| 357 |
#' @param id If provided, you can use `input$`*`id`* in your |
| 358 |
#' server logic to determine which of the current tabs is active. The value |
| 359 |
#' will correspond to the `value` argument that is passed to |
| 360 |
#' [tabPanel()]. |
| 361 |
#' @param selected The `value` (or, if none was supplied, the `title`) |
| 362 |
#' of the tab that should be selected by default. If `NULL`, the first |
| 363 |
#' tab will be selected. |
| 364 |
#' @param position Determines whether the navbar should be displayed at the top |
| 365 |
#' of the page with normal scrolling behavior (`"static-top"`), pinned at |
| 366 |
#' the top (`"fixed-top"`), or pinned at the bottom |
| 367 |
#' (`"fixed-bottom"`). Note that using `"fixed-top"` or |
| 368 |
#' `"fixed-bottom"` will cause the navbar to overlay your body content, |
| 369 |
#' unless you add padding, e.g.: \code{tags$style(type="text/css", "body |
| 370 |
#' {padding-top: 70px;}")} |
| 371 |
#' @param header Tag or list of tags to display as a common header above all |
| 372 |
#' tabPanels. |
| 373 |
#' @param footer Tag or list of tags to display as a common footer below all |
| 374 |
#' tabPanels |
| 375 |
#' @param inverse `TRUE` to use a dark background and light text for the |
| 376 |
#' navigation bar |
| 377 |
#' @param collapsible `TRUE` to automatically collapse the navigation |
| 378 |
#' elements into an expandable menu on mobile devices or narrow window widths. |
| 379 |
#' @param fluid `TRUE` to use a fluid layout. `FALSE` to use a fixed |
| 380 |
#' layout. |
| 381 |
#' @param windowTitle the browser window title (as a character string). The |
| 382 |
#' default value, `NA`, means to use any character strings that appear in |
| 383 |
#' `title` (if none are found, the host URL of the page is displayed by |
| 384 |
#' default). |
| 385 |
#' @inheritParams bootstrapPage |
| 386 |
#' @param icon Optional icon to appear on a `navbarMenu` tab. |
| 387 |
#' |
| 388 |
#' @return A UI definition that can be passed to the [shinyUI] function. |
| 389 |
#' |
| 390 |
#' @details The `navbarMenu` function can be used to create an embedded |
| 391 |
#' menu within the navbar that in turns includes additional tabPanels (see |
| 392 |
#' example below). |
| 393 |
#' |
| 394 |
#' @seealso [tabPanel()], [tabsetPanel()], |
| 395 |
#' [updateNavbarPage()], [insertTab()], |
| 396 |
#' [showTab()] |
| 397 |
#' |
| 398 |
#' @family layout functions |
| 399 |
#' |
| 400 |
#' @examples |
| 401 |
#' navbarPage("App Title", |
| 402 |
#' tabPanel("Plot"), |
| 403 |
#' tabPanel("Summary"), |
| 404 |
#' tabPanel("Table") |
| 405 |
#' ) |
| 406 |
#' |
| 407 |
#' navbarPage("App Title", |
| 408 |
#' tabPanel("Plot"), |
| 409 |
#' navbarMenu("More", |
| 410 |
#' tabPanel("Summary"), |
| 411 |
#' "----", |
| 412 |
#' "Section header", |
| 413 |
#' tabPanel("Table") |
| 414 |
#' ) |
| 415 |
#' ) |
| 416 |
#' @export |
| 417 |
navbarPage <- function(title, |
| 418 |
..., |
| 419 |
id = NULL, |
| 420 |
selected = NULL, |
| 421 |
position = c("static-top", "fixed-top", "fixed-bottom"), |
| 422 |
header = NULL, |
| 423 |
footer = NULL, |
| 424 |
inverse = FALSE, |
| 425 |
collapsible = FALSE, |
| 426 |
fluid = TRUE, |
| 427 |
theme = NULL, |
| 428 |
windowTitle = NA, |
| 429 |
lang = NULL) { |
| 430 |
remove_first_class(bslib::page_navbar( |
| 431 |
..., title = title, id = id, selected = selected, |
| 432 |
position = match.arg(position), |
| 433 |
header = header, footer = footer, |
| 434 |
inverse = inverse, collapsible = collapsible, |
| 435 |
fluid = fluid, |
| 436 |
theme = theme, |
| 437 |
window_title = windowTitle, |
| 438 |
lang = lang |
| 439 |
)) |
| 440 |
} |
| 441 |
|
| 442 |
#' @param menuName A name that identifies this `navbarMenu`. This |
| 443 |
#' is needed if you want to insert/remove or show/hide an entire |
| 444 |
#' `navbarMenu`. |
| 445 |
#' |
| 446 |
#' @rdname navbarPage |
| 447 |
#' @export |
| 448 |
navbarMenu <- function(title, ..., menuName = title, icon = NULL) { |
| 449 |
bslib::nav_menu(title, ..., value = menuName, icon = icon) |
| 450 |
} |
| 451 |
|
| 452 |
#' Create a well panel |
| 453 |
#' |
| 454 |
#' Creates a panel with a slightly inset border and grey background. Equivalent |
| 455 |
#' to Bootstrap's `well` CSS class. |
| 456 |
#' |
| 457 |
#' @param ... UI elements to include inside the panel. |
| 458 |
#' @return The newly created panel. |
| 459 |
#' @export |
| 460 |
wellPanel <- function(...) { |
| 461 |
div(class="well", ...) |
| 462 |
} |
| 463 |
|
| 464 |
#' Conditional Panel |
| 465 |
#' |
| 466 |
#' Creates a panel that is visible or not, depending on the value of a |
| 467 |
#' JavaScript expression. The JS expression is evaluated once at startup and |
| 468 |
#' whenever Shiny detects a relevant change in input/output. |
| 469 |
#' |
| 470 |
#' In the JS expression, you can refer to `input` and `output` |
| 471 |
#' JavaScript objects that contain the current values of input and output. For |
| 472 |
#' example, if you have an input with an id of `foo`, then you can use |
| 473 |
#' `input.foo` to read its value. (Be sure not to modify the input/output |
| 474 |
#' objects, as this may cause unpredictable behavior.) |
| 475 |
#' |
| 476 |
#' @param condition A JavaScript expression that will be evaluated repeatedly to |
| 477 |
#' determine whether the panel should be displayed. |
| 478 |
#' @param ns The [`namespace()`][NS] object of the current module, if |
| 479 |
#' any. |
| 480 |
#' @param ... Elements to include in the panel. |
| 481 |
#' |
| 482 |
#' @note You are not recommended to use special JavaScript characters such as a |
| 483 |
#' period `.` in the input id's, but if you do use them anyway, for |
| 484 |
#' example, `inputId = "foo.bar"`, you will have to use |
| 485 |
#' `input["foo.bar"]` instead of `input.foo.bar` to read the input |
| 486 |
#' value. |
| 487 |
#' @examples |
| 488 |
#' ## Only run this example in interactive R sessions |
| 489 |
#' if (interactive()) { |
| 490 |
#' ui <- fluidPage( |
| 491 |
#' sidebarPanel( |
| 492 |
#' selectInput("plotType", "Plot Type", |
| 493 |
#' c(Scatter = "scatter", Histogram = "hist") |
| 494 |
#' ), |
| 495 |
#' # Only show this panel if the plot type is a histogram |
| 496 |
#' conditionalPanel( |
| 497 |
#' condition = "input.plotType == 'hist'", |
| 498 |
#' selectInput( |
| 499 |
#' "breaks", "Breaks", |
| 500 |
#' c("Sturges", "Scott", "Freedman-Diaconis", "[Custom]" = "custom") |
| 501 |
#' ), |
| 502 |
#' # Only show this panel if Custom is selected |
| 503 |
#' conditionalPanel( |
| 504 |
#' condition = "input.breaks == 'custom'", |
| 505 |
#' sliderInput("breakCount", "Break Count", min = 1, max = 50, value = 10) |
| 506 |
#' ) |
| 507 |
#' ) |
| 508 |
#' ), |
| 509 |
#' mainPanel( |
| 510 |
#' plotOutput("plot") |
| 511 |
#' ) |
| 512 |
#' ) |
| 513 |
#' |
| 514 |
#' server <- function(input, output) { |
| 515 |
#' x <- rnorm(100) |
| 516 |
#' y <- rnorm(100) |
| 517 |
#' |
| 518 |
#' output$plot <- renderPlot({ |
| 519 |
#' if (input$plotType == "scatter") { |
| 520 |
#' plot(x, y) |
| 521 |
#' } else { |
| 522 |
#' breaks <- input$breaks |
| 523 |
#' if (breaks == "custom") { |
| 524 |
#' breaks <- input$breakCount |
| 525 |
#' } |
| 526 |
#' |
| 527 |
#' hist(x, breaks = breaks) |
| 528 |
#' } |
| 529 |
#' }) |
| 530 |
#' } |
| 531 |
#' |
| 532 |
#' shinyApp(ui, server) |
| 533 |
#' } |
| 534 |
#' @export |
| 535 |
conditionalPanel <- function(condition, ..., ns = NS(NULL)) { |
| 536 |
div( |
| 537 |
class = "shiny-panel-conditional", |
| 538 |
`data-display-if` = condition, |
| 539 |
`data-ns-prefix` = ns(""), |
| 540 |
... |
| 541 |
) |
| 542 |
} |
| 543 |
|
| 544 |
#' Create a help text element |
| 545 |
#' |
| 546 |
#' Create help text which can be added to an input form to provide additional |
| 547 |
#' explanation or context. |
| 548 |
#' |
| 549 |
#' @param ... One or more help text strings (or other inline HTML elements) |
| 550 |
#' @return A help text element that can be added to a UI definition. |
| 551 |
#' |
| 552 |
#' @examples |
| 553 |
#' helpText("Note: while the data view will show only", |
| 554 |
#' "the specified number of observations, the", |
| 555 |
#' "summary will be based on the full dataset.") |
| 556 |
#' @export |
| 557 |
helpText <- function(...) { |
| 558 |
span(class="help-block", ...) |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
#' Create a tab panel |
| 563 |
#' |
| 564 |
#' |
| 565 |
#' @param title Display title for tab |
| 566 |
#' @param ... UI elements to include within the tab |
| 567 |
#' @param value The value that should be sent when `tabsetPanel` reports |
| 568 |
#' that this tab is selected. If omitted and `tabsetPanel` has an |
| 569 |
#' `id`, then the title will be used. |
| 570 |
#' @param icon Optional icon to appear on the tab. This attribute is only |
| 571 |
#' valid when using a `tabPanel` within a [navbarPage()]. |
| 572 |
#' @return A tab that can be passed to [tabsetPanel()] |
| 573 |
#' |
| 574 |
#' @seealso [tabsetPanel()] |
| 575 |
#' |
| 576 |
#' @examples |
| 577 |
#' # Show a tabset that includes a plot, summary, and |
| 578 |
#' # table view of the generated distribution |
| 579 |
#' mainPanel( |
| 580 |
#' tabsetPanel( |
| 581 |
#' tabPanel("Plot", plotOutput("plot")), |
| 582 |
#' tabPanel("Summary", verbatimTextOutput("summary")), |
| 583 |
#' tabPanel("Table", tableOutput("table")) |
| 584 |
#' ) |
| 585 |
#' ) |
| 586 |
#' @export |
| 587 |
#' @describeIn tabPanel Create a tab panel that can be included within a [tabsetPanel()] or a [navbarPage()]. |
| 588 |
tabPanel <- function(title, ..., value = title, icon = NULL) { |
| 589 |
bslib::nav(title, ..., value = value, icon = icon) |
| 590 |
} |
| 591 |
|
| 592 |
#' @export |
| 593 |
#' @describeIn tabPanel Create a tab panel that drops the title argument. |
| 594 |
#' This function should be used within `tabsetPanel(type = "hidden")`. See [tabsetPanel()] for example usage. |
| 595 |
tabPanelBody <- function(value, ..., icon = NULL) { |
| 596 |
bslib::nav_content(value, ..., icon = icon) |
| 597 |
} |
| 598 |
|
| 599 |
#' Create a tabset panel |
| 600 |
#' |
| 601 |
#' Create a tabset that contains [tabPanel()] elements. Tabsets are |
| 602 |
#' useful for dividing output into multiple independently viewable sections. |
| 603 |
#' |
| 604 |
#' @param ... [tabPanel()] elements to include in the tabset |
| 605 |
#' @param id If provided, you can use `input$`*`id`* in your |
| 606 |
#' server logic to determine which of the current tabs is active. The value |
| 607 |
#' will correspond to the `value` argument that is passed to |
| 608 |
#' [tabPanel()]. |
| 609 |
#' @param selected The `value` (or, if none was supplied, the `title`) |
| 610 |
#' of the tab that should be selected by default. If `NULL`, the first |
| 611 |
#' tab will be selected. |
| 612 |
#' @param type \describe{ |
| 613 |
#' \item{`"tabs"`}{Standard tab look} |
| 614 |
#' \item{`"pills"`}{Selected tabs use the background fill color} |
| 615 |
#' \item{`"hidden"`}{Hides the selectable tabs. Use `type = "hidden"` in |
| 616 |
#' conjunction with [tabPanelBody()] and [updateTabsetPanel()] to control the |
| 617 |
#' active tab via other input controls. (See example below)} |
| 618 |
#' } |
| 619 |
#' @inheritParams navbarPage |
| 620 |
#' @return A tabset that can be passed to [mainPanel()] |
| 621 |
#' |
| 622 |
#' @seealso [tabPanel()], [updateTabsetPanel()], |
| 623 |
#' [insertTab()], [showTab()] |
| 624 |
#' |
| 625 |
#' @examples |
| 626 |
#' # Show a tabset that includes a plot, summary, and |
| 627 |
#' # table view of the generated distribution |
| 628 |
#' mainPanel( |
| 629 |
#' tabsetPanel( |
| 630 |
#' tabPanel("Plot", plotOutput("plot")), |
| 631 |
#' tabPanel("Summary", verbatimTextOutput("summary")), |
| 632 |
#' tabPanel("Table", tableOutput("table")) |
| 633 |
#' ) |
| 634 |
#' ) |
| 635 |
#' |
| 636 |
#' ui <- fluidPage( |
| 637 |
#' sidebarLayout( |
| 638 |
#' sidebarPanel( |
| 639 |
#' radioButtons("controller", "Controller", 1:3, 1) |
| 640 |
#' ), |
| 641 |
#' mainPanel( |
| 642 |
#' tabsetPanel( |
| 643 |
#' id = "hidden_tabs", |
| 644 |
#' # Hide the tab values. |
| 645 |
#' # Can only switch tabs by using `updateTabsetPanel()` |
| 646 |
#' type = "hidden", |
| 647 |
#' tabPanelBody("panel1", "Panel 1 content"), |
| 648 |
#' tabPanelBody("panel2", "Panel 2 content"), |
| 649 |
#' tabPanelBody("panel3", "Panel 3 content") |
| 650 |
#' ) |
| 651 |
#' ) |
| 652 |
#' ) |
| 653 |
#' ) |
| 654 |
#' |
| 655 |
#' server <- function(input, output, session) { |
| 656 |
#' observeEvent(input$controller, { |
| 657 |
#' updateTabsetPanel(session, "hidden_tabs", selected = paste0("panel", input$controller)) |
| 658 |
#' }) |
| 659 |
#' } |
| 660 |
#' |
| 661 |
#' if (interactive()) { |
| 662 |
#' shinyApp(ui, server) |
| 663 |
#' } |
| 664 |
#' @export |
| 665 |
tabsetPanel <- function(..., |
| 666 |
id = NULL, |
| 667 |
selected = NULL, |
| 668 |
type = c("tabs", "pills", "hidden"), |
| 669 |
header = NULL, |
| 670 |
footer = NULL) { |
| 671 |
|
| 672 |
func <- switch( |
| 673 |
match.arg(type), |
| 674 |
tabs = bslib::navs_tab, |
| 675 |
pills = bslib::navs_pill, |
| 676 |
hidden = bslib::navs_hidden |
| 677 |
) |
| 678 |
|
| 679 |
# bslib adds a class to make the content browsable() by default, |
| 680 |
# but that's probably too big of a change for shiny |
| 681 |
remove_first_class( |
| 682 |
func(..., id = id, selected = selected, header = header, footer = footer) |
| 683 |
) |
| 684 |
} |
| 685 |
|
| 686 |
#' Create a navigation list panel |
| 687 |
#' |
| 688 |
#' Create a navigation list panel that provides a list of links on the left |
| 689 |
#' which navigate to a set of tabPanels displayed to the right. |
| 690 |
#' |
| 691 |
#' @param ... [tabPanel()] elements to include in the navlist |
| 692 |
#' @param id If provided, you can use `input$`*`id`* in your |
| 693 |
#' server logic to determine which of the current navlist items is active. The |
| 694 |
#' value will correspond to the `value` argument that is passed to |
| 695 |
#' [tabPanel()]. |
| 696 |
#' @param selected The `value` (or, if none was supplied, the `title`) |
| 697 |
#' of the navigation item that should be selected by default. If `NULL`, |
| 698 |
#' the first navigation will be selected. |
| 699 |
#' @param well `TRUE` to place a well (gray rounded rectangle) around the |
| 700 |
#' navigation list. |
| 701 |
#' @param fluid `TRUE` to use fluid layout; `FALSE` to use fixed |
| 702 |
#' layout. |
| 703 |
#' @param widths Column widths of the navigation list and tabset content areas |
| 704 |
#' respectively. |
| 705 |
#' @inheritParams tabsetPanel |
| 706 |
#' @inheritParams navbarPage |
| 707 |
#' |
| 708 |
#' @details You can include headers within the `navlistPanel` by including |
| 709 |
#' plain text elements in the list. Versions of Shiny before 0.11 supported |
| 710 |
#' separators with "------", but as of 0.11, separators were no longer |
| 711 |
#' supported. This is because version 0.11 switched to Bootstrap 3, which |
| 712 |
#' doesn't support separators. |
| 713 |
#' |
| 714 |
#' @seealso [tabPanel()], [updateNavlistPanel()], |
| 715 |
#' [insertTab()], [showTab()] |
| 716 |
#' |
| 717 |
#' @examples |
| 718 |
#' fluidPage( |
| 719 |
#' |
| 720 |
#' titlePanel("Application Title"), |
| 721 |
#' |
| 722 |
#' navlistPanel( |
| 723 |
#' "Header", |
| 724 |
#' tabPanel("First"), |
| 725 |
#' tabPanel("Second"), |
| 726 |
#' tabPanel("Third") |
| 727 |
#' ) |
| 728 |
#' ) |
| 729 |
#' @export |
| 730 |
navlistPanel <- function(..., |
| 731 |
id = NULL, |
| 732 |
selected = NULL, |
| 733 |
header = NULL, |
| 734 |
footer = NULL, |
| 735 |
well = TRUE, |
| 736 |
fluid = TRUE, |
| 737 |
widths = c(4, 8)) { |
| 738 |
remove_first_class(bslib::navs_pill_list( |
| 739 |
..., id = id, selected = selected, |
| 740 |
header = header, footer = footer, |
| 741 |
well = well, fluid = fluid, widths = widths |
| 742 |
)) |
| 743 |
} |
| 744 |
|
| 745 |
remove_first_class <- function(x) { |
| 746 |
class(x) <- class(x)[-1] |
| 747 |
x |
| 748 |
} |
| 749 |
|
| 750 |
#' Create a text output element |
| 751 |
#' |
| 752 |
#' Render a reactive output variable as text within an application page. |
| 753 |
#' `textOutput()` is usually paired with [renderText()] and puts regular text |
| 754 |
#' in `<div>` or `<span>`; `verbatimTextOutput()` is usually paired with |
| 755 |
#' [renderPrint()] and provides fixed-width text in a `<pre>`. |
| 756 |
#' |
| 757 |
#' In both functions, text is HTML-escaped prior to rendering. |
| 758 |
#' |
| 759 |
#' @param outputId output variable to read the value from |
| 760 |
#' @param container a function to generate an HTML element to contain the text |
| 761 |
#' @param inline use an inline (`span()`) or block container (`div()`) |
| 762 |
#' for the output |
| 763 |
#' @return An output element for use in UI. |
| 764 |
#' @examples |
| 765 |
#' ## Only run this example in interactive R sessions |
| 766 |
#' if (interactive()) { |
| 767 |
#' shinyApp( |
| 768 |
#' ui = basicPage( |
| 769 |
#' textInput("txt", "Enter the text to display below:"), |
| 770 |
#' textOutput("text"), |
| 771 |
#' verbatimTextOutput("verb") |
| 772 |
#' ), |
| 773 |
#' server = function(input, output) { |
| 774 |
#' output$text <- renderText({ input$txt }) |
| 775 |
#' output$verb <- renderText({ input$txt }) |
| 776 |
#' } |
| 777 |
#' ) |
| 778 |
#' } |
| 779 |
#' @export |
| 780 |
textOutput <- function(outputId, container = if (inline) span else div, inline = FALSE) { |
| 781 |
container(id = outputId, class = "shiny-text-output") |
| 782 |
} |
| 783 |
|
| 784 |
#' @param placeholder if the output is empty or `NULL`, should an empty |
| 785 |
#' rectangle be displayed to serve as a placeholder? (does not affect |
| 786 |
#' behavior when the output is nonempty) |
| 787 |
#' @export |
| 788 |
#' @rdname textOutput |
| 789 |
verbatimTextOutput <- function(outputId, placeholder = FALSE) { |
| 790 |
pre(id = outputId, |
| 791 |
class = "shiny-text-output", |
| 792 |
class = if (!placeholder) "noplaceholder" |
| 793 |
) |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
#' @name plotOutput |
| 798 |
#' @rdname plotOutput |
| 799 |
#' @export |
| 800 |
imageOutput <- function(outputId, width = "100%", height="400px", |
| 801 |
click = NULL, dblclick = NULL, hover = NULL, brush = NULL, |
| 802 |
inline = FALSE, fill = FALSE) { |
| 803 |
|
| 804 |
style <- if (!inline) { |
| 805 |
# Using `css()` here instead of paste/sprintf so that NULL values will |
| 806 |
# result in the property being dropped altogether |
| 807 |
css(width = validateCssUnit(width), height = validateCssUnit(height)) |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
# Build up arguments for call to div() or span() |
| 812 |
args <- list( |
| 813 |
id = outputId, |
| 814 |
class = "shiny-image-output", |
| 815 |
style = style |
| 816 |
) |
| 817 |
|
| 818 |
# Given a named list with options, replace names like "delayType" with |
| 819 |
# "data-hover-delay-type" (given a prefix "hover") |
| 820 |
formatOptNames <- function(opts, prefix) { |
| 821 |
newNames <- paste("data", prefix, names(opts), sep = "-") |
| 822 |
# Replace capital letters with "-" and lowercase letter |
| 823 |
newNames <- gsub("([A-Z])", "-\\L\\1", newNames, perl = TRUE) |
| 824 |
names(opts) <- newNames |
| 825 |
opts |
| 826 |
} |
| 827 |
|
| 828 |
if (!is.null(click)) { |
| 829 |
# If click is a string, turn it into clickOpts object |
| 830 |
if (is.character(click)) { |
| 831 |
click <- clickOpts(id = click) |
| 832 |
} |
| 833 |
args <- c(args, formatOptNames(click, "click")) |
| 834 |
} |
| 835 |
|
| 836 |
if (!is.null(dblclick)) { |
| 837 |
if (is.character(dblclick)) { |
| 838 |
dblclick <- clickOpts(id = dblclick) |
| 839 |
} |
| 840 |
args <- c(args, formatOptNames(dblclick, "dblclick")) |
| 841 |
} |
| 842 |
|
| 843 |
if (!is.null(hover)) { |
| 844 |
if (is.character(hover)) { |
| 845 |
hover <- hoverOpts(id = hover) |
| 846 |
} |
| 847 |
args <- c(args, formatOptNames(hover, "hover")) |
| 848 |
} |
| 849 |
|
| 850 |
if (!is.null(brush)) { |
| 851 |
if (is.character(brush)) { |
| 852 |
brush <- brushOpts(id = brush) |
| 853 |
} |
| 854 |
args <- c(args, formatOptNames(brush, "brush")) |
| 855 |
} |
| 856 |
|
| 857 |
container <- if (inline) span else div |
| 858 |
res <- do.call(container, args) |
| 859 |
bindFillRole(res, item = fill) |
| 860 |
} |
| 861 |
|
| 862 |
#' Create an plot or image output element |
| 863 |
#' |
| 864 |
#' Render a [renderPlot()] or [renderImage()] within an |
| 865 |
#' application page. |
| 866 |
#' |
| 867 |
#' @section Interactive plots: |
| 868 |
#' |
| 869 |
#' Plots and images in Shiny support mouse-based interaction, via clicking, |
| 870 |
#' double-clicking, hovering, and brushing. When these interaction events |
| 871 |
#' occur, the mouse coordinates will be sent to the server as `input$` |
| 872 |
#' variables, as specified by `click`, `dblclick`, `hover`, or |
| 873 |
#' `brush`. |
| 874 |
#' |
| 875 |
#' For `plotOutput`, the coordinates will be sent scaled to the data |
| 876 |
#' space, if possible. (At the moment, plots generated by base graphics and |
| 877 |
#' ggplot2 support this scaling, although plots generated by lattice and |
| 878 |
#' others do not.) If scaling is not possible, the raw pixel coordinates will |
| 879 |
#' be sent. For `imageOutput`, the coordinates will be sent in raw pixel |
| 880 |
#' coordinates. |
| 881 |
#' |
| 882 |
#' With ggplot2 graphics, the code in `renderPlot` should return a ggplot |
| 883 |
#' object; if instead the code prints the ggplot2 object with something like |
| 884 |
#' `print(p)`, then the coordinates for interactive graphics will not be |
| 885 |
#' properly scaled to the data space. |
| 886 |
#' |
| 887 |
#' @param outputId output variable to read the plot/image from. |
| 888 |
#' @param width,height Image width/height. Must be a valid CSS unit (like |
| 889 |
#' `"100%"`, `"400px"`, `"auto"`) or a number, which will be |
| 890 |
#' coerced to a string and have `"px"` appended. These two arguments are |
| 891 |
#' ignored when `inline = TRUE`, in which case the width/height of a plot |
| 892 |
#' must be specified in `renderPlot()`. Note that, for height, using |
| 893 |
#' `"auto"` or `"100%"` generally will not work as expected, |
| 894 |
#' because of how height is computed with HTML/CSS. |
| 895 |
#' @param click This can be `NULL` (the default), a string, or an object |
| 896 |
#' created by the [clickOpts()] function. If you use a value like |
| 897 |
#' `"plot_click"` (or equivalently, `clickOpts(id="plot_click")`), |
| 898 |
#' the plot will send coordinates to the server whenever it is clicked, and |
| 899 |
#' the value will be accessible via `input$plot_click`. The value will be |
| 900 |
#' a named list with `x` and `y` elements indicating the mouse |
| 901 |
#' position. |
| 902 |
#' @param dblclick This is just like the `click` argument, but for |
| 903 |
#' double-click events. |
| 904 |
#' @param hover Similar to the `click` argument, this can be `NULL` |
| 905 |
#' (the default), a string, or an object created by the |
| 906 |
#' [hoverOpts()] function. If you use a value like |
| 907 |
#' `"plot_hover"` (or equivalently, `hoverOpts(id="plot_hover")`), |
| 908 |
#' the plot will send coordinates to the server pauses on the plot, and the |
| 909 |
#' value will be accessible via `input$plot_hover`. The value will be a |
| 910 |
#' named list with `x` and `y` elements indicating the mouse |
| 911 |
#' position. To control the hover time or hover delay type, you must use |
| 912 |
#' [hoverOpts()]. |
| 913 |
#' @param brush Similar to the `click` argument, this can be `NULL` |
| 914 |
#' (the default), a string, or an object created by the |
| 915 |
#' [brushOpts()] function. If you use a value like |
| 916 |
#' `"plot_brush"` (or equivalently, `brushOpts(id="plot_brush")`), |
| 917 |
#' the plot will allow the user to "brush" in the plotting area, and will send |
| 918 |
#' information about the brushed area to the server, and the value will be |
| 919 |
#' accessible via `input$plot_brush`. Brushing means that the user will |
| 920 |
#' be able to draw a rectangle in the plotting area and drag it around. The |
| 921 |
#' value will be a named list with `xmin`, `xmax`, `ymin`, and |
| 922 |
#' `ymax` elements indicating the brush area. To control the brush |
| 923 |
#' behavior, use [brushOpts()]. Multiple |
| 924 |
#' `imageOutput`/`plotOutput` calls may share the same `id` |
| 925 |
#' value; brushing one image or plot will cause any other brushes with the |
| 926 |
#' same `id` to disappear. |
| 927 |
#' @param fill Whether or not the returned tag should be treated as a fill item, |
| 928 |
#' meaning that its `height` is allowed to grow/shrink to fit a fill container |
| 929 |
#' with an opinionated height (see [htmltools::bindFillRole()]) with an |
| 930 |
#' opinionated height. Examples of fill containers include `bslib::card()` and |
| 931 |
#' `bslib::card_body_fill()`. |
| 932 |
#' @inheritParams textOutput |
| 933 |
#' @note The arguments `clickId` and `hoverId` only work for R base graphics |
| 934 |
#' (see the \pkg{\link[graphics:graphics-package]{graphics}} package). They do |
| 935 |
#' not work for \pkg{\link[grid:grid-package]{grid}}-based graphics, such as |
| 936 |
#' \pkg{ggplot2}, \pkg{lattice}, and so on. |
| 937 |
#' @return A plot or image output element that can be included in a panel. |
| 938 |
#' @seealso For the corresponding server-side functions, see [renderPlot()] and |
| 939 |
#' [renderImage()]. |
| 940 |
#' |
| 941 |
#' @examples |
| 942 |
#' # Only run these examples in interactive R sessions |
| 943 |
#' if (interactive()) { |
| 944 |
#' |
| 945 |
#' # A basic shiny app with a plotOutput |
| 946 |
#' shinyApp( |
| 947 |
#' ui = fluidPage( |
| 948 |
#' sidebarLayout( |
| 949 |
#' sidebarPanel( |
| 950 |
#' actionButton("newplot", "New plot") |
| 951 |
#' ), |
| 952 |
#' mainPanel( |
| 953 |
#' plotOutput("plot") |
| 954 |
#' ) |
| 955 |
#' ) |
| 956 |
#' ), |
| 957 |
#' server = function(input, output) { |
| 958 |
#' output$plot <- renderPlot({ |
| 959 |
#' input$newplot |
| 960 |
#' # Add a little noise to the cars data |
| 961 |
#' cars2 <- cars + rnorm(nrow(cars)) |
| 962 |
#' plot(cars2) |
| 963 |
#' }) |
| 964 |
#' } |
| 965 |
#' ) |
| 966 |
#' |
| 967 |
#' |
| 968 |
#' # A demonstration of clicking, hovering, and brushing |
| 969 |
#' shinyApp( |
| 970 |
#' ui = basicPage( |
| 971 |
#' fluidRow( |
| 972 |
#' column(width = 4, |
| 973 |
#' plotOutput("plot", height=300, |
| 974 |
#' click = "plot_click", # Equiv, to click=clickOpts(id="plot_click") |
| 975 |
#' hover = hoverOpts(id = "plot_hover", delayType = "throttle"), |
| 976 |
#' brush = brushOpts(id = "plot_brush") |
| 977 |
#' ), |
| 978 |
#' h4("Clicked points"), |
| 979 |
#' tableOutput("plot_clickedpoints"), |
| 980 |
#' h4("Brushed points"), |
| 981 |
#' tableOutput("plot_brushedpoints") |
| 982 |
#' ), |
| 983 |
#' column(width = 4, |
| 984 |
#' verbatimTextOutput("plot_clickinfo"), |
| 985 |
#' verbatimTextOutput("plot_hoverinfo") |
| 986 |
#' ), |
| 987 |
#' column(width = 4, |
| 988 |
#' wellPanel(actionButton("newplot", "New plot")), |
| 989 |
#' verbatimTextOutput("plot_brushinfo") |
| 990 |
#' ) |
| 991 |
#' ) |
| 992 |
#' ), |
| 993 |
#' server = function(input, output, session) { |
| 994 |
#' data <- reactive({ |
| 995 |
#' input$newplot |
| 996 |
#' # Add a little noise to the cars data so the points move |
| 997 |
#' cars + rnorm(nrow(cars)) |
| 998 |
#' }) |
| 999 |
#' output$plot <- renderPlot({ |
| 1000 |
#' d <- data() |
| 1001 |
#' plot(d$speed, d$dist) |
| 1002 |
#' }) |
| 1003 |
#' output$plot_clickinfo <- renderPrint({ |
| 1004 |
#' cat("Click:\n") |
| 1005 |
#' str(input$plot_click) |
| 1006 |
#' }) |
| 1007 |
#' output$plot_hoverinfo <- renderPrint({ |
| 1008 |
#' cat("Hover (throttled):\n") |
| 1009 |
#' str(input$plot_hover) |
| 1010 |
#' }) |
| 1011 |
#' output$plot_brushinfo <- renderPrint({ |
| 1012 |
#' cat("Brush (debounced):\n") |
| 1013 |
#' str(input$plot_brush) |
| 1014 |
#' }) |
| 1015 |
#' output$plot_clickedpoints <- renderTable({ |
| 1016 |
#' # For base graphics, we need to specify columns, though for ggplot2, |
| 1017 |
#' # it's usually not necessary. |
| 1018 |
#' res <- nearPoints(data(), input$plot_click, "speed", "dist") |
| 1019 |
#' if (nrow(res) == 0) |
| 1020 |
#' return() |
| 1021 |
#' res |
| 1022 |
#' }) |
| 1023 |
#' output$plot_brushedpoints <- renderTable({ |
| 1024 |
#' res <- brushedPoints(data(), input$plot_brush, "speed", "dist") |
| 1025 |
#' if (nrow(res) == 0) |
| 1026 |
#' return() |
| 1027 |
#' res |
| 1028 |
#' }) |
| 1029 |
#' } |
| 1030 |
#' ) |
| 1031 |
#' |
| 1032 |
#' |
| 1033 |
#' # Demo of clicking, hovering, brushing with imageOutput |
| 1034 |
#' # Note that coordinates are in pixels |
| 1035 |
#' shinyApp( |
| 1036 |
#' ui = basicPage( |
| 1037 |
#' fluidRow( |
| 1038 |
#' column(width = 4, |
| 1039 |
#' imageOutput("image", height=300, |
| 1040 |
#' click = "image_click", |
| 1041 |
#' hover = hoverOpts( |
| 1042 |
#' id = "image_hover", |
| 1043 |
#' delay = 500, |
| 1044 |
#' delayType = "throttle" |
| 1045 |
#' ), |
| 1046 |
#' brush = brushOpts(id = "image_brush") |
| 1047 |
#' ) |
| 1048 |
#' ), |
| 1049 |
#' column(width = 4, |
| 1050 |
#' verbatimTextOutput("image_clickinfo"), |
| 1051 |
#' verbatimTextOutput("image_hoverinfo") |
| 1052 |
#' ), |
| 1053 |
#' column(width = 4, |
| 1054 |
#' wellPanel(actionButton("newimage", "New image")), |
| 1055 |
#' verbatimTextOutput("image_brushinfo") |
| 1056 |
#' ) |
| 1057 |
#' ) |
| 1058 |
#' ), |
| 1059 |
#' server = function(input, output, session) { |
| 1060 |
#' output$image <- renderImage({ |
| 1061 |
#' input$newimage |
| 1062 |
#' |
| 1063 |
#' # Get width and height of image output |
| 1064 |
#' width <- session$clientData$output_image_width |
| 1065 |
#' height <- session$clientData$output_image_height |
| 1066 |
#' |
| 1067 |
#' # Write to a temporary PNG file |
| 1068 |
#' outfile <- tempfile(fileext = ".png") |
| 1069 |
#' |
| 1070 |
#' png(outfile, width=width, height=height) |
| 1071 |
#' plot(rnorm(200), rnorm(200)) |
| 1072 |
#' dev.off() |
| 1073 |
#' |
| 1074 |
#' # Return a list containing information about the image |
| 1075 |
#' list( |
| 1076 |
#' src = outfile, |
| 1077 |
#' contentType = "image/png", |
| 1078 |
#' width = width, |
| 1079 |
#' height = height, |
| 1080 |
#' alt = "This is alternate text" |
| 1081 |
#' ) |
| 1082 |
#' }) |
| 1083 |
#' output$image_clickinfo <- renderPrint({ |
| 1084 |
#' cat("Click:\n") |
| 1085 |
#' str(input$image_click) |
| 1086 |
#' }) |
| 1087 |
#' output$image_hoverinfo <- renderPrint({ |
| 1088 |
#' cat("Hover (throttled):\n") |
| 1089 |
#' str(input$image_hover) |
| 1090 |
#' }) |
| 1091 |
#' output$image_brushinfo <- renderPrint({ |
| 1092 |
#' cat("Brush (debounced):\n") |
| 1093 |
#' str(input$image_brush) |
| 1094 |
#' }) |
| 1095 |
#' } |
| 1096 |
#' ) |
| 1097 |
#' |
| 1098 |
#' } |
| 1099 |
#' @export |
| 1100 |
plotOutput <- function(outputId, width = "100%", height="400px", |
| 1101 |
click = NULL, dblclick = NULL, hover = NULL, brush = NULL, |
| 1102 |
inline = FALSE, fill = !inline) { |
| 1103 |
|
| 1104 |
# Result is the same as imageOutput, except for HTML class |
| 1105 |
res <- imageOutput(outputId, width, height, click, dblclick, |
| 1106 |
hover, brush, inline, fill) |
| 1107 |
|
| 1108 |
res$attribs$class <- "shiny-plot-output" |
| 1109 |
res |
| 1110 |
} |
| 1111 |
|
| 1112 |
#' @param outputId output variable to read the table from |
| 1113 |
#' @rdname renderTable |
| 1114 |
#' @export |
| 1115 |
tableOutput <- function(outputId) { |
| 1116 |
div(id = outputId, class="shiny-html-output shiny-table-output") |
| 1117 |
} |
| 1118 |
|
| 1119 |
dataTableDependency <- list( |
| 1120 |
htmlDependency( |
| 1121 |
"datatables", |
| 1122 |
"1.10.22", |
| 1123 |
src = "www/shared/datatables", |
| 1124 |
package = "shiny", |
| 1125 |
script = "js/jquery.dataTables.min.js" |
| 1126 |
), |
| 1127 |
htmlDependency( |
| 1128 |
"datatables-bootstrap", |
| 1129 |
"1.10.22", |
| 1130 |
src = "www/shared/datatables", |
| 1131 |
package = "shiny", |
| 1132 |
stylesheet = "css/dataTables.bootstrap.css", |
| 1133 |
script = "js/dataTables.bootstrap.js" |
| 1134 |
) |
| 1135 |
) |
| 1136 |
|
| 1137 |
#' @rdname renderDataTable |
| 1138 |
#' @export |
| 1139 |
dataTableOutput <- function(outputId) { |
| 1140 |
legacy <- useLegacyDataTable(from = "shiny::dataTableOutput()", to = "DT::DTOutput()") |
| 1141 |
|
| 1142 |
if (legacy) { |
| 1143 |
attachDependencies( |
| 1144 |
div(id = outputId, class = "shiny-datatable-output"), |
| 1145 |
dataTableDependency |
| 1146 |
) |
| 1147 |
} else { |
| 1148 |
DT::DTOutput(outputId) |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
useLegacyDataTable <- function(from, to) { |
| 1153 |
legacy <- getOption("shiny.legacy.datatable") |
| 1154 |
|
| 1155 |
# If option has been set, user knows what they're doing |
| 1156 |
if (!is.null(legacy)) { |
| 1157 |
return(legacy) |
| 1158 |
} |
| 1159 |
|
| 1160 |
# If not set, use DT if a suitable version is available (and inform either way) |
| 1161 |
hasDT <- is_installed("DT", "0.32.1") |
| 1162 |
details <- NULL |
| 1163 |
if (hasDT) { |
| 1164 |
details <- paste0(c( |
| 1165 |
"Since you have a suitable version of DT (>= v0.32.1), ", |
| 1166 |
from, |
| 1167 |
" will automatically use ", |
| 1168 |
to, |
| 1169 |
" under-the-hood.\n", |
| 1170 |
"If this happens to break your app, set `options(shiny.legacy.datatable = TRUE)` ", |
| 1171 |
"to get the legacy datatable implementation (or `FALSE` to squelch this message).\n" |
| 1172 |
), collapse = "") |
| 1173 |
} |
| 1174 |
|
| 1175 |
details <- paste0(details, "See <https://rstudio.github.io/DT/shiny.html> for more information.") |
| 1176 |
|
| 1177 |
shinyDeprecated("1.8.1", from, to, details) |
| 1178 |
|
| 1179 |
!hasDT |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
#' Create an HTML output element |
| 1184 |
#' |
| 1185 |
#' Render a reactive output variable as HTML within an application page. The |
| 1186 |
#' text will be included within an HTML `div` tag, and is presumed to contain |
| 1187 |
#' HTML content which should not be escaped. |
| 1188 |
#' |
| 1189 |
#' `uiOutput` is intended to be used with `renderUI` on the server side. It is |
| 1190 |
#' currently just an alias for `htmlOutput`. |
| 1191 |
#' |
| 1192 |
#' @param outputId output variable to read the value from |
| 1193 |
#' @param ... Other arguments to pass to the container tag function. This is |
| 1194 |
#' useful for providing additional classes for the tag. |
| 1195 |
#' @param fill If `TRUE`, the result of `container` is treated as _both_ a fill |
| 1196 |
#' item and container (see [htmltools::bindFillRole()]), which means both the |
| 1197 |
#' `container` as well as its immediate children (i.e., the result of |
| 1198 |
#' `renderUI()`) are allowed to grow/shrink to fit a fill container with an |
| 1199 |
#' opinionated height. Set `fill = "item"` or `fill = "container"` to treat |
| 1200 |
#' `container` as just a fill item or a fill container. |
| 1201 |
#' @inheritParams textOutput |
| 1202 |
#' @return An HTML output element that can be included in a panel |
| 1203 |
#' @examples |
| 1204 |
#' htmlOutput("summary") |
| 1205 |
#' |
| 1206 |
#' # Using a custom container and class |
| 1207 |
#' tags$ul( |
| 1208 |
#' htmlOutput("summary", container = tags$li, class = "custom-li-output") |
| 1209 |
#' ) |
| 1210 |
#' @export |
| 1211 |
htmlOutput <- function(outputId, inline = FALSE, |
| 1212 |
container = if (inline) span else div, fill = FALSE, ...) |
| 1213 |
{ |
| 1214 |
if (any_unnamed(list(...))) { |
| 1215 |
warning("Unnamed elements in ... will be replaced with dynamic UI.") |
| 1216 |
} |
| 1217 |
res <- container(id = outputId, class = "shiny-html-output", ...) |
| 1218 |
bindFillRole( |
| 1219 |
res, item = isTRUE(fill) || isTRUE("item" == fill), |
| 1220 |
container = isTRUE(fill) || isTRUE("container" == fill) |
| 1221 |
) |
| 1222 |
} |
| 1223 |
|
| 1224 |
#' @rdname htmlOutput |
| 1225 |
#' @export |
| 1226 |
uiOutput <- htmlOutput |
| 1227 |
|
| 1228 |
#' Create a download button or link |
| 1229 |
#' |
| 1230 |
#' Use these functions to create a download button or link; when clicked, it |
| 1231 |
#' will initiate a browser download. The filename and contents are specified by |
| 1232 |
#' the corresponding [downloadHandler()] defined in the server |
| 1233 |
#' function. |
| 1234 |
#' |
| 1235 |
#' @param outputId The name of the output slot that the `downloadHandler` |
| 1236 |
#' is assigned to. |
| 1237 |
#' @param label The label that should appear on the button. |
| 1238 |
#' @param class Additional CSS classes to apply to the tag, if any. |
| 1239 |
#' @param icon An [icon()] to appear on the button. Default is `icon("download")`. |
| 1240 |
#' @param ... Other arguments to pass to the container tag function. |
| 1241 |
#' |
| 1242 |
#' @examples |
| 1243 |
#' \dontrun{ |
| 1244 |
#' ui <- fluidPage( |
| 1245 |
#' p("Choose a dataset to download."), |
| 1246 |
#' selectInput("dataset", "Dataset", choices = c("mtcars", "airquality")), |
| 1247 |
#' downloadButton("downloadData", "Download") |
| 1248 |
#' ) |
| 1249 |
#' |
| 1250 |
#' server <- function(input, output) { |
| 1251 |
#' # The requested dataset |
| 1252 |
#' data <- reactive({ |
| 1253 |
#' get(input$dataset) |
| 1254 |
#' }) |
| 1255 |
#' |
| 1256 |
#' output$downloadData <- downloadHandler( |
| 1257 |
#' filename = function() { |
| 1258 |
#' # Use the selected dataset as the suggested file name |
| 1259 |
#' paste0(input$dataset, ".csv") |
| 1260 |
#' }, |
| 1261 |
#' content = function(file) { |
| 1262 |
#' # Write the dataset to the `file` that will be downloaded |
| 1263 |
#' write.csv(data(), file) |
| 1264 |
#' } |
| 1265 |
#' ) |
| 1266 |
#' } |
| 1267 |
#' |
| 1268 |
#' shinyApp(ui, server) |
| 1269 |
#' } |
| 1270 |
#' |
| 1271 |
#' @aliases downloadLink |
| 1272 |
#' @seealso [downloadHandler()] |
| 1273 |
#' @export |
| 1274 |
downloadButton <- function(outputId, |
| 1275 |
label="Download", |
| 1276 |
class=NULL, |
| 1277 |
..., |
| 1278 |
icon = shiny::icon("download")) { |
| 1279 |
tags$a(id=outputId, |
| 1280 |
class='btn btn-default shiny-download-link disabled', |
| 1281 |
class=class, |
| 1282 |
href='', |
| 1283 |
target='_blank', |
| 1284 |
download=NA, |
| 1285 |
"aria-disabled"="true", |
| 1286 |
tabindex="-1", |
| 1287 |
validateIcon(icon), |
| 1288 |
label, ...) |
| 1289 |
} |
| 1290 |
|
| 1291 |
#' @rdname downloadButton |
| 1292 |
#' @export |
| 1293 |
downloadLink <- function(outputId, label="Download", class=NULL, ...) { |
| 1294 |
tags$a(id=outputId, |
| 1295 |
class='shiny-download-link disabled', |
| 1296 |
class=class, |
| 1297 |
href='', |
| 1298 |
target='_blank', |
| 1299 |
download=NA, |
| 1300 |
"aria-disabled"="true", |
| 1301 |
tabindex="-1", |
| 1302 |
label, ...) |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
#' Create an icon |
| 1307 |
#' |
| 1308 |
#' Create an icon for use within a page. Icons can appear on their own, inside |
| 1309 |
#' of a button, and/or used with [tabPanel()] and [navbarMenu()]. |
| 1310 |
#' |
| 1311 |
#' @param name The name of the icon. A name from either [Font |
| 1312 |
#' Awesome](https://fontawesome.com/) (when `lib="font-awesome"`) or |
| 1313 |
#' [Bootstrap |
| 1314 |
#' Glyphicons](https://getbootstrap.com/docs/3.3/components/#glyphicons) (when |
| 1315 |
#' `lib="glyphicon"`) may be provided. Note that the `"fa-"` and |
| 1316 |
#' `"glyphicon-"` prefixes should not appear in name (i.e., the |
| 1317 |
#' `"fa-calendar"` icon should be referred to as `"calendar"`). A `name` of |
| 1318 |
#' `NULL` may also be provided to get a raw `<i>` tag with no library attached |
| 1319 |
#' to it. |
| 1320 |
#' @param class Additional classes to customize the style of an icon (see the |
| 1321 |
#' [usage examples](https://fontawesome.com/how-to-use) for details on |
| 1322 |
#' supported styles). |
| 1323 |
#' @param lib The icon library to use. Either `"font-awesome"` or `"glyphicon"`. |
| 1324 |
#' @param ... Arguments passed to the `<i>` tag of [htmltools::tags]. |
| 1325 |
#' |
| 1326 |
#' @return An `<i>` (icon) HTML tag. |
| 1327 |
#' |
| 1328 |
#' @seealso For lists of available icons, see <https://fontawesome.com/icons> |
| 1329 |
#' and <https://getbootstrap.com/docs/3.3/components/#glyphicons> |
| 1330 |
#' |
| 1331 |
#' @examples |
| 1332 |
#' # add an icon to a submit button |
| 1333 |
#' submitButton("Update View", icon = icon("redo")) |
| 1334 |
#' |
| 1335 |
#' navbarPage("App Title", |
| 1336 |
#' tabPanel("Plot", icon = icon("bar-chart-o")), |
| 1337 |
#' tabPanel("Summary", icon = icon("list-alt")), |
| 1338 |
#' tabPanel("Table", icon = icon("table")) |
| 1339 |
#' ) |
| 1340 |
#' @export |
| 1341 |
icon <- function(name, class = NULL, lib = "font-awesome", ...) { |
| 1342 |
|
| 1343 |
# A NULL name allows for a generic <i> not tied to any library |
| 1344 |
if (is.null(name)) { |
| 1345 |
lib <- "none" |
| 1346 |
} |
| 1347 |
|
| 1348 |
switch( |
| 1349 |
lib %||% "", |
| 1350 |
"none" = iconTag(name, class = class, ...), |
| 1351 |
"font-awesome" = fontawesome::fa_i(name = name, class = class, ...), |
| 1352 |
"glyphicon" = iconTag( |
| 1353 |
name, class = "glyphicon", class = paste0("glyphicon-", name), |
| 1354 |
class = class, ... |
| 1355 |
), |
| 1356 |
stop("Unknown icon library: ", lib, ". See `?icon` for supported libraries.") |
| 1357 |
) |
| 1358 |
} |
| 1359 |
|
| 1360 |
iconTag <- function(name, ...) { |
| 1361 |
htmltools::browsable( |
| 1362 |
tags$i(..., role = "presentation", `aria-label` = paste(name, "icon")) |
| 1363 |
) |
| 1364 |
} |