What is “{” class in R?

前端 未结 2 1783
渐次进展
渐次进展 2020-12-03 15:15

Here is the code:

mf = function(..., expr) {
    expr = substitute(expr)
    print(class(expr))
    print(str(expr))
    expr
}
mf(a = 1, b = 2, expr = {matr         


        
2条回答
  •  暖寄归人
    2020-12-03 15:49

    The { is the class for a block of code. Just looking at the classes, note the 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. The length() indicates how many statements are in the block (including the start of the block). For 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" and "srcfile" are how R tracks where functions are defined for trying to give informative error messages. You can see the ?srcfile help page for more information about that.

提交回复
热议问题