2
votes

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()"                             
2

2 Answers

3
votes

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)}
2
votes

We can also use get after deparseing

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