How to get the name of the calling function inside the called routine?
Thanks @GavinSimpson and @RicardoSporta, but I've figured it out. I'll post an answer in case somebody searches for this in SO.
The name of the function that generated the current call can be retrieved by
deparse(sys.calls()[[sys.nframe()-1]])
This returns a string that contains not only the name of the function, but the entire call object. The name alone can be retrieve by subsetting sys.calls()[[sys.nframe()-1]]
before deparsing.
I wanted this because I wrote a function that checks the arguments and halts execution in case of an error. But I wanted this function to (i) dump the environment and (ii) show name of the function one level above in the execution stack. (i) is easy, but I was stuck in (ii).
As for the second question in my post, this is what happens: the expression stop("invalid input")
is evaluated in the environment of the test
function, but this is not the same thing as if the expression was part of test
's body, because the execution stack is different in this 2 scenarios. In the latter case, stop
has only test
above it, but in the first, it has eval
, check
and then test
upwards. The execution stack, returned by sys.calls()
is not the same thing as the enclosing environments. This is what may cause confusion.
See ?match.call
. For example:
foo <- function() {
match.call()[[1]]
}
foo()
as.character(foo())
which produces
> foo()
foo
>
> as.character(foo())
[1] "foo"
A simplified version of your code is
check <- function(x) {
match.call()[[1]]
}
test <- function(y) {
check(y)
}
giving
> test(2)
check
> as.character(test(2))
[1] "check"
Note match.call()
works via the use of sys.call()
(actually it calls sys.call(sys.parent())
) when called as I did above with no arguments. So you may wish to consult ?sys.call
too.