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 -- View this message in context: http://www.nabble.com/Access-to-values-of-function-arguments-tp18554602p18554602.html Sent from the R help mailing list archive at Nabble.com.
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
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. > WillemfMyFun <- 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
Dear Mike and Brian, Thank you very much, this solves my immediate problem. Thanks for your time very much. I will do the reading on those two functions. I have looked for books that deal with the more programmatic aspects of R instead of the statistical side (e.g. dealing with R objects, manipulating environments, lists in R and the like). I have not seen a huge lot. The books that I have seen that deal with "Programming in R" are actually statistically-focused and, as far as I could find out, do not really cover the programmatic side. Any obvious suggestions for resources that I might have missed? I have several books on R but R programming (as opposed to performing statistical/graphical/mathematical manipulation) is one aspect that I am getting into now and for which I do not have a good reference. The language ref on CRAN is quite helpful but not a good tutorial at all. Kind regards, Willem -- View this message in context: http://www.nabble.com/Access-to-values-of-function-arguments-tp18554602p18557568.html Sent from the R help mailing list archive at Nabble.com.
Check out "S Programming" by Venables & Ripley. On Sun, Jul 20, 2008 at 3:02 PM, willemf <jwhferguson at zoology.up.ac.za> wrote:> > Dear Mike and Brian, > > Thank you very much, this solves my immediate problem. Thanks for your time > very much. I will do the reading on those two functions. > > I have looked for books that deal with the more programmatic aspects of R > instead of the statistical side (e.g. dealing with R objects, manipulating > environments, lists in R and the like). I have not seen a huge lot. The > books that I have seen that deal with "Programming in R" are actually > statistically-focused and, as far as I could find out, do not really cover > the programmatic side. Any obvious suggestions for resources that I might > have missed? I have several books on R but R programming (as opposed to > performing statistical/graphical/mathematical manipulation) is one aspect > that I am getting into now and for which I do not have a good reference. The > language ref on CRAN is quite helpful but not a good tutorial at all. > > Kind regards, > Willem > > -- > View this message in context: http://www.nabble.com/Access-to-values-of-function-arguments-tp18554602p18557568.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?