[R] environments

Douglas Bates bates at stat.wisc.edu
Tue Aug 26 15:01:33 CEST 2008


On Tue, Aug 26, 2008 at 6:07 AM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> I think you need assign, see ?assign for more details.

> On Tue, Aug 26, 2008 at 6:02 AM, Antje <niederlein-rstat at yahoo.de> wrote:
>> Hi there,

>> I try to understand the usage of environments but I'm not sure if I get it.
>> I wrote a test script like this:

>> testenv <- new.env(environment())

>> myfun <- function(x) {
>>        print(testvar)
>>        testenv$testvar_2 <- 20
>> }
>> environment(myfun) <- testenv

>> testenv$testvar <- 10

As Henrique said, the canonical way of assigning a value within an
environment is the "assign".  A more obscure, but also more effective,
approach is evalq which quotes an expression then evaluates it in the
given environment.  For example

> env <- new.env()
> evalq({aa <- 1:3; bb <- LETTERS[1:9]; cc <- list(A = aa, B = bb)}, env)
> objects(env)
[1] "aa" "bb" "cc"
> env$aa
[1] 1 2 3

>> myfun("hello")
>> ls(envir = testenv)
>>
>> Now, I was wondering if there is any way to create new variables in my
>> environment without this "testenv$...". I know that I can access it that way
>> if I do an attach(testenv) before, but that does not help when creating new
>> ones...
>> Do I completely misunderstand the concept?
>> I'm just looking for an elegant way to access objects of a graphical
>> userinterface from each handler-function and so on. And I thought it might
>> be good to pack them into an environment...
>>
>> Antje
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> 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