Skip to content
Merged
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ Theme fixes:
- Fixed `xlab = NA` / `ylab = NA` gotchas: a barplot with `xlab = NA` no longer
errors, and spineplots no longer clip their category and tick labels when
`xlab`/`ylab` are set to `NA` under a dynamic theme. (#635 @grantmcdermott)
- `tinyplot_add()` (`plt_add()`) now captures its arguments unevaluated, so
arguments that rely on non-standard evaluation against `data` (e.g.,
`plt_add(..., subset = <>)`) resolve correctly instead of erroring with
"object not found". (#638 @grantmcdermott)

## v0.6.1

Expand Down
30 changes: 23 additions & 7 deletions R/tinyplot_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#'
#' @param ... All named arguments override arguments from the previous calls.
#' Arguments not supplied to [tinyplot_add] remain unchanged from the previous
#' call.
#' call. Arguments are captured unevaluated and spliced into the updated call,
#' so those that rely on non-standard evaluation against `data`---e.g.
#' `subset = cyl == 4` or `weights = wt`---behave the same as in a direct
#' [`tinyplot`] call.
#'
#' @examples
#' tinyplot(Sepal.Width ~ Sepal.Length | Species,
Expand All @@ -37,6 +40,10 @@
#' # type = "lm",
#' # add = TRUE)
#'
#' ## Arguments relying on non-standard evaluation (e.g. `subset`) work too:
#' tinyplot(mpg ~ wt, data = mtcars)
#' tinyplot_add(subset = cyl == 4, col = "red", pch = 16)
#'
#' @returns No return value, called for side effect of producing a plot.
#'
#' @export
Expand All @@ -47,16 +54,25 @@ tinyplot_add = function(...) {
stop("No previous tinyplot call found.")
}

args = list(...)
for (n in names(args)) {
if (n != "") {
cal[[n]] = args[[n]]
# Capture the dots as *unevaluated* expressions rather than evaluated values.
# This way, arguments that rely on non-standard evaluation against `data`
# (e.g. `subset = cyl == 4`, `weights = wt`, or a bare-name formula) are
# spliced into the rebuilt call and only resolved when it is finally
# evaluated through the formula method's `model.frame`. Using `list(...)`
# here would instead force evaluation in this frame, where data-scoped
# variables are not visible. (#630)
args = match.call(expand.dots = FALSE)[["..."]]
nms = names(args)
for (i in seq_along(args)) {
n = if (is.null(nms)) "" else nms[[i]]
if (!is.null(n) && n != "") {
cal[[n]] = args[[i]]
}
}

# allow first argument in tinyplot_add() to be unnamed
if (isTRUE(names(args)[1] == "")) {
cal[[2]] = args[[1]]
if (length(args) >= 1L && (is.null(nms) || nms[[1L]] == "")) {
cal[[2]] = args[[1L]]
}

cal[["add"]] = TRUE
Expand Down
104 changes: 104 additions & 0 deletions inst/tinytest/_tinysnapshot/tinyplot_add_subset.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions inst/tinytest/test-tinyplot_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@ f = function() {
tinyplot_add(type = "jitter", cex = 0.5, alpha = 0.3)
}
expect_snapshot_plot(f, label = "tinyplot_add_jitter_on_grouped_violin")

# subset NSE in tinyplot_add (#630)
f = function() {
tinyplot(mpg ~ wt, data = mtcars)
tinyplot_add(subset = cyl == 4, col = "red", pch = 16)
}
expect_snapshot_plot(f, label = "tinyplot_add_subset")
9 changes: 8 additions & 1 deletion man/tinyplot_add.Rd

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