[R] Recovering object names when using the ... argument in a fn XXXX

Bert Gunter gunter.berton at gene.com
Fri Oct 18 19:25:00 CEST 2013


That's because I screwed up! I gave you the wrong function, "f" instead of "g" .

Here's g:

g <- function(...){
  sapply(as.list(match.call())[-1],deparse)
}

and the example should now work.

Sheepishly,

-Bert


On Fri, Oct 18, 2013 at 10:21 AM, Dan Abner <dan.abner99 at gmail.com> wrote:
> Hi Bert,
>
> Thank you for the code.
>
> However, I don't see what I am doing different, but my output is different.
> I would much rather have output similar to yours where only the input
> objects are returned (instead of the fn name, the encapsulating parentheses,
> etc.):
>
>
>> 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<<-deparse(match.call())
> + }
>>
>> set1(d1,d2,d3)
>>
>> name1
> [1] "set1(d1, d2, d3)"
>>
> Another application (with example data too large to email) gives:
>
>> name1
> [1] "matx.set(df1, df2, df3, df4, df5, df6, by = \"ID\")"
>
> Is there a simple way to just get:
>
> "df1" "df2" "df3" "df4" "df5" "df6"
>
> Thanks!
>
> Dan
>
>
>
> On Fri, Oct 18, 2013 at 1:07 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>>
>> 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
>
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374



More information about the R-help mailing list