[R] creating and populating an environment

Christian Prinoth Christian.Prinoth at epsilonsgr.it
Tue Jul 28 09:59:09 CEST 2009


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}}




More information about the R-help mailing list