expression - What is "{" class in R? -
here code:
mf = function(..., expr) { expr = substitute(expr) print(class(expr)) print(str(expr)) expr } mf(a = 1, b = 2, expr = {matrix(na, 4, 4)})
output:
[1] "{" length 2 { matrix(na, 4, 4) } - attr(*, "srcref")=list of 2 ..$ :class 'srcref' atomic [1:8] 1 25 1 25 25 25 1 1 .. .. ..- attr(*, "srcfile")=classes 'srcfilecopy', 'srcfile' <environment: 0x7fbcdbce3860> ..$ :class 'srcref' atomic [1:8] 1 26 1 41 26 41 1 1 .. .. ..- attr(*, "srcfile")=classes 'srcfilecopy', 'srcfile' <environment: 0x7fbcdbce3860> - attr(*, "srcfile")=classes 'srcfilecopy', 'srcfile' <environment: 0x7fbcdbce3860> - attr(*, "wholesrcref")=class 'srcref' atomic [1:8] 1 0 1 42 0 42 1 1 .. ..- attr(*, "srcfile")=classes 'srcfilecopy', 'srcfile' <environment: 0x7fbcdbce3860> null { matrix(na, 4, 4) }
apparently result of substitute(expr)
produces of class "{". class exactly? why {matrix(na, 4, 4)}
of length 2? these strange attrs mean?
the {
class block of code. looking @ classes, note difference between these
mf(a = 1, b = 2, expr = {matrix(na, 4, 4)}) # [1] "{" mf(a = 1, b = 2, expr = matrix(na, 4, 4)) # [1] "call"
a class of {
can hold multiple statements. length()
indicates how many statements in block (including start of block). example
length(quote({matrix(na, 4, 4)})) # [1] 2 length(quote({matrix(na, 4, 4); matrix(na,3,3)})) # [1] 3 length(quote({})) # [1] 1
the attributes "srcref" , "srcfile" how r tracks functions defined trying give informative error messages. can see ?srcfile
page more information that.
Comments
Post a Comment