Compare commits

...

4 Commits

Author SHA1 Message Date
schloerke
f2be21861f devtools::document() (GitHub Actions) 2023-04-18 20:11:51 +00:00
Barret Schloerke
fce5216000 Add App State helper method to set options. Use it 2023-04-18 16:05:45 -04:00
Barret Schloerke
e418f3540a Use new getCurrentAppStateOptions() helper method paired with isRunning() 2023-04-18 16:05:45 -04:00
Barret Schloerke
977383de7f Move isRunning() to R/app-state.R to put all app state code in one location 2023-04-18 16:01:18 -04:00
6 changed files with 33 additions and 24 deletions

View File

@@ -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.")
@@ -21,6 +32,14 @@ getCurrentAppState <- function() {
.globals$appState
}
getCurrentAppStateOptions <- function() {
.globals$appState$options
}
setCurrentAppStateOptions <- function(options) {
stopifnot(isRunning())
.globals$appState$options <- options
}
clearCurrentAppState <- function() {
.globals$appState <- NULL
}

View File

@@ -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()

View File

@@ -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,

View File

@@ -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)
}
@@ -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.
@@ -218,9 +219,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)

View File

@@ -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)

View File

@@ -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}