Skip to content

Dummy quesion about environment

4 messages · Ron Ophir, Roger D. Peng, Peter Dalgaard +1 more

#
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
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?
how can I see an environment tree? oes search does it?
Thanks,
Ron
#
Try 'get("x", env = environment(h))'

-roger
Ron Ophir wrote:

  
    
#
"Ron Ophir" <ron.ophir at weizmann.ac.il> writes:
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.
[1] 10

As I said, x is stranger, which is because you used f() in the call:
symbol
[1] TRUE
Error in eval(expr, envir, enclos) : argument "x" is missing, with no
 default
[1] TRUE

You'll get the point if you look long and hard enough...
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.
Huh?

  
    
#
Hej,

but in your function you add x and y to 10 and 3, so your values are 
merged to one value available in g variable.
And now you want to see what was your y and your x?
I guess I do not get the idea of your question.
Well, then you could return y and x as well as g.

Greetz n god luck.


Roger D. Peng schrieb: