From 3f1985a9dcb62b93b5f2dfd755a8bcfe12c1204d Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 25 Jul 2016 22:10:38 -0500 Subject: [PATCH] Allow modules to exclude their inputs --- NAMESPACE | 3 ++- R/bookmark-state.R | 25 +++++++++++-------- R/shiny.R | 24 +++++++++++++++--- ...ureBookmarking.Rd => enableBookmarking.Rd} | 13 ++++------ man/setBookmarkExclude.Rd | 17 +++++++++++++ 5 files changed, 59 insertions(+), 23 deletions(-) rename man/{configureBookmarking.Rd => enableBookmarking.Rd} (51%) create mode 100644 man/setBookmarkExclude.Rd diff --git a/NAMESPACE b/NAMESPACE index 4462dc5be..505cc12bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -56,7 +56,6 @@ export(code) export(column) export(conditionStackTrace) export(conditionalPanel) -export(configureBookmarking) export(createWebDependency) export(dataTableOutput) export(dateInput) @@ -68,6 +67,7 @@ export(downloadButton) export(downloadHandler) export(downloadLink) export(em) +export(enableBookmarking) export(eventReactive) export(exprToFunction) export(extractStackTrace) @@ -192,6 +192,7 @@ export(safeError) export(selectInput) export(selectizeInput) export(serverInfo) +export(setBookmarkExclude) export(setProgress) export(shinyApp) export(shinyAppDir) diff --git a/R/bookmark-state.R b/R/bookmark-state.R index 67deb5c42..906ec819f 100644 --- a/R/bookmark-state.R +++ b/R/bookmark-state.R @@ -500,19 +500,25 @@ urlModal <- function(url, title = "Bookmarked application link", subtitle = NULL ) } -#' Configure bookmarking for a Shiny application +#' Enable bookmarking for a Shiny application #' #' @param store Either \code{"url"}, which encodes all of the relevant values in #' a URL, \code{"server"}, which saves to disk on the server, or #' \code{"disable"}, which disables any previously-enabled bookmarking. -#' @param exclude A character vector of names of input values to exclude from +#' @export +enableBookmarking <- function(store = c("url", "server", "disable")) { + store <- match.arg(store) + shinyOptions(bookmarkStore = store) +} + + +#' Exclude inputs from bookmarking +#' +#' @param names A character vector containing names of inputs to exclude from #' bookmarking. #' @export -configureBookmarking <- function(store = c("url", "server", "disable"), - exclude = NULL) -{ - store <- match.arg(store) - shinyOptions(bookmarkStore = store, bookmarkExclude = exclude) +setBookmarkExclude <- function(names = character(0), session = getDefaultReactiveDomain()) { + session$setBookmarkExclude(names) } @@ -565,11 +571,10 @@ onRestored <- function(fun, session = getDefaultReactiveDomain()) { consumeBookmarkOptions <- function() { # Get options from configureBookmarking options <- list( - bookmarkStore = getShinyOption("bookmarkStore"), - bookmarkExclude = getShinyOption("bookmarkExclude") + bookmarkStore = getShinyOption("bookmarkStore") ) - shinyOptions(bookmarkStore = NULL, bookmarkExclude = NULL) + shinyOptions(bookmarkStore = NULL) options } diff --git a/R/shiny.R b/R/shiny.R index c80d7b195..ea808959b 100644 --- a/R/shiny.R +++ b/R/shiny.R @@ -359,6 +359,7 @@ ShinySession <- R6Class( bookmarkedCallbacks = 'Callbacks', restoreCallbacks = 'Callbacks', restoredCallbacks = 'Callbacks', + bookmarkExclude = character(0), # Names of inputs to exclude from bookmarking sendResponse = function(requestMsg, value) { if (is.null(requestMsg$tag)) { @@ -435,7 +436,6 @@ ShinySession <- R6Class( # Get bookmarking config appConfig <- getShinyOption("appConfig") store <- appConfig$bookmarkStore %OR% "disable" - exclude <- appConfig$bookmarkExclude if (store == "disable") return() @@ -453,7 +453,7 @@ ShinySession <- R6Class( withLogErrors({ saveState <- ShinySaveState$new( input = session$input, - exclude = exclude, + exclude = session$getBookmarkExclude(), onSave = function(state) { private$bookmarkCallbacks$invoke(state) } @@ -643,6 +643,11 @@ ShinySession <- R6Class( makeScope = function(namespace) { ns <- NS(namespace) + # Names of inputs to exclude from bookmarking. Can't be part of the scope + # object because `$<-.session_proxy` doesn't allow assignment on overidden + # names. + bookmarkExclude = character(0) + scope <- createSessionProxy(self, input = .createReactiveValues(private$.input, readonly = TRUE, ns = ns), output = .createOutputWriter(self, ns = ns), @@ -657,10 +662,15 @@ ShinySession <- R6Class( self$makeScope(ns(namespace)) }, + setBookmarkExclude = function(names) { + bookmarkExclude <<- names + }, + getBookmarkExclude = function() { + bookmarkExclude + }, onBookmark = function(fun) { self$onBookmark(function(state) { - # TODO: Update `exclude` to have per-session values. - scopeState <- ShinySaveState$new(scope$input) + scopeState <- ShinySaveState$new(scope$input, scope$getBookmarkExclude()) # Create subdir for this scope if (!is.null(state$dir)) { @@ -1033,6 +1043,12 @@ ShinySession <- R6Class( } }, + setBookmarkExclude = function(names) { + private$bookmarkExclude <- names + }, + getBookmarkExclude = function() { + private$bookmarkExclude + }, onBookmark = function(fun) { if (!is.function(fun) || length(fun) != 1) { stop("`fun` must be a function that takes one argument") diff --git a/man/configureBookmarking.Rd b/man/enableBookmarking.Rd similarity index 51% rename from man/configureBookmarking.Rd rename to man/enableBookmarking.Rd index 0f2001644..8ffa0bb44 100644 --- a/man/configureBookmarking.Rd +++ b/man/enableBookmarking.Rd @@ -1,20 +1,17 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/bookmark-state.R -\name{configureBookmarking} -\alias{configureBookmarking} -\title{Configure bookmarking for a Shiny application} +\name{enableBookmarking} +\alias{enableBookmarking} +\title{Enable bookmarking for a Shiny application} \usage{ -configureBookmarking(store = c("url", "server", "disable"), exclude = NULL) +enableBookmarking(store = c("url", "server", "disable")) } \arguments{ \item{store}{Either \code{"url"}, which encodes all of the relevant values in a URL, \code{"server"}, which saves to disk on the server, or \code{"disable"}, which disables any previously-enabled bookmarking.} - -\item{exclude}{A character vector of names of input values to exclude from -bookmarking.} } \description{ -Configure bookmarking for a Shiny application +Enable bookmarking for a Shiny application } diff --git a/man/setBookmarkExclude.Rd b/man/setBookmarkExclude.Rd new file mode 100644 index 000000000..3a99ab5af --- /dev/null +++ b/man/setBookmarkExclude.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bookmark-state.R +\name{setBookmarkExclude} +\alias{setBookmarkExclude} +\title{Exclude inputs from bookmarking} +\usage{ +setBookmarkExclude(names = character(0), + session = getDefaultReactiveDomain()) +} +\arguments{ +\item{names}{A character vector containing names of inputs to exclude from +bookmarking.} +} +\description{ +Exclude inputs from bookmarking +} +