This function finds and returns the name of the function calling it. This can be useful, for example, when generating functions algorithmically.

curfnfinder(skipframes = 0, skipnames = "(FUN)|(.+apply)|(replicate)",
            retIfNone = "Not in function", retStack = FALSE,
            extraPrefPerLevel = "\t")

Arguments

skipframes

Number of frames to skip; useful when called from an anonymous function.

skipnames

A regular expression specifying which substrings to delete.

retIfNone

What to return when called from outside a function.

retStack

Whether to return the entire stack or just one function.

extraPrefPerLevel

Extra prefixes to return for each level of the function.

Details

This function was written by Nick Sabbe for his package addendum. He posted it on Stack Exchange at http://stackoverflow.com/questions/7307987/logging-current-function-name and I included this here with this permission.

Value

The current function.

Examples

functionA <- functionB <- function() { curFn <- curfnfinder(); if (curFn == 'functionA') { cat('Doing something\n'); } else { cat('Doing something else\n'); } cat('Doing something generic.'); } functionA();
#> Doing something else #> Doing something generic.
functionB();
#> Doing something else #> Doing something generic.