| 1 |
# TODO: Subapp global.R |
| 2 |
|
| 3 |
#' Create a Shiny app object |
| 4 |
#' |
| 5 |
#' These functions create Shiny app objects from either an explicit UI/server |
| 6 |
#' pair (`shinyApp`), or by passing the path of a directory that contains a |
| 7 |
#' Shiny app (`shinyAppDir`). |
| 8 |
#' |
| 9 |
#' Normally when this function is used at the R console, the Shiny app object is |
| 10 |
#' automatically passed to the `print()` function, which runs the app. If |
| 11 |
#' this is called in the middle of a function, the value will not be passed to |
| 12 |
#' `print()` and the app will not be run. To make the app run, pass the app |
| 13 |
#' object to `print()` or [runApp()]. |
| 14 |
#' |
| 15 |
#' @param ui The UI definition of the app (for example, a call to |
| 16 |
#' `fluidPage()` with nested controls). |
| 17 |
#' |
| 18 |
#' If bookmarking is enabled (see `enableBookmarking`), this must be |
| 19 |
#' a single argument function that returns the UI definition. |
| 20 |
#' @param server A function with three parameters: `input`, `output`, and |
| 21 |
#' `session`. The function is called once for each session ensuring that each |
| 22 |
#' app is independent. |
| 23 |
#' @param onStart A function that will be called before the app is actually run. |
| 24 |
#' This is only needed for `shinyAppObj`, since in the `shinyAppDir` |
| 25 |
#' case, a `global.R` file can be used for this purpose. |
| 26 |
#' @param options Named options that should be passed to the `runApp` call |
| 27 |
#' (these can be any of the following: "port", "launch.browser", "host", "quiet", |
| 28 |
#' "display.mode" and "test.mode"). You can also specify `width` and |
| 29 |
#' `height` parameters which provide a hint to the embedding environment |
| 30 |
#' about the ideal height/width for the app. |
| 31 |
#' @param uiPattern A regular expression that will be applied to each `GET` |
| 32 |
#' request to determine whether the `ui` should be used to handle the |
| 33 |
#' request. Note that the entire request path must match the regular |
| 34 |
#' expression in order for the match to be considered successful. |
| 35 |
#' @param enableBookmarking Can be one of `"url"`, `"server"`, or |
| 36 |
#' `"disable"`. The default value, `NULL`, will respect the setting from |
| 37 |
#' any previous calls to [enableBookmarking()]. See [enableBookmarking()] |
| 38 |
#' for more information on bookmarking your app. |
| 39 |
#' @return An object that represents the app. Printing the object or passing it |
| 40 |
#' to [runApp()] will run the app. |
| 41 |
#' |
| 42 |
#' @examples |
| 43 |
#' ## Only run this example in interactive R sessions |
| 44 |
#' if (interactive()) { |
| 45 |
#' options(device.ask.default = FALSE) |
| 46 |
#' |
| 47 |
#' shinyApp( |
| 48 |
#' ui = fluidPage( |
| 49 |
#' numericInput("n", "n", 1), |
| 50 |
#' plotOutput("plot") |
| 51 |
#' ), |
| 52 |
#' server = function(input, output) { |
| 53 |
#' output$plot <- renderPlot( plot(head(cars, input$n)) ) |
| 54 |
#' } |
| 55 |
#' ) |
| 56 |
#' |
| 57 |
#' shinyAppDir(system.file("examples/01_hello", package="shiny")) |
| 58 |
#' |
| 59 |
#' |
| 60 |
#' # The object can be passed to runApp() |
| 61 |
#' app <- shinyApp( |
| 62 |
#' ui = fluidPage( |
| 63 |
#' numericInput("n", "n", 1), |
| 64 |
#' plotOutput("plot") |
| 65 |
#' ), |
| 66 |
#' server = function(input, output) { |
| 67 |
#' output$plot <- renderPlot( plot(head(cars, input$n)) ) |
| 68 |
#' } |
| 69 |
#' ) |
| 70 |
#' |
| 71 |
#' runApp(app) |
| 72 |
#' } |
| 73 |
#' @export |
| 74 |
shinyApp <- function(ui, server, onStart=NULL, options=list(), |
| 75 |
uiPattern="/", enableBookmarking=NULL) { |
| 76 |
if (!is.function(server)) { |
| 77 |
stop("`server` must be a function", call. = FALSE) |
| 78 |
} |
| 79 |
|
| 80 |
# Ensure that the entire path is a match |
| 81 |
uiPattern <- sprintf("^%s$", uiPattern) |
| 82 |
|
| 83 |
httpHandler <- uiHttpHandler(ui, uiPattern) |
| 84 |
|
| 85 |
serverFuncSource <- function() { |
| 86 |
server |
| 87 |
} |
| 88 |
|
| 89 |
if (!is.null(enableBookmarking)) { |
| 90 |
bookmarkStore <- match.arg(enableBookmarking, c("url", "server", "disable")) |
| 91 |
enableBookmarking(bookmarkStore) |
| 92 |
} |
| 93 |
|
| 94 |
# Store the appDir and bookmarking-related options, so that we can read them |
| 95 |
# from within the app. |
| 96 |
appOptions <- captureAppOptions() |
| 97 |
|
| 98 |
structure( |
| 99 |
list( |
| 100 |
httpHandler = httpHandler, |
| 101 |
serverFuncSource = serverFuncSource, |
| 102 |
onStart = onStart, |
| 103 |
options = options, |
| 104 |
appOptions = appOptions |
| 105 |
), |
| 106 |
class = "shiny.appobj" |
| 107 |
) |
| 108 |
} |
| 109 |
|
| 110 |
#' @rdname shinyApp |
| 111 |
#' @param appDir Path to directory that contains a Shiny app (i.e. a server.R |
| 112 |
#' file and either ui.R or www/index.html) |
| 113 |
#' @export |
| 114 |
shinyAppDir <- function(appDir, options=list()) { |
| 115 |
if (!utils::file_test('-d', appDir)) { |
| 116 |
rlang::abort( |
| 117 |
paste0("No Shiny application exists at the path \"", appDir, "\""), |
| 118 |
class = "invalidShinyAppDir" |
| 119 |
) |
| 120 |
} |
| 121 |
|
| 122 |
# In case it's a relative path, convert to absolute (so we're not adversely |
| 123 |
# affected by future changes to the path) |
| 124 |
appDir <- normalizePath(appDir, mustWork = TRUE) |
| 125 |
|
| 126 |
if (file.exists.ci(appDir, "server.R")) { |
| 127 |
shinyAppDir_serverR(appDir, options = options) |
| 128 |
} else if (file.exists.ci(appDir, "app.R")) { |
| 129 |
shinyAppDir_appR("app.R", appDir, options = options) |
| 130 |
} else { |
| 131 |
rlang::abort( |
| 132 |
"App dir must contain either app.R or server.R.", |
| 133 |
class = "invalidShinyAppDir" |
| 134 |
) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
#' @rdname shinyApp |
| 139 |
#' @param appFile Path to a .R file containing a Shiny application |
| 140 |
#' @export |
| 141 |
shinyAppFile <- function(appFile, options=list()) { |
| 142 |
appFile <- normalizePath(appFile, mustWork = TRUE) |
| 143 |
appDir <- dirname(appFile) |
| 144 |
|
| 145 |
shinyAppDir_appR(basename(appFile), appDir, options = options) |
| 146 |
} |
| 147 |
|
| 148 |
# This reads in an app dir in the case that there's a server.R (and ui.R/www) |
| 149 |
# present, and returns a shiny.appobj. |
| 150 |
# appDir must be a normalized (absolute) path, not a relative one |
| 151 |
shinyAppDir_serverR <- function(appDir, options=list()) { |
| 152 |
# Most of the complexity here comes from needing to hot-reload if the .R files |
| 153 |
# change on disk, or are created, or are removed. |
| 154 |
|
| 155 |
# In an upcoming version of shiny, this option will go away. |
| 156 |
if (getOption("shiny.autoload.r", TRUE)) { |
| 157 |
# Create a child env which contains all the helpers and will be the shared parent |
| 158 |
# of the ui.R and server.R load. |
| 159 |
sharedEnv <- new.env(parent = globalenv()) |
| 160 |
} else { |
| 161 |
# old behavior |
| 162 |
sharedEnv <- globalenv() |
| 163 |
} |
| 164 |
|
| 165 |
# To enable hot-reloading of support files, this function is called |
| 166 |
# whenever the UI or Server func source is updated. To avoid loading |
| 167 |
# support files 2x, we follow the last cache update trigger timestamp. |
| 168 |
autoload_r_support_if_needed <- local({ |
| 169 |
autoload_last_loaded <- -1 |
| 170 |
function() { |
| 171 |
if (!isTRUE(getOption("shiny.autoload.r", TRUE))) return() |
| 172 |
|
| 173 |
last_cache_trigger <- cachedAutoReloadLastChanged$get() |
| 174 |
if (identical(autoload_last_loaded, last_cache_trigger)) return() |
| 175 |
|
| 176 |
loadSupport(appDir, renv = sharedEnv, globalrenv = globalenv()) |
| 177 |
|
| 178 |
autoload_last_loaded <<- last_cache_trigger |
| 179 |
} |
| 180 |
}) |
| 181 |
|
| 182 |
# uiHandlerSource is a function that returns an HTTP handler for serving up |
| 183 |
# ui.R as a webpage. The "cachedFuncWithFile" call makes sure that the closure |
| 184 |
# we're creating here only gets executed when ui.R's contents change. |
| 185 |
uiHandlerSource <- cachedFuncWithFile(appDir, "ui.R", case.sensitive = FALSE, |
| 186 |
function(uiR) { |
| 187 |
autoload_r_support_if_needed() |
| 188 |
if (file.exists(uiR)) { |
| 189 |
# If ui.R contains a call to shinyUI (which sets .globals$ui), use that. |
| 190 |
# If not, then take the last expression that's returned from ui.R. |
| 191 |
.globals$ui <- NULL |
| 192 |
on.exit(.globals$ui <- NULL, add = FALSE) |
| 193 |
ui <- sourceUTF8(uiR, envir = new.env(parent = sharedEnv)) |
| 194 |
if (!is.null(.globals$ui)) { |
| 195 |
ui <- .globals$ui[[1]] |
| 196 |
} |
| 197 |
return(uiHttpHandler(ui)) |
| 198 |
} else { |
| 199 |
return(function(req) NULL) |
| 200 |
} |
| 201 |
} |
| 202 |
) |
| 203 |
uiHandler <- function(req) { |
| 204 |
uiHandlerSource()(req) |
| 205 |
} |
| 206 |
|
| 207 |
wwwDir <- file.path.ci(appDir, "www") |
| 208 |
if (dirExists(wwwDir)) { |
| 209 |
staticPaths <- list("/" = staticPath(wwwDir, indexhtml = FALSE, fallthrough = TRUE)) |
| 210 |
} else { |
| 211 |
staticPaths <- list() |
| 212 |
} |
| 213 |
|
| 214 |
fallbackWWWDir <- system_file("www-dir", package = "shiny") |
| 215 |
|
| 216 |
serverSource <- cachedFuncWithFile(appDir, "server.R", case.sensitive = FALSE, |
| 217 |
function(serverR) { |
| 218 |
autoload_r_support_if_needed() |
| 219 |
# If server.R contains a call to shinyServer (which sets .globals$server), |
| 220 |
# use that. If not, then take the last expression that's returned from |
| 221 |
# server.R. |
| 222 |
.globals$server <- NULL |
| 223 |
on.exit(.globals$server <- NULL, add = TRUE) |
| 224 |
result <- sourceUTF8(serverR, envir = new.env(parent = sharedEnv)) |
| 225 |
if (!is.null(.globals$server)) { |
| 226 |
result <- .globals$server[[1]] |
| 227 |
} |
| 228 |
return(result) |
| 229 |
} |
| 230 |
) |
| 231 |
|
| 232 |
# This function stands in for the server function, and reloads the |
| 233 |
# real server function as necessary whenever server.R changes |
| 234 |
serverFuncSource <- function() { |
| 235 |
serverFunction <- serverSource() |
| 236 |
if (is.null(serverFunction)) { |
| 237 |
return(function(input, output) NULL) |
| 238 |
} else if (is.function(serverFunction)) { |
| 239 |
# This is what we normally expect; run the server function |
| 240 |
return(serverFunction) |
| 241 |
} else { |
| 242 |
stop("server.R returned an object of unexpected type: ", |
| 243 |
typeof(serverFunction)) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
shinyOptions(appDir = appDir) |
| 248 |
|
| 249 |
oldwd <- NULL |
| 250 |
monitorHandle <- NULL |
| 251 |
onStart <- function() { |
| 252 |
oldwd <<- getwd() |
| 253 |
setwd(appDir) |
| 254 |
if (getOption("shiny.autoload.r", TRUE)) { |
| 255 |
autoload_r_support_if_needed() |
| 256 |
} else { |
| 257 |
if (file.exists(file.path.ci(appDir, "global.R"))) |
| 258 |
sourceUTF8(file.path.ci(appDir, "global.R")) |
| 259 |
} |
| 260 |
monitorHandle <<- initAutoReloadMonitor(appDir) |
| 261 |
} |
| 262 |
onStop <- function() { |
| 263 |
setwd(oldwd) |
| 264 |
# It is possible that while calling appObj()$onStart() or loadingSupport, an error occured |
| 265 |
# This will cause `onStop` to be called. |
| 266 |
# The `oldwd` will exist, but `monitorHandle` is not a function yet. |
| 267 |
if (is.function(monitorHandle)) { |
| 268 |
monitorHandle() |
| 269 |
monitorHandle <<- NULL |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
structure( |
| 274 |
list( |
| 275 |
staticPaths = staticPaths, |
| 276 |
# Even though the wwwDir is handled as a static path, we need to include |
| 277 |
# it here to be handled by R as well. This is because the special case |
| 278 |
# of index.html: it is specifically not handled as a staticPath for |
| 279 |
# reasons explained above, but if someone does want to serve up an |
| 280 |
# index.html, we need to handle it, and we do it by using the |
| 281 |
# staticHandler in the R code path. (#2380) |
| 282 |
httpHandler = joinHandlers(c(uiHandler, wwwDir, fallbackWWWDir)), |
| 283 |
serverFuncSource = serverFuncSource, |
| 284 |
onStart = onStart, |
| 285 |
onStop = onStop, |
| 286 |
options = options |
| 287 |
), |
| 288 |
class = "shiny.appobj" |
| 289 |
) |
| 290 |
} |
| 291 |
|
| 292 |
# Start a reactive observer that continually monitors dir for changes to files |
| 293 |
# that have the extensions: r, htm, html, js, css, png, jpg, jpeg, gif. Case is |
| 294 |
# ignored when checking extensions. If any changes are detected, all connected |
| 295 |
# Shiny sessions are reloaded. |
| 296 |
# |
| 297 |
# Use options(shiny.autoreload = TRUE) to enable this behavior. Since monitoring |
| 298 |
# for changes is expensive (we are polling for mtimes here, nothing fancy) this |
| 299 |
# feature is intended only for development. |
| 300 |
# |
| 301 |
# You can customize the file patterns Shiny will monitor by setting the |
| 302 |
# shiny.autoreload.pattern option. For example, to monitor only ui.R: |
| 303 |
# options(shiny.autoreload.pattern = glob2rx("ui.R")) |
| 304 |
# |
| 305 |
# The return value is a function that halts monitoring when called. |
| 306 |
initAutoReloadMonitor <- function(dir) { |
| 307 |
if (!get_devmode_option("shiny.autoreload", FALSE)) { |
| 308 |
return(function(){}) |
| 309 |
} |
| 310 |
|
| 311 |
filePattern <- getOption( |
| 312 |
"shiny.autoreload.pattern", |
| 313 |
".*\\.(r|html?|js|css|png|jpe?g|gif)$" |
| 314 |
) |
| 315 |
|
| 316 |
|
| 317 |
if (is_installed("watcher")) { |
| 318 |
check_for_update <- function(paths) { |
| 319 |
paths <- grep( |
| 320 |
filePattern, |
| 321 |
paths, |
| 322 |
ignore.case = TRUE, |
| 323 |
value = TRUE |
| 324 |
) |
| 325 |
|
| 326 |
if (length(paths) == 0) { |
| 327 |
return() |
| 328 |
} |
| 329 |
|
| 330 |
cachedAutoReloadLastChanged$set() |
| 331 |
autoReloadCallbacks$invoke() |
| 332 |
} |
| 333 |
|
| 334 |
# [garrick, 2025-02-20] Shiny <= v1.10.0 used `invalidateLater()` with an |
| 335 |
# autoreload.interval in ms. {watcher} instead uses a latency parameter in |
| 336 |
# seconds, which serves a similar purpose and that I'm keeping for backcompat. |
| 337 |
latency <- getOption("shiny.autoreload.interval", 250) / 1000 |
| 338 |
watcher <- watcher::watcher(dir, check_for_update, latency = latency) |
| 339 |
watcher$start() |
| 340 |
onStop(watcher$stop) |
| 341 |
} else { |
| 342 |
# Fall back to legacy observer behavior |
| 343 |
if (!is_false(getOption("shiny.autoreload.legacy_warning", TRUE))) { |
| 344 |
cli::cli_warn( |
| 345 |
c( |
| 346 |
"Using legacy autoreload file watching. Please install {.pkg watcher} for a more performant autoreload file watcher.", |
| 347 |
"i" = "Set {.run options(shiny.autoreload.legacy_warning = FALSE)} to suppress this warning." |
| 348 |
), |
| 349 |
.frequency = "regularly", |
| 350 |
.frequency_id = "shiny.autoreload.legacy_warning" |
| 351 |
) |
| 352 |
} |
| 353 |
|
| 354 |
lastValue <- NULL |
| 355 |
observeLabel <- paste0("File Auto-Reload - '", basename(dir), "'") |
| 356 |
watcher <- observe(label = observeLabel, { |
| 357 |
files <- sort_c( |
| 358 |
list.files(dir, pattern = filePattern, recursive = TRUE, ignore.case = TRUE) |
| 359 |
) |
| 360 |
times <- file.info(files)$mtime |
| 361 |
names(times) <- files |
| 362 |
|
| 363 |
if (is.null(lastValue)) { |
| 364 |
# First run |
| 365 |
lastValue <<- times |
| 366 |
} else if (!identical(lastValue, times)) { |
| 367 |
# We've changed! |
| 368 |
lastValue <<- times |
| 369 |
cachedAutoReloadLastChanged$set() |
| 370 |
autoReloadCallbacks$invoke() |
| 371 |
} |
| 372 |
|
| 373 |
invalidateLater(getOption("shiny.autoreload.interval", 500)) |
| 374 |
}) |
| 375 |
|
| 376 |
onStop(watcher$destroy) |
| 377 |
|
| 378 |
watcher$destroy |
| 379 |
} |
| 380 |
|
| 381 |
invisible(watcher) |
| 382 |
} |
| 383 |
|
| 384 |
#' Load an app's supporting R files |
| 385 |
#' |
| 386 |
#' Loads all of the supporting R files of a Shiny application. Specifically, |
| 387 |
#' this function loads any top-level supporting `.R` files in the `R/` directory |
| 388 |
#' adjacent to the `app.R`/`server.R`/`ui.R` files. |
| 389 |
#' |
| 390 |
#' Since Shiny 1.5.0, this function is called by default when running an |
| 391 |
#' application. If it causes problems, there are two ways to opt out. You can |
| 392 |
#' either place a file named `_disable_autoload.R` in your R/ directory, or |
| 393 |
#' set `options(shiny.autoload.r=FALSE)`. If you set this option, it will |
| 394 |
#' affect any application that runs later in the same R session, potentially |
| 395 |
#' breaking it, so after running your application, you should unset option with |
| 396 |
#' `options(shiny.autoload.r=NULL)` |
| 397 |
#' |
| 398 |
#' @details The files are sourced in alphabetical order (as determined by |
| 399 |
#' [list.files]). `global.R` is evaluated before the supporting R files in the |
| 400 |
#' `R/` directory. |
| 401 |
#' @param appDir The application directory. If `appDir` is `NULL` or |
| 402 |
#' not supplied, the nearest enclosing directory that is a Shiny app, starting |
| 403 |
#' with the current directory, is used. |
| 404 |
#' @param renv The environment in which the files in the `R/` directory should |
| 405 |
#' be evaluated. |
| 406 |
#' @param globalrenv The environment in which `global.R` should be evaluated. If |
| 407 |
#' `NULL`, `global.R` will not be evaluated at all. |
| 408 |
#' @export |
| 409 |
loadSupport <- function(appDir=NULL, renv=new.env(parent=globalenv()), globalrenv=globalenv()){ |
| 410 |
require(shiny) |
| 411 |
|
| 412 |
if (is.null(appDir)) { |
| 413 |
appDir <- findEnclosingApp(".") |
| 414 |
} |
| 415 |
|
| 416 |
if (!is.null(globalrenv)){ |
| 417 |
# Evaluate global.R, if it exists. |
| 418 |
globalPath <- file.path.ci(appDir, "global.R") |
| 419 |
if (file.exists(globalPath)){ |
| 420 |
withr::with_dir(appDir, { |
| 421 |
sourceUTF8(basename(globalPath), envir=globalrenv) |
| 422 |
}) |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
helpersDir <- file.path(appDir, "R") |
| 428 |
|
| 429 |
disabled <- list.files(helpersDir, pattern="^_disable_autoload\\.r$", recursive=FALSE, ignore.case=TRUE) |
| 430 |
if (length(disabled) > 0) { |
| 431 |
return(invisible(renv)) |
| 432 |
} |
| 433 |
|
| 434 |
warn_if_app_dir_is_package(appDir) |
| 435 |
|
| 436 |
helpers <- list.files(helpersDir, pattern="\\.[rR]$", recursive=FALSE, full.names=TRUE) |
| 437 |
# Ensure files in R/ are sorted according to the 'C' locale before sourcing. |
| 438 |
# This convention is based on the default for packages. For details, see: |
| 439 |
# https://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-DESCRIPTION-file |
| 440 |
helpers <- sort_c(helpers) |
| 441 |
helpers <- normalizePath(helpers) |
| 442 |
|
| 443 |
withr::with_dir(appDir, { |
| 444 |
lapply(helpers, sourceUTF8, envir=renv) |
| 445 |
}) |
| 446 |
|
| 447 |
invisible(renv) |
| 448 |
} |
| 449 |
|
| 450 |
warn_if_app_dir_is_package <- function(appDir) { |
| 451 |
has_namespace <- file.exists(file.path.ci(appDir, "NAMESPACE")) |
| 452 |
has_desc_pkg <- FALSE |
| 453 |
|
| 454 |
if (!has_namespace) { |
| 455 |
descFile <- file.path.ci(appDir, "DESCRIPTION") |
| 456 |
|
| 457 |
has_desc_pkg <- |
| 458 |
file.exists(descFile) && |
| 459 |
identical(as.character(read.dcf(descFile, fields = "Type")), "Package") |
| 460 |
} |
| 461 |
|
| 462 |
if (has_namespace || has_desc_pkg) { |
| 463 |
warning( |
| 464 |
"Loading R/ subdirectory for Shiny application, but this directory appears ", |
| 465 |
"to contain an R package. Sourcing files in R/ may cause unexpected behavior. ", |
| 466 |
"See `?loadSupport` for more details." |
| 467 |
) |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
# This reads in an app dir for a single-file application (e.g. app.R), and |
| 472 |
# returns a shiny.appobj. |
| 473 |
# appDir must be a normalized (absolute) path, not a relative one |
| 474 |
shinyAppDir_appR <- function(fileName, appDir, options=list()) |
| 475 |
{ |
| 476 |
fullpath <- file.path.ci(appDir, fileName) |
| 477 |
|
| 478 |
# This sources app.R and caches the content. When appObj() is called but |
| 479 |
# app.R hasn't changed, it won't re-source the file. But if called and |
| 480 |
# app.R has changed, it'll re-source the file and return the result. |
| 481 |
appObj <- cachedFuncWithFile(appDir, fileName, case.sensitive = FALSE, |
| 482 |
function(appR) { |
| 483 |
wasDir <- setwd(appDir) |
| 484 |
on.exit(setwd(wasDir)) |
| 485 |
|
| 486 |
if (getOption("shiny.autoload.r", TRUE)) { |
| 487 |
# Create a child env which contains all the helpers and will be the shared parent |
| 488 |
# of the ui.R and server.R load. |
| 489 |
sharedEnv <- new.env(parent = globalenv()) |
| 490 |
loadSupport(appDir, renv=sharedEnv, globalrenv=NULL) |
| 491 |
} else { |
| 492 |
sharedEnv <- globalenv() |
| 493 |
} |
| 494 |
result <- sourceUTF8(fullpath, envir = new.env(parent = sharedEnv)) |
| 495 |
|
| 496 |
if (!is.shiny.appobj(result)) |
| 497 |
stop("app.R did not return a shiny.appobj object.") |
| 498 |
|
| 499 |
applyCapturedAppOptions(result$appOptions) |
| 500 |
|
| 501 |
return(result) |
| 502 |
} |
| 503 |
) |
| 504 |
|
| 505 |
# A function that invokes the http handler from the appObj in app.R, but |
| 506 |
# since this uses appObj(), it only re-sources the file when it changes. |
| 507 |
dynHttpHandler <- function(...) { |
| 508 |
appObj()$httpHandler(...) |
| 509 |
} |
| 510 |
|
| 511 |
dynServerFuncSource <- function(...) { |
| 512 |
appObj()$serverFuncSource(...) |
| 513 |
} |
| 514 |
|
| 515 |
wwwDir <- file.path.ci(appDir, "www") |
| 516 |
if (dirExists(wwwDir)) { |
| 517 |
# wwwDir is a static path served by httpuv. It does _not_ serve up |
| 518 |
# index.html, for two reasons. (1) It's possible that the user's |
| 519 |
# www/index.html file is not actually used as the index, but as a template |
| 520 |
# that gets processed before being sent; and (2) the index content may be |
| 521 |
# modified by the hosting environment (as in SockJSAdapter.R). |
| 522 |
# |
| 523 |
# The call to staticPath normalizes the path, so that if the working dir |
| 524 |
# later changes, it will continue to point to the right place. |
| 525 |
staticPaths <- list("/" = staticPath(wwwDir, indexhtml = FALSE, fallthrough = TRUE)) |
| 526 |
} else { |
| 527 |
staticPaths <- list() |
| 528 |
} |
| 529 |
|
| 530 |
fallbackWWWDir <- system_file("www-dir", package = "shiny") |
| 531 |
|
| 532 |
oldwd <- NULL |
| 533 |
monitorHandle <- NULL |
| 534 |
onStart <- function() { |
| 535 |
oldwd <<- getwd() |
| 536 |
setwd(appDir) |
| 537 |
if (!is.null(appObj()$onStart)) appObj()$onStart() |
| 538 |
monitorHandle <<- initAutoReloadMonitor(appDir) |
| 539 |
invisible() |
| 540 |
} |
| 541 |
onStop <- function() { |
| 542 |
setwd(oldwd) |
| 543 |
# It is possible that while calling appObj()$onStart() or loadingSupport, an error occured |
| 544 |
# This will cause `onStop` to be called. |
| 545 |
# The `oldwd` will exist, but `monitorHandle` is not a function yet. |
| 546 |
if (is.function(monitorHandle)) { |
| 547 |
monitorHandle() |
| 548 |
monitorHandle <<- NULL |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
appObjOptions <- appObj()$options |
| 553 |
|
| 554 |
structure( |
| 555 |
list( |
| 556 |
# fallbackWWWDir is _not_ listed in staticPaths, because it needs to |
| 557 |
# come after the uiHandler. It also does not need to be fast, since it |
| 558 |
# should rarely be hit. The order is wwwDir (in staticPaths), then |
| 559 |
# uiHandler, then falbackWWWDir (which is served up by the R |
| 560 |
# staticHandler function). |
| 561 |
staticPaths = staticPaths, |
| 562 |
# Even though the wwwDir is handled as a static path, we need to include |
| 563 |
# it here to be handled by R as well. This is because the special case |
| 564 |
# of index.html: it is specifically not handled as a staticPath for |
| 565 |
# reasons explained above, but if someone does want to serve up an |
| 566 |
# index.html, we need to handle it, and we do it by using the |
| 567 |
# staticHandler in the R code path. (#2380) |
| 568 |
httpHandler = joinHandlers(c(dynHttpHandler, wwwDir, fallbackWWWDir)), |
| 569 |
serverFuncSource = dynServerFuncSource, |
| 570 |
onStart = onStart, |
| 571 |
onStop = onStop, |
| 572 |
options = joinOptions(appObjOptions, options) |
| 573 |
), |
| 574 |
class = "shiny.appobj" |
| 575 |
) |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
#' Shiny App object |
| 580 |
#' |
| 581 |
#' Internal methods for the `shiny.appobj` S3 class. |
| 582 |
#' |
| 583 |
#' @keywords internal |
| 584 |
#' @name shiny.appobj |
| 585 |
NULL |
| 586 |
|
| 587 |
#' @rdname shiny.appobj |
| 588 |
#' @param x Object to convert to a Shiny app. |
| 589 |
#' @export |
| 590 |
as.shiny.appobj <- function(x) { |
| 591 |
UseMethod("as.shiny.appobj", x) |
| 592 |
} |
| 593 |
|
| 594 |
#' @rdname shiny.appobj |
| 595 |
#' @export |
| 596 |
as.shiny.appobj.shiny.appobj <- function(x) { |
| 597 |
x |
| 598 |
} |
| 599 |
|
| 600 |
#' @rdname shiny.appobj |
| 601 |
#' @export |
| 602 |
as.shiny.appobj.list <- function(x) { |
| 603 |
shinyApp(ui = x$ui, server = x$server) |
| 604 |
} |
| 605 |
|
| 606 |
#' @rdname shiny.appobj |
| 607 |
#' @export |
| 608 |
as.shiny.appobj.character <- function(x) { |
| 609 |
if (identical(tolower(tools::file_ext(x)), "r")) |
| 610 |
shinyAppFile(x) |
| 611 |
else |
| 612 |
shinyAppDir(x) |
| 613 |
} |
| 614 |
|
| 615 |
#' @rdname shiny.appobj |
| 616 |
#' @export |
| 617 |
is.shiny.appobj <- function(x) { |
| 618 |
inherits(x, "shiny.appobj") |
| 619 |
} |
| 620 |
|
| 621 |
#' @rdname shiny.appobj |
| 622 |
#' @param ... Ignored. |
| 623 |
#' @export |
| 624 |
print.shiny.appobj <- function(x, ...) { |
| 625 |
runApp(x) |
| 626 |
} |
| 627 |
|
| 628 |
# Joins two options objects (i.e. the `options` argument to shinyApp(), |
| 629 |
# shinyAppDir(), etc.). The values in `b` should take precedence over the values |
| 630 |
# in `a`. Given the current options available, it is safe to throw away any |
| 631 |
# values in `a` that are provided in `b`. But in the future, if new options are |
| 632 |
# introduced that need to be combined in some way instead of simply overwritten, |
| 633 |
# then this will be the place to do it. See the implementations of |
| 634 |
# print.shiny.appobj() and runApp() (for the latter, look specifically for |
| 635 |
# "findVal()") to determine the set of possible options. |
| 636 |
joinOptions <- function(a, b) { |
| 637 |
stopifnot(is.null(a) || is.list(a)) |
| 638 |
stopifnot(is.null(b) || is.list(b)) |
| 639 |
|
| 640 |
mergeVectors(a, b) |
| 641 |
} |
| 642 |
|
| 643 |
#' @rdname shiny.appobj |
| 644 |
#' @method as.tags shiny.appobj |
| 645 |
#' @export |
| 646 |
as.tags.shiny.appobj <- function(x, ...) { |
| 647 |
# jcheng 06/06/2014: Unfortunate copy/paste between this function and |
| 648 |
# knit_print.shiny.appobj, but I am trying to make the most conservative |
| 649 |
# change possible due to upcoming release. |
| 650 |
opts <- x$options %||% list() |
| 651 |
width <- if (is.null(opts$width)) "100%" else opts$width |
| 652 |
height <- if (is.null(opts$height)) "400" else opts$height |
| 653 |
|
| 654 |
path <- addSubApp(x) |
| 655 |
deferredIFrame(path, width, height) |
| 656 |
} |
| 657 |
|
| 658 |
# Generate subapp iframes in such a way that they will not actually load right |
| 659 |
# away. Loading subapps immediately upon app load can result in a storm of |
| 660 |
# connections, all of which are contending for the few concurrent connections |
| 661 |
# that a browser will make to a specific origin. Instead, we load dummy iframes |
| 662 |
# and let the client load them when convenient. (See the initIframes function in |
| 663 |
# init_shiny.js.) |
| 664 |
deferredIFrame <- function(path, width, height) { |
| 665 |
tags$iframe("data-deferred-src" = path, |
| 666 |
width = width, height = height, |
| 667 |
class = "shiny-frame shiny-frame-deferred" |
| 668 |
) |
| 669 |
} |