Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redundancy of Function Parse Trees in R

Tags:

r

parse-tree

In R, I noticed that the parse tree of the function operator seems to be redundant in the sense that its fourth element is seemingly always composed of the first three elements.

For example,

> as.list(substitute(function(x = 1){x^2}))
[[1]]
`function`

[[2]]
[[2]]$x
[1] 1


[[3]]
{
    x^2
}

[[4]]
function(x = 1){x^2}

One thing I noticed was that the fourth element does store the format in which the function was entered.

> as.list(substitute(function(x = 1){
+ x^2})[[4]]
function(x = 1){
x^2}

What is the purpose of this fourth element in the parse tree? The only time I can see it being used is if you wanted to print a function verbatim, which you can already do by printing the function, e.g.

> f = function(x = 1){
+ x^2}
> f
function(x = 1){
x^2}
like image 913
Jon Claus Avatar asked Jun 11 '13 21:06

Jon Claus


1 Answers

Apparently this component is a source reference: it's not easily located in the R language definition, but its purpose is precisely for retaining the structure of the original source, especially comments; for example

s <- substitute(function(x=1){ 
      ## a comment
       x^2})
str(s[[4]])
##  Class 'srcref'  atomic [1:8] 1 21 2 15 21 15 1 2
##   ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x8a87634>

shows that it's a srcref object. The mysterious numbers (1, 21, 2, 15, ...) represent indices into a lower-level object representing the source code, as described in the ?srcfile page (i.e. c(first_line, first_byte, last_line, last_byte, first_column, last_column, first_parsed, last_parsed)). As @SimonO101 points out, there's an R Journal article by Duncan Murdoch that probably gives the best explanation.

like image 189
Ben Bolker Avatar answered Nov 14 '22 08:11

Ben Bolker