Hi, I want to pass a variable value from one function to another, but not as a function argument. For this propose I have put <<-, but it doesn?t work. My code: one<-function(){ a<<-"variable passed" } two<-function(){ print(a) } dos() If I execute dos(), then the error message is: Error in print(a) : object 'a' not found Thanks"! -- View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710472.html Sent from the R help mailing list archive at Nabble.com.
In line comments On 28/07/2015 13:22, jpara3 wrote:> Hi, I want to pass a variable value from one function to another, but not as > a function argument. For this propose I have put <<-, but it doesn?t work. > > My code: > > one<-function(){ > > a<<-"variable passed" > }So you have to execute one() first?> two<-function(){ > print(a) > } >So go one() here> dos() >I suppose you meant two() pero sin problema.> If I execute dos(), then the error message is: > > Error in print(a) : object 'a' not found > > > Thanks"! > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Global-variables-tp4710472.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Please don't. Function arguments are good... global variables are bad. one <- function(){ result <- list( a="variable passed" ) result } two <- function( v ){ print( v$a ) } x <- one() two( x ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On July 28, 2015 8:22:41 AM EDT, jpara3 <j.para.fernandez at hotmail.com> wrote:>Hi, I want to pass a variable value from one function to another, but >not as >a function argument. For this propose I have put <<-, but it doesn?t >work. > >My code: > >one<-function(){ > >a<<-"variable passed" >} >two<-function(){ >print(a) >} > >dos() > >If I execute dos(), then the error message is: > >Error in print(a) : object 'a' not found > > >Thanks"! > > > > >-- >View this message in context: >http://r.789695.n4.nabble.com/Global-variables-tp4710472.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
normally that works, BUT <<- is BAD and not accepted in some repositories as Bioconductor. one<-function(){ a<-"variable passed" return(a) } x <- one() two<-function(x){ print(x) } On Tue, Jul 28, 2015 at 1:22 PM, jpara3 <j.para.fernandez at hotmail.com> wrote:> Hi, I want to pass a variable value from one function to another, but not > as > a function argument. For this propose I have put <<-, but it doesn?t work. > > My code: > > one<-function(){ > > a<<-"variable passed" > } > two<-function(){ > print(a) > } > > dos() > > If I execute dos(), then the error message is: > > Error in print(a) : object 'a' not found > > > Thanks"! > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Global-variables-tp4710472.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]