-
Notifications
You must be signed in to change notification settings - Fork 11
Add function to remove environments tracked by ggplot2 #58
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
Open
vandenman
wants to merge
1
commit into
jasp-stats:master
Choose a base branch
from
vandenman:cleanPlotEnvironment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: jaspGraphs | ||
Type: Package | ||
Title: Custom Graphs for JASP | ||
Version: 0.5.2.12 | ||
Version: 0.5.2.14 | ||
Author: Don van den Bergh | ||
Maintainer: JASP-team <[email protected]> | ||
Description: Graph making functions and wrappers for JASP. | ||
|
This file contains hidden or 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 hidden or 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,169 @@ | ||
#' @title removeEnvironments - remove environments from ggplot2 objects | ||
#' @param x a plot object. Currently, only ggplot2 objects and jaspGraphsPlot are supported, other objects are returned unchanged | ||
#' @param debugInfo Should debugInfo be printed? Possible values are 0, no info is shown; 1, some info is shown; or 2, a lot of info is shown. | ||
#' @param ... internal. | ||
#' | ||
#' @details This function may breaks ggplot2 objects that rely on looking up values in environments. | ||
#' see also https://github.com/tidyverse/ggplot2/issues/3994 and https://github.com/tidyverse/ggplot2/issues/4056 | ||
#' | ||
#' @example inst/examples/ex-removeEnvironments.R | ||
#' @returns an object of the same class as the input | ||
#' @export | ||
removeEnvironments <- function(x, debugInfo = 0,...) { | ||
UseMethod("removeEnvironments", x) | ||
} | ||
|
||
#' @export | ||
removeEnvironments.ggplot <- function(x, debugInfo = 0, ...) { | ||
x$plot_env <- rlang::new_environment() | ||
return(removeEnvironmentsInner(x, debugInfo = debugInfo, ...)) | ||
} | ||
|
||
#' @export | ||
removeEnvironments.jaspGraphsPlot <- function(x, debugInfo = 0, ...) { | ||
|
||
for (i in seq_along(x)) { | ||
if (debugInfo >= 1) cat("subplot", i, "\n") | ||
x[[i]] <- removeEnvironmentsInner(x, debugInfo = debugInfo, ...) | ||
} | ||
return(x) | ||
} | ||
|
||
#' @export | ||
removeEnvironments.default <- function(x, debugInfo = 0, ...) { | ||
if (debugInfo >= 1) cat("dispatched to removeEnvironments.default\n") | ||
return(x) | ||
} | ||
|
||
removeEnvironmentsInner <- function( | ||
x, | ||
exitEarlyClasses = c("unit", "element", "data.frame", "FacetNull"), | ||
replacementEnv = rlang::new_environment(parent = baseenv()), | ||
environmentsAlreadyVisited = rlang::new_environment(), | ||
debugInfo = 1, | ||
level = deparse1(substitute(x))) { | ||
|
||
if (inherits(x, exitEarlyClasses) || isNamespace(x)) | ||
return(x) | ||
|
||
if (debugInfo >= 2) { | ||
cat("level:", level, "\n") | ||
cat("class:", paste(class(x), collapse = ", "), "\n") | ||
} | ||
|
||
if (inherits(x, "uneval")) { # returned by ggplot2::aes | ||
|
||
if (debugInfo >= 1) { | ||
cat("level:", level, "\n") | ||
cat("class:", paste(class(x), collapse = ", "), "\n") | ||
cat("replacing environment of subelements of x\n") | ||
} | ||
|
||
for (i in seq_along(x)) { | ||
environment(x[[i]]) <- replacementEnv | ||
} | ||
|
||
} else if (inherits(x, "gtable")) { | ||
|
||
# gtable has special indexing with [[ and integers so we use names | ||
for (i in setdiff(names(x), c("widths", "heights"))) { | ||
if (!is.null(x[[i]])) { | ||
level <- paste0(level, "$", i) | ||
x[[i]] <- Recall(x[[i]], exitEarlyClasses, replacementEnv, environmentsAlreadyVisited, debugInfo, level) | ||
} | ||
} | ||
|
||
} else if (is.list(x)) { | ||
|
||
if (!is.null(environment(x)) && !isNamespace(environment(x))) { | ||
if (debugInfo >= 1) { | ||
cat("level:", level, "\n") | ||
cat("class:", paste(class(x), collapse = ", "), "\n") | ||
cat("replacing environment(x)\n") | ||
} | ||
environment(x) <- replacementEnv | ||
} | ||
|
||
nms <- names(x) | ||
for (i in seq_along(x)) { | ||
if (!is.null(x[[i]])) { | ||
|
||
if (is.null(nms) || nms[i] == "") { | ||
newlevel <- paste0(level, "[[", i, "]]") | ||
} else { | ||
newlevel <- paste0(level, "$", nms[i]) | ||
} | ||
x[[i]] <- Recall(x[[i]], exitEarlyClasses, replacementEnv, environmentsAlreadyVisited, debugInfo, newlevel) | ||
} | ||
} | ||
|
||
} else if (ggplot2::is.ggproto(x)) { | ||
|
||
if (inherits(x, "LayerInstance") && inherits(x$geom, "GeomCustomAnn")) { | ||
|
||
# cowplot saves unnecessary stuff | ||
super <- .subset2(x, "super") | ||
superEnv <- environment(super) | ||
superEnv$env$geom_params <- NULL | ||
superEnv$env$params <- NULL | ||
superEnv$env$geom <- NULL | ||
|
||
} | ||
|
||
hash <- rlang::hash(x) | ||
if (!exists(hash, envir = environmentsAlreadyVisited)) { | ||
|
||
environmentsAlreadyVisited[[hash]] <- NULL | ||
nms <- union(names(x), names(as.list(x))) | ||
for (i in nms) { | ||
if (!is.null(.subset2(x, i)) && !isNamespace(environment(.subset2(x, i)))) { | ||
newlevel <- paste0(level, "$", i) | ||
x[[i]] <- Recall(.subset2(x, i), exitEarlyClasses, replacementEnv, environmentsAlreadyVisited, debugInfo, newlevel) | ||
} | ||
} | ||
|
||
} | ||
|
||
} else if (is.environment(x) && !isNamespace(x) && !identical(x, .GlobalEnv)) { | ||
|
||
hash <- rlang::hash(x) | ||
if (!exists(hash, envir = environmentsAlreadyVisited)) { | ||
|
||
environmentsAlreadyVisited[[hash]] <- NULL | ||
for (i in names(x)) { | ||
newlevel <- paste0(level, "$", i) | ||
x[[i]] <- Recall(x[[i]], exitEarlyClasses, replacementEnv, environmentsAlreadyVisited, debugInfo, newlevel) | ||
} | ||
} | ||
|
||
} else if (is.function(x)) { | ||
|
||
hash <- rlang::hash(x) | ||
if (!exists(hash, envir = environmentsAlreadyVisited)) { | ||
|
||
environmentsAlreadyVisited[[hash]] <- NULL | ||
newlevel <- paste0("environment(", level, ")") | ||
environment(x) <- Recall(environment(x), exitEarlyClasses, replacementEnv, environmentsAlreadyVisited, debugInfo, newlevel) | ||
|
||
} | ||
|
||
} else if (!is.null(x) && !is.function(x) && !is.null(environment(x)) && !isNamespace(environment(x))) { | ||
if (debugInfo >= 1) { | ||
cat("level:", level, "\n") | ||
cat("class:", paste(class(x), collapse = ", "), "\n") | ||
cat("replacing environment(x)\n") | ||
} | ||
environment(x) <- replacementEnv | ||
} else if (mode(x) == "...") { | ||
# DOTSXP objects... | ||
if (debugInfo >= 1) { | ||
cat("level:", level, "\n") | ||
cat("class:", paste(class(x), collapse = ", "), "\n") | ||
cat("replacing DOTSXP with NULL\n") | ||
} | ||
return(NULL) | ||
} | ||
|
||
return(x) | ||
|
||
} |
This file contains hidden or 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,64 @@ | ||
\dontrun{ | ||
demo <- function(n = 1e6) { | ||
# adapted from https://github.com/tidyverse/ggplot2/issues/4056 | ||
big_object <- rnorm(n) | ||
df <- data.frame(x = -5:5, y = abs(-5:5)) | ||
ggplot2::ggplot(df, ggplot2::aes(x, y)) + | ||
ggplot2::geom_point() + | ||
ggplot2::geom_point(ggplot2::aes(x = x+1, y = y+1), color = "green") | ||
} | ||
|
||
sizeOnDisk <- function(obj) { | ||
f <- tempfile() | ||
saveRDS(obj, file = f) | ||
sz <- file.size(f) | ||
file.remove(f) | ||
class(sz) <- "object_size" | ||
sz | ||
} | ||
|
||
obj <- demo() | ||
obj | ||
|
||
# object.size lies about the size but | ||
print(object.size(obj), units = "auto") | ||
# 7.1 Kb | ||
|
||
print(object.size(obj$plot_env$big_object), units = "auto") | ||
# 7.6 Mb | ||
|
||
# lobstr::obj_size follows environments and does not lie about the size | ||
if (require("lobstr")) { | ||
sz <- lobstr::obj_size(obj) | ||
class(sz) <- "object_size" | ||
print(sz, units = "auto") | ||
# 8.5 Mb | ||
} | ||
|
||
print(sizeOnDisk(obj), units = "auto") | ||
# 7.4 Mb | ||
|
||
# note that to an extent, removeEnvironments modifies in place | ||
obj <- removeEnvironments(obj) | ||
|
||
obj # still works | ||
|
||
print(sizeOnDisk(obj), units = "auto") | ||
# 33 Kb | ||
|
||
# removeEnvironments may break plots that rely on looking things up in the parent environment | ||
demoBad <- function() { | ||
x = -5:5 | ||
y = abs(-5:5) | ||
ggplot2::ggplot(mapping = ggplot2::aes(x, y)) + | ||
ggplot2::geom_point() + | ||
ggplot2::geom_point(ggplot2::aes(x = x+1, y = y+1), color = "red") | ||
} | ||
|
||
obj <- demoBad() | ||
obj # works | ||
|
||
obj <- removeEnvironments(obj) | ||
obj # Error in FUN(X[[i]], ...) : object 'x' not found | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Now that I think of it, this part (and the other parts that remove environments) actually remove all environments. Perhaps I should add an option to only remove specified environments (by default
ggplotObj$plot_env
).