Johannes Graumann
2007-Apr-03 08:33 UTC
[R] Referencing function name from within function
Hello, For verbose coding I'd like to do something like:> myfunction <- function(x){ > if (a){ > stop(paste(myfunction_name_here,"requires xyz!") > }Is that possible? Thanks for any hints, Joh
rolf at math.unb.ca
2007-Apr-03 11:40 UTC
[R] Referencing function name from within function
I dunno much about such things, but a wee experiment seems to indicate that the following structure does what you want: myfunction <- function(x){ nm <- as.character(match.call())[1] a <- TRUE if (a){ stop(paste(nm,"requires xyz!")) } } cheers, Rolf Turner rolf at math.unb.ca ===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+==Original message:> For verbose coding I'd like to do something like: > > myfunction <- function(x){ > > if (a){ > > stop(paste(myfunction_name_here,"requires xyz!") > > } > Is that possible?(Note: In R *all* things are possible!)
Try this:> myfunction <- function(){+ # some calculations + # now get my name + .caller <- sys.call() + cat(paste(as.character(.caller[[length(.caller)]]),"needs 'xyz'\n")) + }> myfunction()myfunction needs 'xyz'> >On 4/3/07, Johannes Graumann <johannes_graumann@web.de> wrote:> > Hello, > > For verbose coding I'd like to do something like: > > myfunction <- function(x){ > > if (a){ > > stop(paste(myfunction_name_here,"requires xyz!") > > } > Is that possible? > > Thanks for any hints, Joh > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
rolf at math.unb.ca
2007-Apr-03 16:18 UTC
[R] Referencing function name from within function
Wott about this then? myfunction <- function(x){ temp <- sys.calls()[[1]] nm <- temp[length(temp)] a <- TRUE if (a){ stop(paste(nm,"requires xyz!")) } }> myfunction()Error in myfunction() : myfunction requires xyz!> lapply(1:10,myfunction)Error in FUN(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)[[1]], ...) : myfunction requires xyz! cheers, Rolf Turner rolf at math.unb.ca ===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+==Prof Brian Ripley wrote:> This presumes a function is always called by name. Try > > > lapply(1:10, myfunction) > Error in FUN(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)[[1]], ...) : > FUN requires xyz! > > to see a (simple case of) the problem. > > On Tue, 3 Apr 2007, rolf at math.unb.ca wrote: > > > I dunno much about such things, but a wee experiment seems to > > indicate that the following structure does what you want: > > > > myfunction <- function(x){ > > nm <- as.character(match.call())[1] > > a <- TRUE > > if (a){ > > stop(paste(nm,"requires xyz!")) > > } > > } > > > > cheers, > > > > Rolf Turner > > rolf at math.unb.ca > > > > ===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+===+==> > Original message: > > > >> For verbose coding I'd like to do something like: > >>> myfunction <- function(x){ > >>> if (a){ > >>> stop(paste(myfunction_name_here,"requires xyz!") > >>> } > >> Is that possible? > > > > (Note: In R *all* things are possible!) > > I don't believe so.