[R] creating and populating an environment
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Jul 28 12:50:51 CEST 2009
prepending with env$ seems not so terrible.
Another form is:
with(e, ...stmt...) or
with(e, { ...stmt...; ...stmt...; ... })
where e is your environment.
On Tue, Jul 28, 2009 at 3:59 AM, Christian
Prinoth<Christian.Prinoth at epsilonsgr.it> wrote:
> I have looked a bit more at this issue. Using new.env() I am able to put
> new vars into the new environment by using the "env$..." syntax. This
> allows to eliminate all variables in one step with "rm(env)", thus
> fixing problem 2) in my original mail. Problem 1) though is unsolved, or
> even worsened, since now I need to prepend the environment name to each
> variable whenever I use one. Even doing "attach(env)" does not help,
> since the global environment has higher priority than the local one.
>
> Does anyone have a suggestion? Ideally, I would like to have a separate
> environment like I have inside a user-defined function.
>
> Thanks
> Christian Prinoth
>
>> -----Original Message-----
>> From: Christian Prinoth
>> Sent: 27 July, 2009 13:33
>> To: 'r-help at r-project.org'
>> Subject: creating and populating an environment
>>
>> Hi, I often work with R by writing long(ish) Excel-VBA macros
>> interspersed with calls to R via RExcel. A typical example of
>> this would be:
>>
>> Sub VBAMacro()
>> 'fetch some data from an excel sheet
>> 'do some basic stuff on said data
>> 'transfer data from vba to R
>> 'run some R statements
>> 'get data back to vba
>> 'show results on the excel sheet
>> 'clean R by deleting all vars that were created: rrun
>> "rm(a,b,c,....)"
>> end sub
>>
>> This has two obvious disadvantages, as I have to make sure:
>> 1) not to use R variable names which may already exist
>> 2) to remove all variables (garbage collection)
>>
>> In order to overcome these issues I was wondering if I should
>> execute all R statements inside the R macro in a separate
>> namespace. I have looked at new.env() but am not really sure
>> how it is supposed to be used. If I type "temp<-new.env()",
>> how do I make sure that all variables declared from then on
>> end up in the "temp" environment? Once I am done, is
>> "rm(temp)" sufficient to get rid of all its content?
>>
>> Basically, I would like to replace the above example with:
>> Sub VBAMacro()
>> rrun "A<-new.env()"
>> 'fetch some data from an excel sheet
>> 'do some basic stuff on said data
>> 'transfer data from vba to R
>> 'run some R statements
>> 'get data back to vba
>> 'show results on the excel sheet
>> rrun "rm(A)"
>> end sub
>>
>> Thanks
>> Christian Prinoth
>>
>
> DISCLAIMER:\ L'utilizzo non autorizzato del presente mes...{{dropped:16}}
>
> ______________________________________________
> 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