[R] Access to values of function arguments

Marc Schwartz marc_schwartz at comcast.net
Sun Jul 20 18:04:57 CEST 2008


on 07/20/2008 08:42 AM 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?)
> 
> I have a function definition:
> 
> 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


MyFun <- function(a) {
            cat("Anova for variable", deparse(substitute(a)), "\n")}

Age <- 50:60

 > MyFun(Age)
Anova for variable Age


The deparse(substitute(...)) idiom is what you are looking for.

See ?deparse and ?substitute

HTH,

Marc Schwartz



More information about the R-help mailing list