-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plt_add()
#246
Merged
grantmcdermott
merged 5 commits into
grantmcdermott:main
from
vincentarelbundock:plt_add
Nov 10, 2024
Merged
plt_add()
#246
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' Add new elements to the current `tinyplot` | ||
#' | ||
#' @description | ||
#' This function grabs the last `tinyplot` call, sets `add=TRUE`, and | ||
#' overwrites some of the arguments with those explicitly supplied by the | ||
#' user. Then, the call is evaluated again to add a layer to an existing plot. | ||
#' | ||
#' @details | ||
#' Note: the automatic legend for the added elements will be turned off. | ||
#' | ||
#' @param ... All named arguments override arguments from the previous calls. | ||
#' Arguments not supplied to `tinyplot_add()` remain unchanged from the previous call. | ||
#' | ||
#' @examples | ||
#' library(tinyplot) | ||
#' | ||
#' tinyplot(Sepal.Width ~ Sepal.Length | Species, | ||
#' facet = ~Species, | ||
#' data = iris, | ||
#' type = type_lm()) | ||
#' | ||
#' tinyplot_add(type = "p") | ||
#' | ||
#' @returns No return value, called for side effect of producing a plot. | ||
#' | ||
#' @export | ||
tinyplot_add <- function(...) { | ||
cal = getOption("tinyplot_last_call", default = NULL) | ||
|
||
## TODO: remove the global option above and move to this when density is refactored | ||
# cal = get(".last_call", envir = get(".tinyplot_env", envir = parent.env(environment()))) | ||
|
||
if (is.null(cal)) { | ||
stop("No previous tinyplot call found.") | ||
} | ||
|
||
args = list(...) | ||
for (n in names(args)) { | ||
if (n != "") { | ||
cal[[n]] = args[[n]] | ||
} | ||
} | ||
cal[["add"]] = TRUE | ||
eval(cal) | ||
} | ||
|
||
|
||
|
||
#' @export | ||
#' @name plt_add | ||
#' @rdname tinyplot_add | ||
plt_add = tinyplot_add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source("helpers.R") | ||
using("tinysnapshot") | ||
|
||
## error message cannot be tested in this suite | ||
# expect_error(tinyplot_add(type = "p"), pattern = "No previous tinyplot") | ||
|
||
f = function() { | ||
tinyplot(Sepal.Width ~ Sepal.Length | Species, | ||
facet = ~Species, | ||
data = iris, | ||
type = "p") | ||
tinyplot_add(type = type_lm()) | ||
} | ||
expect_snapshot_plot(f, label = "tinyplot_add") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the rest of the package infrastructure, let's rather save this to the existing
.tinyplot_env
. This is what we use inR/get_saved_par.R
, for an example:tinyplot/R/get_saved_par.R
Lines 104 to 116 in 02d7806
We'll need to assign NULL values to this call object at startup in
R/zzz.R
too, e.g.tinyplot/R/zzz.R
Line 46 in 02d7806
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I was unable to get this to work. I kept getting an error in the
tinyplot.density
tests. The message is obscure, having to do with ado.call
and asubstitute
. I just can figure this out.My guess is that this will disappear by itself when we refactor the density code.
In the meantime, I left TODOs in both files with the appropriate save code that we can eventually.
Feel free to take a shot at it if you want, but I have to give up for now.