什么/哪里是一个函数对象的属性? [英] What/Where are the attributes of a function object?

查看:105
本文介绍了什么/哪里是一个函数对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



考虑简单的函数分配,直接输入控制台:

  f < -  function(x)x ^ 2 

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

 >正式(f)
$ x
>身体(f)
x ^ 2
>环境(f)
< environment:R_GlobalEnv>

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

 > str(f)
函数(x)
- attr(*,srcref)=类'srcref'atomic [1:8] 1 6 1 19 6 19 1 1
.. ..- attr(*,srcfile)=类的srcfilecopy','srcfile'< environment:0x00000000145a3cc8>

我们试着找到它们:

 >属性(f)
$ srcref
函数(x)x ^ 2

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

 > c(属性(f)$ srcref)
[1] 1 6 1 19 6 19 1 1

此对象也有其自己的属性:

 >属性(属性(f)$ srcref)
$ srcfile


$ class
[1]srcref


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

 >模式(属性(属性(f)$ srcref)$ srcfile)
[1]environment
> ls(attributes(attributes(f)$ srcref)$ srcfile)
[1]filenamefixedNewlineslines
>属性(属性(f)$ srcref)$ srcfile $文件名
[1]
>属性(属性(f)$ srcref)$ srcfile $ fixedNewlines
[1] TRUE
>属性(属性(f)$ srcref)$ srcfile $行
[1]f < - function(x)x ^ 2

你有!这是R打印属性(f)$ srcref



所使用的字符串。 / p>


  1. 链接到 f 其他 C>?如果是这样,如何到达它们?

  2. 如果我们剥离其属性的 f ,使用 attributes(f)< - NULL ,它似乎不会影响函数。有没有这样做的缺点? 据我所知, srcref 是通常附加到S3函数的唯一属性。 (S4函数是另一回事,我不会推荐搞乱它们有时候很多的属性)。
    $ b $ srcref 属性用于像打印包含在函数源代码中的注释,以及(用于从文件中获取的函数)用于通过行号设置断点的东西,使用 utils :: findLineNum () utils :: setBreakpoint()



    如果你不希望你的函数携带这些额外的行李,你可以通过做选项(keep.source = FALSE)来关闭 srcref 的记录。从?options (它也记录相关的 keep.source.pkgs 选项):


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

    比较:

      options(keep.source = TRUE)
    f1 < - function(x){
    ##这个函数被不必要地评论
    x
    }

    options(keep.source = FALSE)
    f2 < - function(x){
    ## This一个是
    x
    }

    长度(属性(f1))
    #[1] 1
    f1
    #函数(x) {
    ###这个函数被不必要地评论
    #x
    #}

    length(属性(f2))
    #[1] 0
    f2
    #function(x)
    #{
    #x
    #}


    By playing around with a function in R, I found out there are more aspects to it than meets the eye.

    Consider ths simple function assignment, typed directly in the console:

    f <- function(x)x^2
    

    The usual "attributes" of f, in a broad sense, are (i) the list of formal arguments, (ii) the body expression and (iii) the environment that will be the enclosure of the function evaluation frame. They are accessible via:

    > formals(f)
    $x
    > body(f)
    x^2
    > environment(f)
    <environment: R_GlobalEnv>
    

    Moreover, str returns more info attached to 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>
    

    Let's try to reach them:

    > attributes(f)
    $srcref
    function(x)x^2
    

    This is being printed as a text, but it's stored as a numeric vector:

    > c(attributes(f)$srcref)
    [1]  1  6  1 19  6 19  1  1
    

    And this object also has its own attributes:

    > attributes(attributes(f)$srcref)
    $srcfile
    
    
    $class
    [1] "srcref"
    

    The first one is an environment, with 3 internal objects:

    > 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" ""
    

    There you are! This is the string used by R to print attributes(f)$srcref.

    So the questions are:

    1. Are there any other objects linked to f? If so, how to reach them?

    2. If we strip f of its attributes, using attributes(f) <- NULL, it doesn't seem to affect the function. Are there any drawbacks of doing this?

    解决方案

    As far as I know, srcref is the only attribute typically attached to S3 functions. (S4 functions are a different matter, and I wouldn't recommend messing with their sometimes numerous attributes).

    The srcref attribute is used for things like enabling printing of comments included in a function's source code, and (for functions that have been sourced in from a file) for setting breakpoints by line number, using utils::findLineNum() and utils::setBreakpoint().

    If you don't want your functions to carry such additional baggage, you can turn off recording of srcref by doing options(keep.source=FALSE). From ?options (which also documents the related keep.source.pkgs option):

    ‘keep.source’: When ‘TRUE’, the source code for functions (newly defined or loaded) is stored internally allowing comments to be kept in the right places. Retrieve the source by printing or using ‘deparse(fn, control = "useSource")’.

    Compare:

    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
    # }
    

    这篇关于什么/哪里是一个函数对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆