diff --git a/NEWS.md b/NEWS.md index d1863e24..9853f112 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,10 @@ where the formatting is also better._ - `type_text()` now defaults to displaying `y` values if an explicit `labels` arg is not provided, mirroring the behaviour of the base `text()` function. (#501 @grantmcdermott) +- Determining the last call of the `tinyplot()` generic in preparation for + `tinyplot_add()` is now more robust so that it is compatible with `do.call()` + again (reported by @FlorianSchwendinger). This is achieved by inspecting + the functions called rather than just their names. (#504 @zeileis) ### Documentation diff --git a/R/tinyplot.R b/R/tinyplot.R index 597b8f05..6f0da3eb 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -652,10 +652,10 @@ tinyplot.default = function( assert_logical(add) if (!add) { calls = sys.calls() - tinyplot_calls = "(^tinyplot$)|(^tinyplot::tinyplot$)|(^plt$)|(^tinyplot::plt)|(^tinyplot:::)" - idx = grep(tinyplot_calls, sapply(calls, function(k) k[[1]])) + is_tinyplot_call <- function(x) identical(tinyplot, try(eval(x[[1L]]), silent = TRUE)) + idx = which(vapply(calls, is_tinyplot_call, FALSE)) if (length(idx) > 0) { - set_environment_variable(.last_call = calls[[idx[1]]]) + set_environment_variable(.last_call = calls[[idx[1L]]]) } } diff --git a/inst/tinytest/_tinysnapshot/tinyplot_do_call.svg b/inst/tinytest/_tinysnapshot/tinyplot_do_call.svg new file mode 100644 index 00000000..0da06841 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tinyplot_do_call.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + +x +y + + + + + + +1 +2 +3 +4 +5 + + + + + +-1.0 +-0.5 +0.0 +0.5 + + + + + + + + + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinyplot_add.R b/inst/tinytest/test-tinyplot_add.R index 1ee292c9..af2a4305 100644 --- a/inst/tinytest/test-tinyplot_add.R +++ b/inst/tinytest/test-tinyplot_add.R @@ -75,6 +75,15 @@ f = function() { expect_snapshot_plot(f, label = "tinyplot_lollipop") +# use tinyplot_add() after do.call(tinyplot) +f = function() { + d = data.frame(x = 1:5, y = sin(1:5)) + do.call(tinyplot, list(y ~ x, data = d)) + do.call(tinyplot_add, list(type = "h")) +} +expect_snapshot_plot(f, label = "tinyplot_do_call") + + # check that we are avoiding recursive margins for facets, by properly restoring # the original state f = function() { @@ -83,4 +92,4 @@ f = function() { tinyplot(Sepal.Width ~ Sepal.Length, facet = ~Species, data = iris) tinyplot_add(type = "lm") } -expect_snapshot_plot(f, label = "tinyplot_add_no_recursive_margins") \ No newline at end of file +expect_snapshot_plot(f, label = "tinyplot_add_no_recursive_margins")