Redundancy of Parse Trees functions in R

In R, I noticed that the parsing tree of the function operator seems redundant in the sense that its fourth element, apparently, always consists of the first three elements.

For instance,

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

I noticed that the fourth element stores the format in which the function was introduced.

 > 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 see it being used is if you want to print the function verbatim, which you can already do by printing the function, for example.

 > f = function(x = 1){ + x^2} > f function(x = 1){ x^2} 
+4
source share
2 answers

Apparently, this component is the source link: it is not easy to find in the definition of the R language , but its purpose is precisely to preserve the structure of the original source, especially comments; eg

 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 this is an srcref object. Mysterious numbers (1, 21, 2, 15, ...) represent indices in a lower-level object representing the source code, as described in ? Srcfile (i.e. c(first_line, first_byte, last_line, last_byte, first_column, last_column, first_parsed, last_parsed) ). As @ SimonO101 points out, there is an R Journal article by Duncan Murdoch that probably gives a better explanation.

+4
source

Some additional notes:

1) You can disable this function with options(keep.source=FALSE) (default is TRUE ):

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

In this list first element is the character that identifies the closure object, the second saves the formals , and the third saves the body . Please note that this last one is printed in the standard way. The fourth element would save the input text as indicated.

2) If you type function(x = 1){x^2} on the console, R calls print.function , which takes the useSource argument. By default, this is TRUE and causes R to simply repeat what is stored in the fourth element of the parsing list. Setting it to FALSE causes R to print the body function:

 > options(keep.source=TRUE) > f <- function(x = 1){x^2} > f function(x = 1){x^2} > print.function(f) function(x = 1){x^2} > print.function(f, useSource=FALSE) function (x = 1) { x^2 } 
+1
source

Source: https://habr.com/ru/post/1485689/


All Articles