2

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()"                             
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
statquant
  • 13,672
  • 21
  • 91
  • 162

2 Answers2

3

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)}
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
2

We can also use get after deparseing

Browse[1]> get(deparse(as.list(sys.calls()[[1]][1])[[1]]))
#function(x) {gg(x)}
akrun
  • 874,273
  • 37
  • 540
  • 662