Learn R Programming

userfriendlyscience (version 0.7.2)

curfnfinder: Function to find the name of the calling function

Description

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

Usage

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.

Value

The current 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.

Examples

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

Run the code above in your browser using DataLab