From 12f58a61f17b94188f8f0368c844a2778c0d9dd1 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Thu, 14 Nov 2024 06:40:19 -0500 Subject: [PATCH] tinyplot_add call multiple times --- R/tinyplot.R | 11 +++- .../_tinysnapshot/tinyplot_add_multiple.svg | 64 +++++++++++++++++++ inst/tinytest/test-tinyplot_add.R | 23 ++++++- 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 inst/tinytest/_tinysnapshot/tinyplot_add_multiple.svg diff --git a/R/tinyplot.R b/R/tinyplot.R index ead4d5a8..444bbb6b 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -547,9 +547,14 @@ tinyplot.default = function( xaxs = NULL, yaxs = NULL, ...) { - # save for plt_add() - options(tinyplot_last_call = match.call(tinyplot, - call = sys.call(sys.parent()), expand.dots = TRUE)) + # save for tinyplot_add() + if (!isTRUE(add)) { + calls = sys.calls() + idx = grep("^tinyplot", sapply(calls, function(k) k[[1]])) + if (length(idx) > 0) { + options(tinyplot_last_call = calls[[idx[1]]]) + } + } ## TODO: remove the global option above and move to this when density is refactored # cal = match.call(call = sys.call(sys.parent()), expand.dots = TRUE) diff --git a/inst/tinytest/_tinysnapshot/tinyplot_add_multiple.svg b/inst/tinytest/_tinysnapshot/tinyplot_add_multiple.svg new file mode 100644 index 00000000..99e78c4e --- /dev/null +++ b/inst/tinytest/_tinysnapshot/tinyplot_add_multiple.svg @@ -0,0 +1,64 @@ + + + + + + + + + + + + +k +Frequency + + + + + + + + +-3 +-2 +-1 +0 +1 +2 +3 + + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 + + + + + + + + + + + + + diff --git a/inst/tinytest/test-tinyplot_add.R b/inst/tinytest/test-tinyplot_add.R index 9467a012..f4e3fa5b 100644 --- a/inst/tinytest/test-tinyplot_add.R +++ b/inst/tinytest/test-tinyplot_add.R @@ -6,9 +6,26 @@ using("tinysnapshot") f = function() { tinyplot(Sepal.Width ~ Sepal.Length | Species, - facet = ~Species, - data = iris, - type = "p") + facet = ~Species, + data = iris, + type = "p") tinyplot_add(type = type_lm()) } expect_snapshot_plot(f, label = "tinyplot_add") + + +f = function() { + k = seq(-3, 3, length.out = 100) + tinyplot(x = k, type = type_function(dnorm)) + tinyplot_add( + x = k[k < -1.96], + ymax = dnorm(k[k < -1.96]), + ymin = 0, + type = "ribbon") + tinyplot_add( + x = k[k > 1], + ymax = dnorm(k[k > 1]), + ymin = 0, + type = "ribbon") +} +expect_snapshot_plot(f, label = "tinyplot_add_multiple")