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.> JANKfunction(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
Henrique Dallazuanna
2010-Feb-26 20:40 UTC
[R] match.call to obtain the name of a function
Try this: foo <- function() sprintf("The name of this function is %s", gettext(match.call())) On Fri, Feb 26, 2010 at 5:22 PM, Jacob Wegelin <jacobwegelin at fastmail.fm> wrote:> > 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O