什么是“{”在R类中 [英] What is "{" class in R?
问题描述
这是代码:
mf = function(..., expr) {
expr = substitute(expr)
print(class(expr))
print(str(expr))
expr
}
mf(a = 1, b = 2, expr = {matrix(NA, 4, 4)})
输出:
[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)
}
显然, substitute(expr)
的结果产生类{。这个班级是什么?为什么长度为2的 {matrix(NA,4,4)}
这些奇怪的attrs是什么意思?
Apparently the result of substitute(expr)
produces something of the class "{". What is this class exactly? Why is {matrix(NA, 4, 4)}
of length 2? What do these strange attrs mean?
推荐答案
{
一块代码。只需看这些课程,请注意这些
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"
一类 {
可以容纳多个语句。 length()
表示块中有多少个语句(包括块的开始)。例如
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
属性srcref和srcfile是如何R轨迹,其功能定义为试图提供有用的错误消息。您可以看到?srcfile
帮助页面了解更多信息。
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.
这篇关于什么是“{”在R类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!