18.9 Function position

  • The first element of the call object is the function position. This contains the function that will be called when the object is evaluated, and is usually a symbol.
lobstr::ast(foo())
#> █─foo
  • While R allows you to surround the name of the function with quotes, the parser converts it to a symbol:
lobstr::ast("foo"())
#> █─foo
  • However, sometimes the function doesn’t exist in the current environment and you need to do some computation to retrieve it:
    • For example, if the function is in another package, is a method of an R6 object, or is created by a function factory. In this case, the function position will be occupied by another call:
lobstr::ast(pkg::foo(1))
#> █─█─`::` 
#> │ ├─pkg 
#> │ └─foo 
#> └─1
lobstr::ast(obj$foo(1))
#> █─█─`$` 
#> │ ├─obj 
#> │ └─foo 
#> └─1
lobstr::ast(foo(1)(2))
#> █─█─foo 
#> │ └─1 
#> └─2