[R] Access to values of function arguments

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Jul 20 18:04:22 CEST 2008


See ?deparse .  E.g. (with several stylistic improvements)

func <- function(a)
     cat(sprintf("Anova for variable %s\n",
         sQuote(deparse(substitute(a)))))
func(Age)

Note that you expliciltly do not want the *vaiue* of 'a', but the symbol 
passed.  That is non-standard behaviour, and you might want to consider 
why func("Age") is not a better way to do this.

On Sun, 20 Jul 2008, willemf wrote:

>
> Does anyone know of good reading material about the following? The R language
> definition does not appear to explicitly address my problem (maybe I misread
> that document?)

'S Programming' (see the R FAQ).

> I have a function definition:

Well, that is not a function definition: please do use correct and 
reproducible examples as the posting guide asks.

>
> func(a)
>  cat("Anova for variable ",a)
>
> What I wish to achieve is to call func with a value such as:
> func(Age)
>
> and then obtain:
>
> Anova for variable Age
>
> Using "names(formals())" inside function func yields "a". That is not what I
> need. I need the name contained in a, which in this case is Age.
>
> Thanks for your time.
> Willemf


-- 
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