-
-
Notifications
You must be signed in to change notification settings - Fork 8
Use evaluate #258
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
gogonzo
wants to merge
14
commits into
main
Choose a base branch
from
use_evaluate
base: main
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
Use evaluate #258
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
79279da
evaluate caches outputs
gogonzo 9c1f395
WIP
gogonzo ec49b8d
fix R CMD check
gogonzo 004364f
- single evaluate::evaluate
gogonzo 208db3b
fix: problem with tests during R CMD check
averissimo 8587ca7
Fix tests in `use_evaluate` branch (#259)
averissimo 0f2861b
Move `library()` override back to `eval_code()` (#260)
averissimo ab12555
[skip style] [skip vbump] Restyle files
github-actions[bot] 6995ae5
chore: fix test from PR merge
averissimo f878fed
Apply suggestions from code review
gogonzo c26c322
testing
gogonzo 4858a08
Adds some tests to `use_evaluate` feature branch and avoids deprecate…
averissimo a834e08
fix: test with S4 method registration
averissimo d17b8df
vignette
gogonzo 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,5 +1,4 @@ | ||
linters: linters_with_defaults( | ||
line_length_linter = line_length_linter(120), | ||
cyclocomp_linter = NULL, | ||
object_usage_linter = NULL | ||
) |
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
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
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 |
---|---|---|
|
@@ -44,60 +44,57 @@ setMethod("eval_code", signature = c(object = "qenv.error"), function(object, co | |
if (identical(trimws(code), "") || length(code) == 0) { | ||
return(object) | ||
} | ||
code <- paste(split_code(code), collapse = "\n") | ||
averissimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[email protected] <- rlang::env_clone([email protected], parent = parent.env([email protected])) | ||
parsed_code <- parse(text = code, keep.source = TRUE) | ||
[email protected] <- rlang::env_clone([email protected], parent = parent.env(.GlobalEnv)) | ||
if (length(parsed_code) == 0) { | ||
# empty code, or just comments | ||
attr(code, "dependency") <- extract_dependency(parsed_code) # in case comment contains @linksto tag | ||
object@code <- c(object@code, stats::setNames(list(code), sample.int(.Machine$integer.max, size = 1))) | ||
return(object) | ||
} | ||
code_split <- split_code(paste(code, collapse = "\n")) | ||
for (i in seq_along(code_split)) { | ||
current_code <- code_split[[i]] | ||
current_call <- parse(text = current_code, keep.source = TRUE) | ||
# Using withCallingHandlers to capture warnings and messages. | ||
# Using tryCatch to capture the error and abort further evaluation. | ||
x <- withCallingHandlers( | ||
tryCatch( | ||
{ | ||
eval(current_call, envir = [email protected]) | ||
if (!identical(parent.env([email protected]), parent.env(.GlobalEnv))) { | ||
# needed to make sure that @.xData is always a sibling of .GlobalEnv | ||
# could be changed when any new package is added to search path (through library or require call) | ||
parent.env([email protected]) <- parent.env(.GlobalEnv) | ||
} | ||
NULL | ||
}, | ||
error = function(e) { | ||
|
||
old <- evaluate::inject_funs( | ||
library = function(...) { | ||
x <- library(...) | ||
if (!identical(parent.env([email protected]), parent.env(.GlobalEnv))) { | ||
parent.env([email protected]) <- parent.env(.GlobalEnv) | ||
} | ||
invisible(x) | ||
} | ||
) | ||
out <- evaluate::evaluate( | ||
code, | ||
envir = [email protected], | ||
stop_on_error = 1, | ||
output_handler = evaluate::new_output_handler(value = identity) | ||
) | ||
out <- evaluate::trim_intermediate_plots(out) | ||
|
||
evaluate::inject_funs(old) # remove library() override | ||
|
||
new_code <- list() | ||
for (this in out) { | ||
if (inherits(this, "source")) { | ||
this_code <- gsub("\n$", "", this$src) | ||
attr(this_code, "dependency") <- extract_dependency(parse(text = this_code, keep.source = TRUE)) | ||
new_code <- c(new_code, stats::setNames(list(this_code), sample.int(.Machine$integer.max, size = 1))) | ||
} else { | ||
last_code <- new_code[[length(new_code)]] | ||
if (inherits(this, "error")) { | ||
return( | ||
errorCondition( | ||
message = sprintf( | ||
"%s \n when evaluating qenv code:\n%s", | ||
cli::ansi_strip(conditionMessage(e)), | ||
current_code | ||
cli::ansi_strip(conditionMessage(this)), | ||
last_code | ||
), | ||
class = c("qenv.error", "try-error", "simpleError"), | ||
trace = unlist(c(object@code, list(current_code))) | ||
trace = unlist(c(object@code, list(new_code))) | ||
) | ||
} | ||
), | ||
warning = function(w) { | ||
attr(current_code, "warning") <<- cli::ansi_strip(sprintf("> %s\n", conditionMessage(w))) | ||
invokeRestart("muffleWarning") | ||
}, | ||
message = function(m) { | ||
attr(current_code, "message") <<- cli::ansi_strip(sprintf("> %s", conditionMessage(m))) | ||
invokeRestart("muffleMessage") | ||
) | ||
} | ||
) | ||
|
||
if (!is.null(x)) { | ||
return(x) | ||
attr(last_code, "outputs") <- c(attr(last_code, "outputs"), list(this)) | ||
new_code[[length(new_code)]] <- last_code | ||
} | ||
attr(current_code, "dependency") <- extract_dependency(current_call) | ||
object@code <- c(object@code, stats::setNames(list(current_code), sample.int(.Machine$integer.max, size = 1))) | ||
} | ||
|
||
object@code <- c(object@code, new_code) | ||
lockEnvironment([email protected], bindings = TRUE) | ||
object | ||
} | ||
|
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,30 @@ | ||
#' Get outputs | ||
#' | ||
#' @description | ||
#' `eval_code` evaluates code silently so plots and prints don't show up in the console or graphic devices. | ||
#' If one wants to use an output outside of the `qenv` (e.g. use a graph in `renderPlot`) then use `get_outputs`. | ||
#' @param object (`qenv`) | ||
#' @return list of outputs generated in a `qenv`` | ||
#' @examples | ||
#' q <- eval_code( | ||
#' qenv(), | ||
#' quote({ | ||
#' a <- 1 | ||
#' print("I'm an output") | ||
#' plot(1) | ||
#' }) | ||
#' ) | ||
#' get_outputs(q) | ||
#' | ||
#' @aliases get_outputs,qenv-method | ||
#' | ||
#' @export | ||
setGeneric("get_outputs", function(object) standardGeneric("get_outputs")) | ||
|
||
setMethod("get_outputs", signature = "qenv", function(object) { | ||
Reduce( | ||
function(x, y) c(x, attr(y, "outputs")), | ||
init = list(), | ||
x = object@code | ||
) | ||
}) |
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
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 |
---|---|---|
|
@@ -31,6 +31,7 @@ reference: | |
- eval_code | ||
- get_code | ||
- get_env | ||
- get_outputs | ||
- get_var | ||
- get_messages | ||
- get_warnings | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Should we add a small section on the vignette as well? in addition to "### Warnings and messages in
qenv
objects"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.
I've included some info about
get_outputs()
. Please have a look once again before I merge