From 977383de7f342a1bf7023e02df82520bd555db56 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 18 Apr 2023 16:01:18 -0400 Subject: [PATCH 1/4] Move `isRunning()` to `R/app-state.R` to put all app state code in one location --- R/app-state.R | 11 +++++++++++ R/server.R | 10 ---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/R/app-state.R b/R/app-state.R index d5a64733da..44514485c8 100644 --- a/R/app-state.R +++ b/R/app-state.R @@ -7,6 +7,17 @@ NULL .globals$appState <- NULL +#' Check whether a Shiny application is running +#' +#' This function tests whether a Shiny application is currently running. +#' +#' @return `TRUE` if a Shiny application is currently running. Otherwise, +#' `FALSE`. +#' @export +isRunning <- function() { + !is.null(getCurrentAppState()) +} + initCurrentAppState <- function(appobj) { if (!is.null(.globals$appState)) { stop("Can't initialize current app state when another is currently active.") diff --git a/R/server.R b/R/server.R index 0a8e54caec..8233fe4e84 100644 --- a/R/server.R +++ b/R/server.R @@ -495,16 +495,6 @@ serviceApp <- function() { .shinyServerMinVersion <- '0.3.4' -#' Check whether a Shiny application is running -#' -#' This function tests whether a Shiny application is currently running. -#' -#' @return `TRUE` if a Shiny application is currently running. Otherwise, -#' `FALSE`. -#' @export -isRunning <- function() { - !is.null(getCurrentAppState()) -} # Returns TRUE if we're running in Shiny Server or other hosting environment, From e418f3540a5244a59eb4a1c34c084d3e1f631b97 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 18 Apr 2023 16:03:39 -0400 Subject: [PATCH 2/4] Use new `getCurrentAppStateOptions()` helper method paired with `isRunning()` --- R/app-state.R | 3 +++ R/mock-session.R | 2 +- R/shiny-options.R | 13 ++++++------- R/shiny.R | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/R/app-state.R b/R/app-state.R index 44514485c8..0b5964f112 100644 --- a/R/app-state.R +++ b/R/app-state.R @@ -32,6 +32,9 @@ getCurrentAppState <- function() { .globals$appState } +getCurrentAppStateOptions <- function() { + .globals$appState$options +} clearCurrentAppState <- function() { .globals$appState <- NULL } diff --git a/R/mock-session.R b/R/mock-session.R index fd4227d295..9b41033ad0 100644 --- a/R/mock-session.R +++ b/R/mock-session.R @@ -274,7 +274,7 @@ MockShinySession <- R6Class( self$token <- createUniqueId(16) # Copy app-level options - self$options <- getCurrentAppState()$options + self$options <- getCurrentAppStateOptions() self$cache <- cachem::cache_mem() self$appcache <- cachem::cache_mem() diff --git a/R/shiny-options.R b/R/shiny-options.R index c04c3b1e47..c3521fd00f 100644 --- a/R/shiny-options.R +++ b/R/shiny-options.R @@ -19,10 +19,10 @@ getShinyOption <- function(name, default = NULL) { } # Check if there's a current app - app_state <- getCurrentAppState() - if (!is.null(app_state)) { - if (name %in% names(app_state$options)) { - return(app_state$options[[name]]) + if (isRunning()) { + app_state_options <- getCurrentAppStateOptions() + if (name %in% names(app_state_options)) { + return(app_state_options[[name]]) } else { return(default) } @@ -218,9 +218,8 @@ shinyOptions <- function(...) { return(session$options) } - app_state <- getCurrentAppState() - if (!is.null(app_state)) { - return(app_state$options) + if (isRunning()) { + return(getCurrentAppStateOptions()) } return(.globals$options) diff --git a/R/shiny.R b/R/shiny.R index ef9f255c77..842737842d 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -738,7 +738,7 @@ ShinySession <- R6Class( private$.outputOptions <- list() # Copy app-level options - self$options <- getCurrentAppState()$options + self$options <- getCurrentAppStateOptions() self$cache <- cachem::cache_mem(max_size = 200 * 1024^2) From fce5216000f3a8587a04387f5a240a76b6d08688 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 18 Apr 2023 16:05:15 -0400 Subject: [PATCH 3/4] Add App State helper method to set options. Use it --- R/app-state.R | 5 +++++ R/shiny-options.R | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/R/app-state.R b/R/app-state.R index 0b5964f112..8b25481b42 100644 --- a/R/app-state.R +++ b/R/app-state.R @@ -35,6 +35,11 @@ getCurrentAppState <- function() { getCurrentAppStateOptions <- function() { .globals$appState$options } +setCurrentAppStateOptions <- function(options) { + stopifnot(isRunning()) + .globals$appState$options <- options +} + clearCurrentAppState <- function() { .globals$appState <- NULL } diff --git a/R/shiny-options.R b/R/shiny-options.R index c3521fd00f..ae71f305d9 100644 --- a/R/shiny-options.R +++ b/R/shiny-options.R @@ -199,11 +199,12 @@ shinyOptions <- function(...) { # If not in a session, but we have a currently running app, modify options # at the app level. - app_state <- getCurrentAppState() - if (!is.null(app_state)) { + if (isRunning()) { # Modify app-level options - app_state$options <- dropNulls(mergeVectors(app_state$options, newOpts)) - return(invisible(app_state$options)) + setCurrentAppStateOptions( + dropNulls(mergeVectors(getCurrentAppStateOptions(), newOpts)) + ) + return(invisible(getCurrentAppStateOptions())) } # If no currently running app, modify global options and return them. From f2be21861f44157d934cf6f54bbbdc81b637e157 Mon Sep 17 00:00:00 2001 From: schloerke Date: Tue, 18 Apr 2023 20:11:51 +0000 Subject: [PATCH 4/4] `devtools::document()` (GitHub Actions) --- man/isRunning.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/isRunning.Rd b/man/isRunning.Rd index a661e167c5..b26b7ae9d4 100644 --- a/man/isRunning.Rd +++ b/man/isRunning.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/server.R +% Please edit documentation in R/app-state.R \name{isRunning} \alias{isRunning} \title{Check whether a Shiny application is running}