Folks, I've entered into an R programming territory I'm not very familiar with, thus this probably very elementary question concerning the mechanic of a function call. I want to know from within a function the name of the variables I pass down. The function makes use of the "..." to allow for multiple unknown arguments, myfun = function(...) { do something } In the body I put, { nm <- names(list(...)) nm } When the function is called with two vectors x, and y myfun(x, y) It returns NULL. However, when the call made is,>myfun(x=x, y=y)The result is [1] "x" "y" Question : how do i get the names of the unknown variables without explicitly saying x=x... Thanks in advance. Horace
Marc Schwartz
2007-Jul-30 12:39 UTC
[R] getting the name of variables passed to a function
On Fri, 2007-07-27 at 09:52 -0700, Horace Tso wrote:> Folks, > > I've entered into an R programming territory I'm not very familiar > with, thus this probably very elementary question concerning the > mechanic of a function call. > > I want to know from within a function the name of the variables I pass > down. The function makes use of the "..." to allow for multiple > unknown arguments, > > myfun = function(...) { do something } > > In the body I put, > > { > nm <- names(list(...)) > nm > } > > When the function is called with two vectors x, and y > > myfun(x, y) > > It returns NULL. However, when the call made is, > > >myfun(x=x, y=y) > > The result is > [1] "x" "y" > > Question : how do i get the names of the unknown variables without > explicitly saying x=x... > > Thanks in advance. > > HoraceSee ?match.call and take note of the 'expand.dots' argument, which defaults to TRUE. DotsFun <- function(...) as.character(match.call())[-1] x <- 1:10 y <- 5:6 > DotsFun(x, y) [1] "x" "y" match.call() returns the full function call. In the above, we take that result, coerce it to a character vector and remove the first element, which is the function being called, thus leaving the arguments. HTH, Marc Schwartz
Prof Brian Ripley
2007-Jul-30 12:39 UTC
[R] getting the name of variables passed to a function
I would start from match.call(expand.dots=TRUE) which has done the hard work for you. On Fri, 27 Jul 2007, Horace Tso wrote:> Folks, > > I've entered into an R programming territory I'm not very familiar with, > thus this probably very elementary question concerning the mechanic of a > function call. > > I want to know from within a function the name of the variables I pass > down. The function makes use of the "..." to allow for multiple unknown > arguments, > > myfun = function(...) { do something } > > In the body I put, > > { > nm <- names(list(...)) > nm > } > > When the function is called with two vectors x, and y > > myfun(x, y) > > It returns NULL. However, when the call made is, > >> myfun(x=x, y=y) > > The result is > [1] "x" "y" > > Question : how do i get the names of the unknown variables without > explicitly saying x=x... > > Thanks in advance. > > Horace > > ______________________________________________ > R-help at 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. >-- 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
Here's one possibility: R> f <- function(...) { call <- match.call(); sapply(as.list(call[-1]), deparse) } R> f(x, y) [1] "x" "y" R> f(x=x, y=y) x y "x" "y" You basically need to know how to manipulate call objects. The relevant section in the R Language Definition should help. Andy From: Horace Tso> > Folks, > > I've entered into an R programming territory I'm not very > familiar with, thus this probably very elementary question > concerning the mechanic of a function call. > > I want to know from within a function the name of the > variables I pass down. The function makes use of the "..." to > allow for multiple unknown arguments, > > myfun = function(...) { do something } > > In the body I put, > > { > nm <- names(list(...)) > nm > } > > When the function is called with two vectors x, and y > > myfun(x, y) > > It returns NULL. However, when the call made is, > > >myfun(x=x, y=y) > > The result is > [1] "x" "y" > > Question : how do i get the names of the unknown variables > without explicitly saying x=x... > > Thanks in advance. > > Horace > > ______________________________________________ > R-help at 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. > > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}