Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set app state options when set #3811

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions R/app-state.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion R/mock-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 0 additions & 10 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions R/shiny-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion man/isRunning.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.