Re: [R] Dummy quesion about environment

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Wed 28 Sep 2005 - 06:49:48 EST

"Ron Ophir" <ron.ophir@weizmann.ac.il> writes:

> Hi,
> I'm trying to understand environment object in R.
> I used the example:
> f <- function(x) {
> y <- 10
> g <- function(x) x + y
> return(g)
> }
> h <- f()
> h(3)
> then i saw that f return an environment
> > h
> function(x) x + y
> <environment: 01B28570>
> but I coudn't access to x and y object in that environment:
> I tried
> get("x",env=h)
> I tried
> h$y
> can I access y and x?

Well, there are special issues with x above, but the basic thing is to take environment(h). Notice that h _is_ a function that _has_ an associated environment.

> get("y",env=environment(h))

[1] 10

As I said, x is stranger, which is because you used f() in the call:

> get("x",env=environment(h))

> str(get("x",env=environment(h)))
 symbol
> a <- get("x",env=environment(h))
> missing(a)
[1] TRUE
> evalq(x,environment(h))

Error in eval(expr, envir, enclos) : argument "x" is missing, with no  default
> evalq(missing(x),environment(h))

[1] TRUE You'll get the point if you look long and hard enough...

> how can I see an environment tree?

You can't. You can see the parent of an environment, the grandparent, etc., but there is no way to see which children a given environment has.

> oes search does it?

Huh?

> Thanks,
> Ron
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Sep 28 06:56:32 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 17:50:22 EST