Skip to content

Commit 376d3b6

Browse files
authored
Merge pull request rstudio#1760 from rstudio/wch-snapshot-preprocess
Add snapshotPreprocess function
2 parents 9fc5758 + df7397a commit 376d3b6

File tree

10 files changed

+85
-18
lines changed

10 files changed

+85
-18
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: shiny
22
Type: Package
33
Title: Web Application Framework for R
4-
Version: 1.0.3.9000
4+
Version: 1.0.3.9001
55
Authors@R: c(
66
person("Winston", "Chang", role = c("aut", "cre"), email = "[email protected]"),
77
person("Joe", "Cheng", role = "aut", email = "[email protected]"),
@@ -84,7 +84,7 @@ Suggests:
8484
magrittr
8585
URL: http://shiny.rstudio.com
8686
BugReports: https://github.com/rstudio/shiny/issues
87-
Collate:
87+
Collate:
8888
'app.R'
8989
'bookmark-state-local.R'
9090
'stack.R'
@@ -144,6 +144,7 @@ Collate:
144144
'shinyui.R'
145145
'shinywrappers.R'
146146
'showcase.R'
147+
'snapshot.R'
147148
'tar.R'
148149
'test-export.R'
149150
'timer.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ export(sidebarPanel)
218218
export(singleton)
219219
export(sliderInput)
220220
export(snapshotExclude)
221+
export(snapshotPreprocess)
221222
export(span)
222223
export(splitLayout)
223224
export(stopApp)

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
shiny 1.0.3.9000
1+
shiny 1.0.3.9001
22
================
33

44
## Full changelog
@@ -21,6 +21,8 @@ shiny 1.0.3.9000
2121

2222
* Addressed [#1738](https://github.com/rstudio/shiny/issues/1738): The `updateTextInput` and `updateTextAreaInput` functions can now update the placeholder. ([#1742](https://github.com/rstudio/shiny/pull/1742))
2323

24+
* Added new `snapshotPreprocess()` function, which is used for preprocessing output values before taking a test snapshot. ([#1760](https://github.com/rstudio/shiny/pull/1760))
25+
2426
### Bug fixes
2527

2628
* Fixed [#1546](https://github.com/rstudio/shiny/issues/1546): make it possible (without any hacks) to write arbitrary data into a module's `session$userData` (which is exactly the same environment as the parent's `session$userData`). To be clear, it allows something like `session$userData$x <- TRUE`, but not something like `session$userData <- TRUE` (that is not allowed in any context, whether you're in the main app, or in a module) ([#1732](https://github.com/rstudio/shiny/pull/1732)).

R/shiny.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,19 @@ ShinySession <- R6Class(
658658
}, logical(1))
659659
values$output <- values$output[!exclude_idx]
660660

661+
# Apply snapshotPreprocess functions for outputs that have them.
662+
values$output <- lapply(
663+
setNames(names(values$output), names(values$output)),
664+
function(name) {
665+
preprocess <- attr(private$.outputs[[name]], "snapshotPreprocess", TRUE)
666+
if (is.function(preprocess)) {
667+
preprocess(values$output[[name]])
668+
} else {
669+
values$output[[name]]
670+
}
671+
}
672+
)
673+
661674
values$output <- sortByName(values$output)
662675
}
663676

@@ -1889,17 +1902,6 @@ outputOptions <- function(x, name, ...) {
18891902
.subset2(x, 'impl')$outputOptions(name, ...)
18901903
}
18911904

1892-
1893-
#' Mark an output to be excluded from test snapshots
1894-
#'
1895-
#' @param x A reactive which will be assigned to an output.
1896-
#'
1897-
#' @export
1898-
snapshotExclude <- function(x) {
1899-
markOutputAttrs(x, snapshotExclude = TRUE)
1900-
}
1901-
1902-
19031905
#' Add callbacks for Shiny session events
19041906
#'
19051907
#' These functions are for registering callbacks on Shiny session events.

R/shinywrappers.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ as.tags.shiny.render.function <- function(x, ..., inline = FALSE) {
9393
#'
9494
#' @inheritParams markRenderFunction
9595
#' @param snapshotExclude If TRUE, exclude the output from test snapshots.
96+
#' @param snapshotPreprocess A function for preprocessing the value before
97+
#' taking a test snapshot.
9698
#'
9799
#' @keywords internal
98-
markOutputAttrs <- function(renderFunc, snapshotExclude = NULL) {
100+
markOutputAttrs <- function(renderFunc, snapshotExclude = NULL,
101+
snapshotPreprocess = NULL)
102+
{
99103
# Add the outputAttrs attribute if necessary
100104
if (is.null(attr(renderFunc, "outputAttrs", TRUE))) {
101105
attr(renderFunc, "outputAttrs") <- list()
@@ -105,6 +109,10 @@ markOutputAttrs <- function(renderFunc, snapshotExclude = NULL) {
105109
attr(renderFunc, "outputAttrs")$snapshotExclude <- snapshotExclude
106110
}
107111

112+
if (!is.null(snapshotPreprocess)) {
113+
attr(renderFunc, "outputAttrs")$snapshotPreprocess <- snapshotPreprocess
114+
}
115+
108116
renderFunc
109117
}
110118

@@ -533,7 +541,18 @@ renderDataTable <- function(expr, options = NULL, searchDelay = 500,
533541
)
534542
}
535543

536-
markRenderFunction(dataTableOutput, renderFunc, outputArgs = outputArgs)
544+
renderFunc <- markRenderFunction(dataTableOutput, renderFunc, outputArgs = outputArgs)
545+
546+
renderFunc <- snapshotPreprocess(renderFunc, function(value) {
547+
# Remove the action field so that it's not saved in test snapshots. It
548+
# contains a value that changes every time an app is run, and shouldn't be
549+
# stored for test snapshots. It will be something like:
550+
# "session/e0d14d3fe97f672f9655a127f2a1e079/dataobj/table?w=&nonce=7f5d6d54e22450a3"
551+
value$action <- NULL
552+
value
553+
})
554+
555+
renderFunc
537556
}
538557

539558
# a data frame containing the DataTables 1.9 and 1.10 names

R/snapshot.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#' Mark an output to be excluded from test snapshots
2+
#'
3+
#' @param x A reactive which will be assigned to an output.
4+
#'
5+
#' @export
6+
snapshotExclude <- function(x) {
7+
markOutputAttrs(x, snapshotExclude = TRUE)
8+
}
9+
10+
#' Add a function for preprocessing an output before taking a test snapshot
11+
#'
12+
#' @param x A reactive which will be assigned to an output.
13+
#' @param fun A function that takes the output value as an input and returns a
14+
#' modified value. The returned value will be used for the test snapshot.
15+
#'
16+
#' @export
17+
snapshotPreprocess <- function(x, fun) {
18+
markOutputAttrs(x, snapshotPreprocess = fun)
19+
}
20+

inst/staticdocs/index.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ sd_section("Utility functions",
194194
"plotPNG",
195195
"exportTestValues",
196196
"snapshotExclude",
197+
"snapshotPreprocess",
197198
"markOutputAttrs",
198199
"repeatable",
199200
"shinyDeprecated",

man/markOutputAttrs.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/snapshotExclude.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/snapshotPreprocess.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)