Dear R-helpers, I try to use nested R-functions as follows: help1 = function(){ x = 1 help2() } with help2 = function(){ if (x == 1) cat("Hello world x = 1") } If I compile these functions and run help1() an error message occurs Fehler in help2() : objekt "x" nicht gefunden in english "error in help2(): object "x" not found" If I change help1 to help1 = function(){ x <<- 1 help2() } so that "x" is now defined at the global environment it works fine. But the problem is, now "x" is defined also outside of help1 and this is not desired ! Is there any usable solution for this problem? But, the original problem is to assign new values for "x" in help1 inside help2 ! Thanks in advance Jörg Betzin --------------------------------------------------- Deutsches Zentrum für Altersfragen Manfred-von-Richthofen-Str. 2 12101 Berlin Tel. (030) 260740-20 E-Mail: joerg.betzin@dza.de URL: http://www.dza.de --------------------------------------------------- [[alternative HTML version deleted]]
On 3/25/2009 11:47 AM, Joerg Betzin wrote:> Dear R-helpers, > > I try to use nested R-functions as follows:You didn't use nested functions. They would look like this: help1 <- function(){ help2 <- function(){ if (x == 1) cat("Hello world x = 1") } x <- 1 help2() } Because help2 is now nested within help1, it can see all the local variables in help1, so things work with it done this way. There are tricks to define help2 outside of help1 but allow it to see the variables within there, but I'd keep it simple and avoid them. Duncan Murdoch> > help1 = function(){ > x = 1 > help2() > } > > with > > help2 = function(){ > if (x == 1) > cat("Hello world x = 1") > } > > If I compile these functions and run help1() > an error message occurs > Fehler in help2() : objekt "x" nicht gefunden > > in english "error in help2(): object "x" not found" > > If I change help1 to > > help1 = function(){ > x <<- 1 > help2() > } > > so that "x" is now defined at the global environment it works fine. > But the problem is, now "x" is defined also outside of help1 and this is > not desired ! > > Is there any usable solution for this problem? > But, the original problem is to assign new values for "x" in help1 inside > help2 ! > > Thanks in advance > > J?rg Betzin > --------------------------------------------------- > Deutsches Zentrum f?r Altersfragen > Manfred-von-Richthofen-Str. 2 > 12101 Berlin > Tel. (030) 260740-20 > E-Mail: joerg.betzin at dza.de > URL: http://www.dza.de > --------------------------------------------------- > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
> > -----Original Message----- > From: r-devel-bounces at r-project.org on behalf of Joerg Betzin > Sent: Wed 3/25/2009 8:47 AM > To: r-devel at r-project.org > Subject: [Rd] linking environments > > Dear R-helpers, > > I try to use nested R-functions as follows:Looks like a question for R-help, not R-devel. It's better to post such questions to the R-help at r-project.org mailing list.> > help1 = function(){ > x = 1 > help2() > } > > with > > help2 = function(){ > if (x == 1) > cat("Hello world x = 1") > } > > If I compile these functions and run help1() > an error message occurs > Fehler in help2() : objekt "x" nicht gefunden > > in english "error in help2(): object "x" not found" >Why not pass the value of x from help1 to help2? help1 <- function(){ x <- 1 help2(x) } help2 <- function(x){ if (x == 1) cat("Hello world x = 1") }> help1 <- function(){+ x <- 1 + help2(x) + }> > > > help2 <- function(x){+ if (x == 1) + cat("Hello world x = 1") + }> > > > help1()Hello world x = 1> (No compiling is involved.)> If I change help1 to > > help1 = function(){ > x <<- 1 > help2() > } > > so that "x" is now defined at the global environment it works fine. > But the problem is, now "x" is defined also outside of help1 and this is > not desired ! > > Is there any usable solution for this problem? > But, the original problem is to assign new values for "x" in help1 inside > help2 !Is this a homework problem? Some reading of ?environment will help answer this.> > Thanks in advance > > J?rg Betzin > --------------------------------------------------- > Deutsches Zentrum f?r Altersfragen > Manfred-von-Richthofen-Str. 2 > 12101 Berlin > Tel. (030) 260740-20 > E-Mail: joerg.betzin at dza.de > URL: http://www.dza.de > --------------------------------------------------- > > [[alternative HTML version deleted]] >Steven McKinney, Ph.D. Statistician Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre email: smckinney +at+ bccrc +dot+ ca tel: 604-675-8000 x7561 BCCRC Molecular Oncology 675 West 10th Ave, Floor 4 Vancouver B.C. V5Z 1L3 Canada