[R] match.call to obtain the name of a function

Jacob Wegelin jacobwegelin at fastmail.fm
Fri Feb 26 21:22:12 CET 2010


Within a function I'd often like to obtain a text string equal to the name of the function.

One use for this: To generate a filename for use in pdf(). This enables me to keep track of which function generated a particular graphic came.

match.call() puts parentheses at the end of the name. I don't want parentheses in a filename.

The following kludgey function gives the desired result.

> JANK
function(x, y) {
    one<-deparse(match.call())
    functionName<-gsub("\\(.*", "", one, perl=T)
    cat("The name of this function is ", functionName, "\n")
}
> JANK(55, pi^2)
The name of this function is  JANK 
> JANK()
The name of this function is  JANK

Is there not a more direct way? To paraphrase Douglas Bates, the above approach is like the diner scene in the movie "Five Easy Pieces". You get an order of toast by first ordering a chicken sandwich and then telling the waitress to hold (that is, to subtract) the meat, lettuce, and mayonnaise.

Thanks for any insights

Jacob Wegelin



More information about the R-help mailing list