9
9
# ' @param code (`character`, `language` or `expression`) code to evaluate.
10
10
# ' It is possible to preserve original formatting of the `code` by providing a `character` or an
11
11
# ' `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.
12
15
# '
13
16
# ' @return
14
17
# ' `qenv` environment with `code/expr` evaluated or `qenv.error` if evaluation fails.
27
30
# ' @aliases eval_code,qenv.error,ANY-method
28
31
# '
29
32
# ' @export
30
- setGeneric ("eval_code ", function(object, code) standardGeneric("eval_code"))
33
+ setGeneric ("eval_code ", function(object, code, cache = FALSE, ... ) standardGeneric("eval_code"))
31
34
32
- setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code) {
35
+ setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code, cache = FALSE, ... ) {
33
36
parsed_code <- parse(text = code , keep.source = TRUE )
34
37
object @ .xData <- rlang :: env_clone(object @ .xData , parent = parent.env(.GlobalEnv ))
35
38
if (length(parsed_code ) == 0 ) {
@@ -42,13 +45,15 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
42
45
for (i in seq_along(code_split )) {
43
46
current_code <- code_split [[i ]]
44
47
current_call <- parse(text = current_code , keep.source = TRUE )
45
-
46
48
# Using withCallingHandlers to capture warnings and messages.
47
49
# Using tryCatch to capture the error and abort further evaluation.
48
50
x <- withCallingHandlers(
49
51
tryCatch(
50
52
{
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
+ }
52
57
if (! identical(parent.env(object @ .xData ), parent.env(.GlobalEnv ))) {
53
58
# needed to make sure that @.xData is always a sibling of .GlobalEnv
54
59
# 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
89
94
object
90
95
})
91
96
92
- setMethod ("eval_code ", signature = c("qenv", "language"), function(object, code) {
97
+ setMethod ("eval_code ", signature = c("qenv", "language"), function(object, code, cache = FALSE, ... ) {
93
98
eval_code(object , code = paste(vapply(lang2calls(code ), deparse1 , collapse = " \n " , character (1L )), collapse = " \n " ))
94
99
})
95
100
96
- setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code) {
101
+ setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code, cache = FALSE, ... ) {
97
102
srcref <- attr(code , " wholeSrcref" )
98
103
if (length(srcref )) {
99
104
eval_code(object , code = paste(attr(code , " wholeSrcref" ), collapse = " \n " ))
@@ -109,7 +114,7 @@ setMethod("eval_code", signature = c("qenv", "expression"), function(object, cod
109
114
}
110
115
})
111
116
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, ... ) {
113
118
object
114
119
})
115
120
0 commit comments