什么/函数对象的属性在哪里?

Fer*_*aft 17 r function

通过在R中使用函数,我发现它有更多的方面,而不是眼睛.

考虑一下简单的函数赋值,直接在控制台中输入:

f <- function(x)x^2
Run Code Online (Sandbox Code Playgroud)

f广义上讲,通常的"属性" 是(i)正式论证清单,(ii)身体表达和(iii)将成为功能评估框架的环境.可通过以下方式访问:

> formals(f)
$x
> body(f)
x^2
> environment(f)
<environment: R_GlobalEnv>
Run Code Online (Sandbox Code Playgroud)

此外,str返回更多信息附加到f:

> str(f)
function (x)  
 - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 6 1 19 6 19 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000000145a3cc8>
Run Code Online (Sandbox Code Playgroud)

让我们试着联系到他们:

> attributes(f)
$srcref
function(x)x^2
Run Code Online (Sandbox Code Playgroud)

这是作为文本打印,但它存储为数字向量:

> c(attributes(f)$srcref)
[1]  1  6  1 19  6 19  1  1
Run Code Online (Sandbox Code Playgroud)

而且这个对象也有自己的属性:

> attributes(attributes(f)$srcref)
$srcfile


$class
[1] "srcref"
Run Code Online (Sandbox Code Playgroud)

第一个是环境,有3个内部对象:

> mode(attributes(attributes(f)$srcref)$srcfile)
[1] "environment"
> ls(attributes(attributes(f)$srcref)$srcfile)
[1] "filename"      "fixedNewlines" "lines" 
> attributes(attributes(f)$srcref)$srcfile$filename
[1] ""
> attributes(attributes(f)$srcref)$srcfile$fixedNewlines
[1] TRUE
> attributes(attributes(f)$srcref)$srcfile$lines
[1] "f <- function(x)x^2" ""
Run Code Online (Sandbox Code Playgroud)

你在这!这是R用于打印的字符串attributes(f)$srcref.

所以问题是:

  1. 是否有其他对象链接到f?如果是这样,如何联系到他们?

  2. 如果我们剥离f它的属性,使用attributes(f) <- NULL它似乎不会影响函数.这样做有什么缺点吗?

Jos*_*ien 13

据我所知,srcref是唯一通常附加到S3功能的属性.(S4功能是另一回事,我不建议弄乱他们有时很多的属性).

srcref属性用于启用函数源代码中包含的注释打印,以及(对于源自文件的函数),通过行号设置断点,使用utils::findLineNum()utils::setBreakpoint().

如果您不希望您的功能携带这样的额外行李,您可以srcref通过执行来关闭录制options(keep.source=FALSE).从?options(也记录相关keep.source.pkgs选项):

'keep.source':当'TRUE'时,函数的源代码(新定义或加载)存储在内部,允许将注释保存在正确的位置.通过打印或使用'deparse(fn,control ="useSource")'来检索源.

相比:

options(keep.source=TRUE)
f1 <- function(x) {
    ## This function is needlessly commented
    x
}

options(keep.source=FALSE)
f2 <- function(x) {
    ## This one is too
    x
}

length(attributes(f1))
# [1] 1
f1
# function(x) {
#     ## This function is needlessly commented
#     x
# }

length(attributes(f2))
# [1] 0
f2
# function (x) 
# {
#     x
# }
Run Code Online (Sandbox Code Playgroud)