Re: [R] Dummy quesion about environment
Thank you Peter, for the comprehensive explanation. The reason I asked Does 'search do it?' is that as I can run ls(env=environment(h)) I can run ls(env=environment(package:methods)) or ls(package:methods) which I can see by search. I thought maybe what I see by search is all the environments under .GobalEnv which I understan this is not what I see by search. Thanks Ron Peter Dalgaard [EMAIL PROTECTED] 09/27/05 11:49 PM Ron Ophir [EMAIL PROTECTED] 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 ~~ - ([EMAIL PROTECTED]) 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
Re: [R] Dummy quesion about environment
Try 'get(x, env = environment(h))' -roger Ron Ophir wrote: 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? how can I see an environment tree? oes search does it? 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 -- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/ __ 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
Re: [R] Dummy quesion about environment
Ron Ophir [EMAIL PROTECTED] 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 ~~ - ([EMAIL PROTECTED]) 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
Re: [R] Dummy quesion about environment
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: Try 'get(x, env = environment(h))' -roger Ron Ophir wrote: 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? how can I see an environment tree? oes search does it? 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 __ 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