[R] Trouble passing list or non-list to function using ...

Gabor Grothendieck ggrothendieck at gmail.com
Sat May 27 14:07:30 CEST 2006


There was some copying/pasting error and some junk got in.  Here
it is again:

testme <- function(...) {
   dotlist <- list(...)
   names(dotlist) <- sapply(names(dotlist),
      function(x) if (exists(x)) as.character(get(x, envir = parent.frame()))
         else x)
      dotlist
}

if (exists("nm")) rm(nm)
testme(nm = NA) # list(nm = NA)
nm <- "age"
testme(nm = NA) # list(age = NA)



On 5/27/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Is this what you want:
> For each component of the dotlist check whether its name exists in
> the caller's environment as an R object and if it exists then use the
> contents of that object coerced to character as the name of the
> component; otherwise, use the component name as given.
>
> If that's it then try this:
>
> testme <- function(...) {
>   dotlist <- list(...)
>   names(dotlist) <- sapply(names(dotlist),
>      function(x) if (exists(x)) as.character(get(x, envir = parent.frame()))
>         else x)
>      dotlist
> },
> envir = baseenv())
>
> if (exists("nm")) rm(nm)
> testme(nm = NA) # list(nm = NA)
> nm <- "age"
> testme(nm = NA) # list(age = NA)
>
>
>
> On 5/27/06, Jason Barnhart <jasoncbarnhart at msn.com> wrote:
> > Sorry about that. I'm trying to pass a "tag=value" argument to a function
> > which creates a list using list(...).
> > Specifically, it's necessary for "tag" to become the name of the list
> > component - I'm not sure I'm using the right nomenclature.
> >
> > I can make the call work explicitly here.  But I would like to loop through
> > a list and pass list values to the function instead.
> >
> >    testme<-function(...){
> >        dotlist<-list(...)
> >        dotlist
> >    }
> >
> > Here's the desired output.
> >     > testme(age=NA)
> >         $age
> >         [1] NA
> >
> > Unfortunately, this doesn't work.
> >    nm<-age
> >    testme(nm=NA)
> >     $nm
> >     [1] NA
> >
> > Please let me know if further clarification is needed.
> > I've tried various ways to pass the 'tag=value'; e.g. using list(nm=NA) and
> > so on, with no success
> > Thanks again,
> > -jason
> >
> >
> > ----- Original Message -----
> > From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
> > To: "Jason Barnhart" <jasoncbarnhart at msn.com>
> > Cc: <r-help at stat.math.ethz.ch>
> > Sent: Friday, May 26, 2006 6:04 PM
> > Subject: Re: [R] Trouble passing list or non-list to function using ...
> >
> >
> > Its not clear from your post which of the tests does not give you
> > the desired output and what the desired output is in that case.
> >
> > Just guessing but maybe you meant to use testlist[[i]] instead of
> > testlist[i] in
> > the loop?
> >
> >
> > On 5/26/06, Jason Barnhart <jasoncbarnhart at msn.com> wrote:
> > > Hello,
> > >
> > > Simply put, I'm trying to call a function "testme" with value "age=NA".
> > > I wish to use "dotlist<-list(...)"  inside the function and have dotlist
> > > become:
> > > $age
> > > [1] NA
> > >
> > > I'm modifying existing code and need to minimize changing that code so
> > > it's easiest to conform
> > > how I call the existing function.
> > >
> > > My sample code fragment, results, and R.version information are listed
> > > below.   I've been searching
> > > for prior questions regarding this phenomena, but haven't quite figured
> > > out how to do this.
> > >
> > > Thank you in advance,
> > > -jason
> > >
> > >
> > >
> > > ##BEGIN TEST CODE
> > > ###########################################################
> > > testme<-function(...){
> > >
> > > dotlist<-list(...)
> > >
> > >    dotlist
> > >
> > > }
> > >
> > > nm<-"age"
> > >
> > > testme(age=NA)
> > >
> > > testme(nm=NA)
> > >
> > > testme(age=NA,age2=NA)
> > >
> > > testlist<-list("age","age2")
> > >
> > > for (i in 1:length(testlist)){
> > >
> > >    print(testme(testlist[i]))
> > >
> > > }
> > >
> > > ##END TEST CODE
> > > #############################################################
> > >
> > > ##BEGIN RESULTS
> > > #############################################################
> > > > testme<-function(...){
> > > +
> > > + dotlist<-list(...)
> > > +
> > > +     dotlist
> > > +
> > > + }
> > > >
> > > > nm<-"age"
> > > >
> > > > testme(age=NA)   *****This is what I'm really after****
> > > $age
> > > [1] NA
> > >
> > > >
> > > > testme(nm=NA)
> > > $nm
> > > [1] NA
> > >
> > > > #trying w/ 2 vars
> > > > testme(age=NA,age2=NA)
> > > $age
> > > [1] NA
> > >
> > > $age2
> > > [1] NA
> > >
> > > >
> > > > testlist<-list("age","age2")
> > > >
> > > > for (i in 1:length(testlist)){
> > > +
> > > +     print(testme(testlist[i]))
> > > +
> > > + }
> > > [[1]]
> > > [[1]][[1]]
> > > [1] "age"
> > >
> > >
> > > [[1]]
> > > [[1]][[1]]
> > > [1] "age2"
> > >
> > > ##END RESULTS
> > > ###############################################################
> > >
> > > platform       i386-pc-mingw32
> > > arch           i386
> > > os             mingw32
> > > system         i386, mingw32
> > > status
> > > major          2
> > > minor          3.0
> > > year           2006
> > > month          04
> > > day            24
> > > svn rev        37909
> > > language       R
> > > version.string Version 2.3.0 (2006-04-24)
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > 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
> > >
> >
>



More information about the R-help mailing list