Skip to content

Commit

Permalink
Make names consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jun 2, 2016
1 parent d52943d commit 6c52c26
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ Collate:
'reactives.R'
'render-plot.R'
'render-table.R'
'restore.R'
'run-url.R'
'save-restore-local.R'
'save-state-local.R'
'save-state.R'
'serializers.R'
'server-input-handlers.R'
'server.R'
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@ export(dataTableOutput)
export(dateInput)
export(dateRangeInput)
export(dblclickOpts)
export(decodeBookmarkDataURL)
export(decodeStateURL)
export(dialogViewer)
export(div)
export(downloadButton)
export(downloadHandler)
export(downloadLink)
export(em)
export(encodeStateURL)
export(eventReactive)
export(exprToFunction)
export(extractStackTrace)
Expand Down Expand Up @@ -183,6 +184,7 @@ export(runGist)
export(runGitHub)
export(runUrl)
export(safeError)
export(saveStateURL)
export(selectInput)
export(selectizeInput)
export(serverInfo)
Expand Down
4 changes: 2 additions & 2 deletions R/save-restore-local.R → R/save-state-local.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# a directory. It must take one argument, \code{stateDir}, which is a
# directory to which it writes/reads.

saveStateLocal <- function(id, callback) {
saveInterfaceLocal <- function(id, callback) {
# Try to save in app directory, or, if that's not available, in the current
# directory.
appDir <- getShinyOption("appDir", default = getwd())
Expand All @@ -19,7 +19,7 @@ saveStateLocal <- function(id, callback) {
callback(stateDir)
}

restoreStateLocal <- function(id, callback) {
restoreInterfaceLocal <- function(id, callback) {
# Try to save in app directory, or, if that's not available, in the current
# directory.
appDir <- getShinyOption("appDir", default = getwd())
Expand Down
120 changes: 75 additions & 45 deletions R/restore.R → R/save-state.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#' @export
decodeBookmarkDataURL <- function(url) {
values <- parseQueryString(url, nested = TRUE)

mapply(names(values), values, SIMPLIFY = FALSE,
FUN = function(name, value) {
tryCatch(
jsonlite::fromJSON(value),
error = function(e) {
stop("Failed to parse URL parameter \"", name, "\"")
}
)
}
)
}

#' @param input The session's input object.
#' @param exclude A character vector of input names that should not be
#' bookmarked.
Expand All @@ -23,37 +7,87 @@ decodeBookmarkDataURL <- function(url) {
#' @export
createBookmark <- function(input, exclude = NULL, persist = FALSE) {
if (persist) {
id <- createUniqueId(8)
saveStateURL(input, exclude)
} else {
encodeStateURL(input, exclude)
}
}

saveState <- getShinyOption("saveState", default = saveStateLocal)

saveState(id, function(stateDir) {
# Serialize values, possibly saving some extra data to stateDir
values <- serializeReactiveValues(stateDir, input, exclude)
#' @export
saveStateURL <- function(input, exclude) {
id <- createUniqueId(8)

stateFile <- file.path(stateDir, "state.rds")
saveRDS(values, stateFile)
})
saveInterface <- getShinyOption("save.interface", default = saveInterfaceLocal)

paste0("_state_id=", encodeURIComponent(id))
saveInterface(id, function(stateDir) {
# Serialize values, possibly saving some extra data to stateDir
values <- serializeReactiveValues(stateDir, input, exclude)

} else {
vals <- serializeReactiveValues(input, exclude, stateDir = NULL)
stateFile <- file.path(stateDir, "state.rds")
saveRDS(values, stateFile)
})

vals <- vapply(vals, function(x) {
toJSON(x, strict_atomic = FALSE)
}, character(1), USE.NAMES = TRUE)
paste0("_state_id=", encodeURIComponent(id))
}

paste0(
encodeURIComponent(names(vals)),
"=",
encodeURIComponent(vals),
collapse = "&"

restoreStateURL <- function(queryString) {
values <- parseQueryString(queryString, nested = TRUE)
id <- values[["_state_id"]]

restoreInterface <- getShinyOption("restore.interface", default = restoreInterfaceLocal)

res <- NULL

restoreInterface(id, function(stateDir) {
stateFile <- file.path(stateDir, "state.rds")

res <<- list(
values = readRDS(stateFile),
dir = stateDir
)
}
})

res
}


#' @export
encodeStateURL <- function(input, exclude) {
vals <- serializeReactiveValues(input, exclude, stateDir = NULL)

vals <- vapply(vals,
function(x) toJSON(x, strict_atomic = FALSE),
character(1),
USE.NAMES = TRUE
)

paste0(
encodeURIComponent(names(vals)),
"=",
encodeURIComponent(vals),
collapse = "&"
)
}


#' @export
decodeStateURL <- function(url) {
values <- parseQueryString(url, nested = TRUE)

mapply(names(values), values, SIMPLIFY = FALSE,
FUN = function(name, value) {
tryCatch(
jsonlite::fromJSON(value),
error = function(e) {
stop("Failed to parse URL parameter \"", name, "\"")
}
)
}
)
}

# Restore context. This is basically a key-value store, except for one important
# difference: When the user `get()`s a value, the value is marked as pending;
# when `flushPending()` is called, those pending values are marked as used. When
Expand All @@ -79,21 +113,17 @@ RestoreContext <- R6Class("RestoreContext",
# other key/value pairs. If not, restore from key/value pairs in the
# query string.
if (!is.null(values[["_state_id"]]) && nzchar(values[["_state_id"]])) {
id <- values[["_state_id"]]

restoreState <- getShinyOption("restoreState", default = restoreStateLocal)

values <- restoreState(id, function(stateDir) {
private$dir <- stateDir
res <- restoreStateURL(queryString)

stateFile <- file.path(stateDir, "state.rds")
readRDS(stateFile)
})
values <- res$values
private$dir <- res$dir

} else {
# The query string contains the saved keys and values
values <- decodeBookmarkDataURL(queryString)
values <- decodeStateURL(queryString)
}

list2env(values, private$values)
}
},
Expand Down
2 changes: 1 addition & 1 deletion man/bookmarkOutput.Rd

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

2 changes: 1 addition & 1 deletion man/createBookmark.Rd

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

0 comments on commit 6c52c26

Please sign in to comment.