An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111107/7f2c955d/attachment.pl>
function that load variables
4 messages · Alaios, R. Michael Weylandt, Jeff Newmiller +1 more
Perhaps you mean to use load(...., envir = .GlobalEnv) Currently you load up the variables in the function environment but then they are thrown away when the function ends. Michael
On Mon, Nov 7, 2011 at 8:40 AM, Alaios <alaios at yahoo.com> wrote:
Dear all,
I have saved few variable names into local files,
I wanted to make a function that load this files and "generates" the variable names into my working environment. I have tried to do that as a function but my problem is
that this function does not return the variable names
load_data<-function(path,Reload=FALSE){
??? if (Reload==TRUE){
??? ? print("Loading results")
??? ? # FirstSet
??? ? load(file=paste(path,'first',sep=""))
??? ? first<-Set
??? ? # SecondSet
??? ? load(file=paste(path,'second',sep=""))
??? ? second<-Set
..................(part omittted here)
??? ? save( first, second,....(part omitted here)...,???? file=paste(path,'Results',sep=""))
??? }
??? return (load(file=paste(path,'Results',sep="")))
}
so my idea was the following:
I call the function and it retuns the values first,second,... loaded into the current working space.
If I want to refresh them, something has changed to the firstDataSet I set the function's variable Reload=True and thus all the data are refreshed.
The problem is not that the return statement I ahve at the end of the function does not return the loaded variable to the working environment but only the status of the load command.
Do you know how I can change that so my function returns also Loaded Variable names to the environment?
Alex
? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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.
A much better solution is to make separate functions for each object you import, and return an object from the function to be assigned in the calling environment. This will be far less confusing to read later.
---------------------------------------------------------------------------
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.
"R. Michael Weylandt" <michael.weylandt at gmail.com> wrote:
Perhaps you mean to use load(...., envir = .GlobalEnv) Currently you load up the variables in the function environment but then they are thrown away when the function ends. Michael On Mon, Nov 7, 2011 at 8:40 AM, Alaios <alaios at yahoo.com> wrote:
Dear all, I have saved few variable names into local files, I wanted to make a function that load this files and "generates" the
variable names into my working environment. I have tried to do that as a function but my problem is
that this function does not return the variable names
load_data<-function(path,Reload=FALSE){
??? if (Reload==TRUE){
??? ? print("Loading results")
??? ? # FirstSet
??? ? load(file=paste(path,'first',sep=""))
??? ? first<-Set
??? ? # SecondSet
??? ? load(file=paste(path,'second',sep=""))
??? ? second<-Set
..................(part omittted here)
??? ? save( first, second,....(part omitted here)...,????
file=paste(path,'Results',sep=""))
??? } ??? return (load(file=paste(path,'Results',sep=""))) } so my idea was the following: I call the function and it retuns the values first,second,... loaded
into the current working space.
If I want to refresh them, something has changed to the firstDataSet
I set the function's variable Reload=True and thus all the data are refreshed.
The problem is not that the return statement I ahve at the end of the
function does not return the loaded variable to the working environment but only the status of the load command.
Do you know how I can change that so my function returns also Loaded
Variable names to the environment?
Alex ? ? ? ?[[alternative HTML version deleted]]
______________________________________________ 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. ______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111107/f2d11b11/attachment.pl>