get_fun_name: Return the name of the current function or a calling function...

Description Usage Arguments Value See Also Examples

View source: R/get_fun_name.r

Description

Return the name of the function that has been called n levels up from a given function's body. This function is intended to be called only within a function.

Usage

1
get_fun_name(n = 0)

Arguments

n

number of levels to go up in the calling chain in search of the calling function name. Defaults to n=0, meaning that the name returned is the name of the function that calls get_fun_name.

Value

A string containing the name of the function that has been called n levels up from the function calling get_env_name. The function name is returned without context, that is the enclosing environment of the function is not part of the returned value. (e.g. if the function is env1$f or env1$env2$f only "f" will be returned).

See Also

get_fun_calling to retrieve the name of the function with its context (e.g. "env1$f").

Examples

1
2
3
4
5
6
7
8
9
# Show the name of the active function
f <- function() { cat("We are in function:", get_fun_name(), "\n") }
f()

# Show the name of the calling function
f <- function(x) { cat("Calling function name is:", get_fun_name(1), "\n") }
env1 <- new.env()
with(env1, g <- function() { f(3) })
env1$g()

envnames documentation built on Dec. 8, 2020, 9:07 a.m.