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}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With