Dan Abner
2013-Oct-18 16:05 UTC
[R] Recovering object names when using the ... argument in a fn XXXX
Hi all, I am using the ... argument to parmeterize a user define fn to accept multiple input objects. I subsquently save all these data as a list. Question: what is the best way to recover or extract the original object names that were fed to the fn? Thanks, Dan [[alternative HTML version deleted]]
Bert Gunter
2013-Oct-18 16:26 UTC
[R] Recovering object names when using the ... argument in a fn XXXX
I'm not exactly sure what you mean by "names." Does the following meet your needs? f <- function(...)names(list(...))> f(a=2,b=3)[1] "a" "b"> f(a=2,3)[1] "a" "" If not, a reproducible example of what you want might be helpful. Cheers, Bert On Fri, Oct 18, 2013 at 9:05 AM, Dan Abner <dan.abner99 at gmail.com> wrote:> Hi all, > > I am using the ... argument to parmeterize a user define fn to accept > multiple input objects. I subsquently save all these data as a list. > Question: what is the best way to recover or extract the original object > names that were fed to the fn? > > Thanks, > > Dan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374
Bert Gunter
2013-Oct-18 17:07 UTC
[R] Recovering object names when using the ... argument in a fn XXXX
1. Always cc to the list unless it is truly a private offlist reply. This is to get help from a wider audience, as may well be required here. Translation: Take my "solution" with a grain of salt. It is fragile at best. 2. I think ?match.call and ?deparse are what you're looking for: f <- function(...){ deparse(match.call()) }> g(a,b,sqrt(c(1,2,5)))[1] "a" "b" "sqrt(c(1, 2, 5))" Cheers, Bert On Fri, Oct 18, 2013 at 9:36 AM, Dan Abner <dan.abner99 at gmail.com> wrote:> Hi Bert, > > Thanks for your response. Please see example below: > > > d1<-data.frame(x1=runif(100),x2=runif(100)) > d2<-data.frame(x3=runif(100),x4=runif(100)) > d3<-data.frame(x5=runif(100),x6=runif(100)) > set1 <- function(...,by){ > df.name <<- list(...) > name1<<-names(list(...)) > } > > set1(d1,d2,d3) > > I need to be able to recover whatever input data frame names that the user > passes to set1() (preferably in the character vector). > >> name1 > NULL > > Here is another possible call to the fn: > > set1(d1,d2) > > > > > > > On Fri, Oct 18, 2013 at 12:26 PM, Bert Gunter <gunter.berton at gene.com> > wrote: >> >> I'm not exactly sure what you mean by "names." Does the following meet >> your needs? >> >> f <- function(...)names(list(...)) >> >> > f(a=2,b=3) >> [1] "a" "b" >> > f(a=2,3) >> [1] "a" "" >> >> If not, a reproducible example of what you want might be helpful. >> >> Cheers, >> Bert >> >> >> >> >> On Fri, Oct 18, 2013 at 9:05 AM, Dan Abner <dan.abner99 at gmail.com> wrote: >> > Hi all, >> > >> > I am using the ... argument to parmeterize a user define fn to accept >> > multiple input objects. I subsquently save all these data as a list. >> > Question: what is the best way to recover or extract the original object >> > names that were fed to the fn? >> > >> > Thanks, >> > >> > Dan >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > 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. >> >> >> >> -- >> >> Bert Gunter >> Genentech Nonclinical Biostatistics >> >> (650) 467-7374 > >-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374
William Dunlap
2013-Oct-18 17:27 UTC
[R] Recovering object names when using the ... argument in a fn XXXX
> I am using the ... argument to parmeterize a user define fn to accept > multiple input objects. I subsquently save all these data as a list. > Question: what is the best way to recover or extract the original object > names that were fed to the fn?The following function, ellipsisInfo, returns character strings representing the actual arguments to the function. If the function was called with tags on the arguments, as in ellipsisInfo(tag=argument), it makes those tags the names on the returned character vector. It does not evaluate the ... arguments, so you don't run into problems with evaluating arguments too soon or evaluating ones that should not be evaluated most of the time. ellipsisInfo <- function(...) { # get the unevaluated expressions given as arguments unevaluatedArgs <- substitute(...()) # convert those expressions to text (truncate to single line) unevaluatedArgsAsText <- vapply(unevaluatedArgs, function(a)deparse(a)[1], "") unevaluatedArgsAsText } E.g.,> i <- ellipsisInfo(x, log(10), e=exp(1), onProblem=stop("there was a problem")) > i"x" "log(10)" e "exp(1)" onProblem "stop(\"there was a problem\")"> ifelse(names(i)=="", i, names(i)) # use tag if supplied, otherwise argument itself[1] "x" "log(10)" "e" [4] "onProblem" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Dan Abner > Sent: Friday, October 18, 2013 9:06 AM > To: r-help at r-project.org > Subject: [R] Recovering object names when using the ... argument in a fn XXXX > > Hi all, > > I am using the ... argument to parmeterize a user define fn to accept > multiple input objects. I subsquently save all these data as a list. > Question: what is the best way to recover or extract the original object > names that were fed to the fn? > > Thanks, > > Dan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.