[R] function(...) {}

Gabor Grothendieck ggrothendieck at myway.com
Wed Aug 18 14:01:17 CEST 2004


Luis Rideau Cruz <Luisr <at> frs.fo> writes:

> 
> R-help
> 
> Assignments within functions are local and temporary,right?(function 1)
> If arguments to another function (function 2) are objects created in 
function 1 and need to call function
> 2,,,,,,,then it won't work,Am i right?
> I have nested the functions but still do not work.
> 
> How can this (it may be simple but I don' know how to get around this) be 
done??

Try this:

f1 <- function() {
   f2 <- function(x) assign(as.character(substitute(x)), x+1, parent.frame())
   z <- 1
   f2(z)
   z   # x has value 
}
f1() # 2

# Actually, you don't have to nest them since parent.frame refers to the
# environment of the caller:

f2 <- function(x) assign(as.character(substitute(x)), x+1, parent.frame())
f1 <- function() {
   z <- 1
   f2(z)
   z   # x has value 
}
f1() # 2




More information about the R-help mailing list