Search code examples
rpryr

How can I get the function object from a call object


I am trying to retrieve a function object from a call object In this example

ff = function(x) {gg(x)}     
gg = function(y) {uu(y)}     
uu = function(z) {browser()} 
ff(1)                        

Say I want to get the function ff from sys.calls()[[1]] I got the below but I want the ff function object, how can I get it

Browse[1]> deparse(sys.calls()[[1]][1])
[1] "ff()"                             

Solution

  • You can extract the symbol by converting the call to a list and subsetting its first member, which is the symbol ff. You can then eval this symbol to show the function body (or use it to build a new call)

    Browse[1]> eval(as.list(sys.calls()[[1]])[[1]])
    #> function(x) {gg(x)}