Dear all, Why does not the three-dot accept arguments from the parent environment? I am just confused with this error, can someone give me a hint?> rm(list=ls()) > testFun <- function(a, ...)+ { + if(a){ + print(a) + }else + { + print(b) + } + }> > myTask <- function(a)+ { + b <- 3 + testFun(a, b = b) + }> myTask(FALSE)Error in print(b) : object 'b' not found Thanks in advance! Feng -- Feng Li Department of Statistics Stockholm University SE-106 91 Stockholm, Sweden http://feng.li/
Hi Feng, I'm afraid I don't entirely understansd your question -- the `...` construct only allows you to pass variable numbers of arguments, not to have arbitrary access to the parent frames. You need to manually extract "b" from the dots inside of testFun. Also, it's quite frowned upon to put ##rm(list = ls())## in your examples: it's the mailing list equivalent of asking a buddy to come over to help you move and then punching him in the face when he tries to lift your sofa. Cheers, Michael On Mon, Jan 14, 2013 at 10:21 AM, Feng Li <m at feng.li> wrote:> Dear all, > > Why does not the three-dot accept arguments from the parent environment? > I am just confused with this error, can someone give me a hint? > >> rm(list=ls()) >> testFun <- function(a, ...) > + { > + if(a){ > + print(a) > + }else > + { > + print(b) > + } > + } >> >> myTask <- function(a) > + { > + b <- 3 > + testFun(a, b = b) > + } >> myTask(FALSE) > Error in print(b) : object 'b' not found > > > Thanks in advance! > > Feng > > -- > Feng Li > Department of Statistics > Stockholm University > SE-106 91 Stockholm, Sweden > http://feng.li/ > > ______________________________________________ > 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.
Hi: If you want testFun to know about b, then you would have to do
b<-list(...)$b inside
TestFun itself. But the dot dot dot argument is not really for that
purpose.
The use of dotdotdot is for the case where a function INSIDE testFun has a
formal argument named say b. Then you can pass the ... at the top level and
the function inside will receive the ... but automatically slurp the b out
of the dot dot dot and know about it. Below is an example of the use I'm
talking about. It was created by a mentoR when I was trying to understand
the dotdotdot concept.
hope it helps you.
#=====================================================================
Note that f does not directly know about x and g does. That is g knows
about x even though f does
not know and g got it from f.
g <- function(x, ...) { cat("g: exists('x') = ",
exists("x"), "\n");
list(...) }
f <- function(...) { cat("f: exists('x') = ",
exists("x"), "\n"); g(...) }
f(x = 3, y = 4)
f: exists('x') = FALSE
g: exists('x') = TRUE
$y
On Mon, Jan 14, 2013 at 5:21 AM, Feng Li <m@feng.li> wrote:
> Dear all,
>
> Why does not the three-dot accept arguments from the parent environment?
> I am just confused with this error, can someone give me a hint?
>
> > rm(list=ls())
> > testFun <- function(a, ...)
> + {
> + if(a){
> + print(a)
> + }else
> + {
> + print(b)
> + }
> + }
> >
> > myTask <- function(a)
> + {
> + b <- 3
> + testFun(a, b = b)
> + }
> > myTask(FALSE)
> Error in print(b) : object 'b' not found
>
>
> Thanks in advance!
>
> Feng
>
> --
> Feng Li
> Department of Statistics
> Stockholm University
> SE-106 91 Stockholm, Sweden
> http://feng.li/
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]