Karl Forner
2013-Jun-24 14:54 UTC
[Rd] sys.source() does not provide the parsing info to eval()
Hello, It seems that the parsing information attached to expressions parsed by the parse() function when keep.source=TRUE is not provided to the eval() function. Please consider this code: path <- tempfile() code <- '(function() print( str( sys.calls() ) ))()' writeLines(code, path) sys.source(path, envir=globalenv(), keep.source=TRUE)> OUTPUT:Dotted pair list of 4 $ : language sys.source(path, envir = globalenv(), keep.source = TRUE) $ : language eval(i, envir) $ : language eval(expr, envir, enclos) $ : language (function() print(str(sys.calls())))() NULL then: eval(parse(text=code))> OUTPUT:Dotted pair list of 3 $ : language eval(parse(text = code)) $ : language eval(expr, envir, enclos) $ :length 1 (function() print(str(sys.calls())))() ..- attr(*, "srcref")=Class 'srcref' atomic [1:8] 1 1 1 42 1 42 1 1 .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951fc388> As you can see, when using eval() directly, the expression/call has the parsing information available in the "srcref" attribute, but not when using sys.source() Looking at sys.source() implementation, this seems to be caused by this line: for (i in exprs) eval(i, envir) The attribute "srcref" is not available anymore when "exprs" is subsetted, as illustred by the code below: ex <- parse( text="1+1; 2+2") attr(ex, 'srcref') print(str(ex)) # length 2 expression(1 + 1, 2 + 2) # - attr(*, "srcref")=List of 2 # ..$ :Class 'srcref' atomic [1:8] 1 1 1 3 1 3 1 1 # .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951f3b00> # ..$ :Class 'srcref' atomic [1:8] 1 6 1 8 6 8 1 1 # .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951f3b00> # - attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951f3b00> # - attr(*, "wholeSrcref")=Class 'srcref' atomic [1:8] 1 0 2 0 0 0 1 2 # .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951f3b00> # NULL print( str(ex[[1]])) # language 1 + 1 # NULL print( str(ex[1])) # length 1 expression(1 + 1) # - attr(*, "srcref")=List of 1 # ..$ :Class 'srcref' atomic [1:8] 1 1 1 3 1 3 1 1 # .. .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x7f42951f3b00> # NULL I suppose that the line "for (i in exprs) eval(i, envir)" could be replaced by "eval(exprs, envir)" ? Best, Karl Forner P.S> sessionInfo()R version 3.0.1 (2013-05-16) Platform: x86_64-unknown-linux-gnu (64-bit) ... [[alternative HTML version deleted]]
Maybe Matching Threads
- Proper way to drop 'srcref' from an expression created via substitute(function() ...)?
- possible bug in utils::removeSource - NULL argument is silently dropped
- formals(x)<- drops attributes including class
- removeSource() vs. function literals
- [PATCH] Code coverage support proof of concept