[R] function that load variables

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Nov 7 17:35:32 CET 2011


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.



More information about the R-help mailing list