Getting the parse tree for a predefined function in R

前端 未结 3 1950
[愿得一人]
[愿得一人] 2021-02-10 05:30

I feel as if this is a fairly basic question, but I can\'t figure it out.

If I define a function in R, how do I later use the name of the function to get its parse tree.

3条回答
  •  囚心锁ツ
    2021-02-10 06:07

    I'm not exactly sure which of these meets your desires:

     eval(f)
    #function(x){ x^2 }
    
     identical(eval(f), get("f"))
    #[1] TRUE
     identical(eval(f), substitute( function(x){ x^2 })  )
    #[1] FALSE
    
     deparse(f)
    #[1] "function (x) " "{"             "    x^2"       "}"            
    
     body(f)
    #------
    {
        x^2
    }
    #---------
    
    eval(parse(text=deparse(f)))
    #---------
    function (x) 
    {
        x^2
    }
    #-----------
    
     parse(text=deparse(f))
    #--------
    expression(function (x) 
    {
        x^2
    })
    #--------
    
     get("f")
    # function(x){ x^2 }
    

    The print representation may not display the full features of the values returned.

     class(substitute(function(x){ x^2 }) )
    #[1] "call"
     class( eval(f) ) 
    #[1] "function"
    

提交回复
热议问题