[R] Referencing function name from within function

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 3 14:17:30 CEST 2007


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.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list