99# ' @param code (`character`, `language` or `expression`) code to evaluate.
1010# ' It is possible to preserve original formatting of the `code` by providing a `character` or an
1111# ' `expression` being a result of `parse(keep.source = TRUE)`.
12+ # ' @param cache (`logical(1)`) whether to cache returned value of the code evaluation.
13+ # '
14+ # ' @param ... ([`dots`]) additional arguments passed to future methods.
1215# '
1316# ' @return
1417# ' `qenv` environment with `code/expr` evaluated or `qenv.error` if evaluation fails.
2730# ' @aliases eval_code,qenv.error,ANY-method
2831# '
2932# ' @export
30- setGeneric ("eval_code ", function(object, code) standardGeneric("eval_code"))
33+ setGeneric ("eval_code ", function(object, code, cache = FALSE, ... ) standardGeneric("eval_code"))
3134
32- setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code) {
35+ setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code, cache = FALSE, ... ) {
3336 parsed_code <- parse(text = code , keep.source = TRUE )
3437 object @ .xData <- rlang :: env_clone(object @ .xData , parent = parent.env(.GlobalEnv ))
3538 if (length(parsed_code ) == 0 ) {
@@ -42,13 +45,15 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
4245 for (i in seq_along(code_split )) {
4346 current_code <- code_split [[i ]]
4447 current_call <- parse(text = current_code , keep.source = TRUE )
45-
4648 # Using withCallingHandlers to capture warnings and messages.
4749 # Using tryCatch to capture the error and abort further evaluation.
4850 x <- withCallingHandlers(
4951 tryCatch(
5052 {
51- eval(current_call , envir = object @ .xData )
53+ out <- eval(current_call , envir = object @ .xData )
54+ if (cache && i == seq_along(code_split )) {
55+ attr(current_code , " cache" ) <- out
56+ }
5257 if (! identical(parent.env(object @ .xData ), parent.env(.GlobalEnv ))) {
5358 # needed to make sure that @.xData is always a sibling of .GlobalEnv
5459 # could be changed when any new package is added to search path (through library or require call)
@@ -89,11 +94,11 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
8994 object
9095})
9196
92- setMethod ("eval_code ", signature = c("qenv", "language"), function(object, code) {
97+ setMethod ("eval_code ", signature = c("qenv", "language"), function(object, code, cache = FALSE, ... ) {
9398 eval_code(object , code = paste(vapply(lang2calls(code ), deparse1 , collapse = " \n " , character (1L )), collapse = " \n " ))
9499})
95100
96- setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code) {
101+ setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code, cache = FALSE, ... ) {
97102 srcref <- attr(code , " wholeSrcref" )
98103 if (length(srcref )) {
99104 eval_code(object , code = paste(attr(code , " wholeSrcref" ), collapse = " \n " ))
@@ -109,7 +114,7 @@ setMethod("eval_code", signature = c("qenv", "expression"), function(object, cod
109114 }
110115})
111116
112- setMethod ("eval_code ", signature = c("qenv.error", "ANY"), function(object, code) {
117+ setMethod ("eval_code ", signature = c("qenv.error", "ANY"), function(object, code, cache = FALSE, ... ) {
113118 object
114119})
115120
0 commit comments