Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
R    parent.env   parent.frame                  Tokyo.Lang.R #0 (2012/02/19)                                     @a_bicky
• Takeshi Arabiki    ‣    ‣ Twitter &          : @a_bicky & id:a_bicky•                                R•                 ...
R        Tokyo.R #16                                Tsukuba.R #9                            R                      2011htt...
R• R• parent.env   parent.frame••
R• R• parent.env   parent.frame••
R
“ ”
←R“ ”
> x <- "x of R_GlobalEnvn"> f <- function() {+     x <- "x of function fn"+     g()+     #                                ...
> x <- "x of R_GlobalEnvn"> # parent.frame()> f <- function() {+     x <- "x of function fn"+     g()+     #             f...
R                   (enclosing environment)> x <- 1; y <- 1                  R_GlobalEnv> f <- function(x) print(x)       ...
parent.env            parent.frame parent.envenclosing environmentparent.framecaller’s environment
parent.env            parent.frame parent.envenclosing environmentparent.framecaller’s environment
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }                            callee ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }                            callee ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
>   f <- function() {+       g()+   }>   g <- function() {+       h1()+       h2()+   }>   h1 <- function() NULL>   h2 <- ...
> f <- function() {+     g()+ }> g <- function() {+     h1()+     h2()+ }> h1 <- function() NULL> h2 <- function() NULL> f...
R• R• parent.env   parent.frame••
parent.env   parent.frame
parent.env        parent.frame parent.envenclosing environmentparent.framecaller’s environment
parent.env                        1> f <- function() {+     e <- environment()+     cat("environment of f: "); print(e)+  ...
parent.env                        1> f <- function() {+     e <- environment()+     cat("environment of f: "); print(e)+  ...
parent.env                        2> f <- function() {+     e <- environment()+     cat("environment of f: "); print(e)+  ...
parent.env                       2> f <- function() {+     e <- environment()+     cat("environment of f: "); print(e)+   ...
parent.frame                           1> f <- function() {+     e <- environment()+     cat("environment of f: "); print(...
parent.frame                           1> f <- function() {+     e <- environment()+     cat("environment of f: "); print(...
parent.frame                           2> f <- function() {+     e <- environment()+     cat("environment of f: "); print(...
parent.frame                           2> f <- function() {+     e <- environment()+     cat("environment of f: "); print(...
>   x <- "x of R_GlobalEnv"           R_GlobalEnv>   f <- function() {+       x <- "x of function f"        x = "x of R_Gl...
>   x <- "x of R_GlobalEnv"           R_GlobalEnv>   f <- function() {+       x <- "x of function f"        x = "x of R_Gl...
>   x <- "x of R_GlobalEnv"                R_GlobalEnv>   f <- function() {+       x <- "x of function f"             x = ...
>   x <- "x of R_GlobalEnv"           R_GlobalEnv>   f <- function() {+       x <- "x of function f"        x = "x of R_Gl...
R• R• parent.env   parent.frame••
> search() # R_GlobalEnv                 base package[1] ".GlobalEnv"         "package:stats"      "package:graphics"[4] "...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)>                               ...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)> x[1] "x of R_GlobalEnv">      ...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] ...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] ...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] ...
> x <- "x of R_GlobalEnv"   base package> l <- list(y = "y of l")   .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] ...
> counter <- function(cnt) {+     function() { # <<-+         cnt <<- cnt + 1; print(cnt)+     }+ }> c1 <- counter(1); c1 ...
> counter <- function(cnt) {   R_GlobalEnv+     function() {+         cnt <<- cnt + 1+         print(cnt)                c...
> counter <- function(cnt) {   R_GlobalEnv+      function() {+          cnt <<- cnt + 1+          print(cnt)+      }      ...
> counter <- function(cnt) {   R_GlobalEnv+      function() {+          cnt <<- cnt + 1+          print(cnt)+      }      ...
R• R• parent.env   parent.frame••
• R• parent.env    enclosing environment• parent.frame    caller’s environment••
•   R Language Definition http://cran.r-project.org/doc/manuals/R-lang.pdf    2.1.10 Environment, 3.5 Scope of variables, 4...
Rのスコープとフレームと環境と
Rのスコープとフレームと環境と
Upcoming SlideShare
Loading in …5
×

Rのスコープとフレームと環境と

4,266 views

Published on

Tokyo.Lang.R #0 の発表内容です

Published in: Technology, Business
  • Dating direct: ❶❶❶ http://bit.ly/2ZDZFYj ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ♥♥♥ http://bit.ly/2ZDZFYj ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Rのスコープとフレームと環境と

  1. 1. R parent.env parent.frame Tokyo.Lang.R #0 (2012/02/19) @a_bicky
  2. 2. • Takeshi Arabiki ‣ ‣ Twitter & : @a_bicky & id:a_bicky• R• http://d.hatena.ne.jp/a_bicky/
  3. 3. R Tokyo.R #16 Tsukuba.R #9 R 2011http://www.slideshare.net/abicky/r-9034336 http://www.slideshare.net/abicky/r-10128090 http://www.slideshare.net/abicky/rtwitter
  4. 4. R• R• parent.env parent.frame••
  5. 5. R• R• parent.env parent.frame••
  6. 6. R
  7. 7. “ ”
  8. 8. ←R“ ”
  9. 9. > x <- "x of R_GlobalEnvn"> f <- function() {+ x <- "x of function fn"+ g()+ # f x+ h <- function() cat("h:", x)+ h()+ }> # R_GlobalEnv x> g <- function() cat("g:", x)> f() # f g, hg: x of R_GlobalEnvh: x of function f> g() # gg: x of R_GlobalEnv
  10. 10. > x <- "x of R_GlobalEnvn"> # parent.frame()> f <- function() {+ x <- "x of function fn"+ g()+ # f f x+ h <- function() cat("h:", evalq(x, parent.frame()))+ h()+ }> # x> g <- function() cat("g:", evalq(x, parent.frame()))> f() # f g, hg: x of function fh: x of function f> g() # gg: x of R_GlobalEnv
  11. 11. R (enclosing environment)> x <- 1; y <- 1 R_GlobalEnv> f <- function(x) print(x) x = 1> f(2) y = 1[1] 2 f x = 2
  12. 12. parent.env parent.frame parent.envenclosing environmentparent.framecaller’s environment
  13. 13. parent.env parent.frame parent.envenclosing environmentparent.framecaller’s environment
  14. 14. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL>
  15. 15. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL> f() f f()
  16. 16. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL callee g g()> f() caller f
  17. 17. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ } callee h1 h1()> h1 <- function() NULL> h2 <- function() NULL caller g> f() f
  18. 18. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL g> f() f
  19. 19. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ } callee h2 h2()> h1 <- function() NULL> h2 <- function() NULL caller g> f() f
  20. 20. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL g> f() f
  21. 21. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL> f() f
  22. 22. > f <- function() {+ g()+ }> g <- function() {+ h1()+ h2()+ }> h1 <- function() NULL> h2 <- function() NULL> f()NULL
  23. 23. R• R• parent.env parent.frame••
  24. 24. parent.env parent.frame
  25. 25. parent.env parent.frame parent.envenclosing environmentparent.framecaller’s environment
  26. 26. parent.env 1> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.env(e))+ g <- function() { # f g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.env(e))+ }+ g()+ }> f()environment of f: <environment: 0x105acb7c0>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105acf228>parent environment of g: <environment: 0x105acb7c0>
  27. 27. parent.env 1> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.env(e))+ g <- function() { # f g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.env(e))+ }+ g()+ } f parent.env R_GlobalEnv g parent.env f> f()environment of f: <environment: 0x105acb7c0>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105acf228>parent environment of g: <environment: 0x105acb7c0>
  28. 28. parent.env 2> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.env(e))+ g()+ }> g <- function() { # f R_GlobalEnv g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.env(e))+ }> f()environment of f: <environment: 0x105adbad0>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105adf638>parent environment of g: <environment: R_GlobalEnv>
  29. 29. parent.env 2> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.env(e))+ g()+ }> g <- function() { # f R_GlobalEnv g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.env(e))+ } f parent.env g parent.env R_GlobalEnv> f()environment of f: <environment: 0x105adbad0>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105adf638>parent environment of g: <environment: R_GlobalEnv>
  30. 30. parent.frame 1> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.frame())+ g <- function() { # f g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.frame())+ }+ g()+ }> f()environment of f: <environment: 0x105ae7670>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105aeb110>parent environment of g: <environment: 0x105ae7670>
  31. 31. parent.frame 1> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.frame())+ g <- function() { # f g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.frame())+ }+ g()+ } f parent.frame R_GlobalEnv g parent.frame f> f()environment of f: <environment: 0x105ae7670>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105aeb110>parent environment of g: <environment: 0x105ae7670>
  32. 32. parent.frame 2> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.frame())+ g()+ }> g <- function() { # f R_GlobalEnv g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.frame())+ }> f()environment of f: <environment: 0x105aef440>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105aee7c0>parent environment of g: <environment: 0x105aef440>
  33. 33. parent.frame 2> f <- function() {+ e <- environment()+ cat("environment of f: "); print(e)+ cat("parent environment of f: "); print(parent.frame())+ g()+ }> g <- function() { # f R_GlobalEnv g+ e <- environment()+ cat("environment of g: "); print(e)+ cat("parent environment of g: "); print(parent.frame())+ } f parent.frame R_GlobalEnv g parent.frame f> f()environment of f: <environment: 0x105aef440>parent environment of f: <environment: R_GlobalEnv>environment of g: <environment: 0x105aee7c0>parent environment of g: <environment: 0x105aef440>
  34. 34. > x <- "x of R_GlobalEnv" R_GlobalEnv> f <- function() {+ x <- "x of function f" x = "x of R_GlobalEnv"+ g <- function() {+ cat("x in g:", x, "n")+ }+ g(); h() f+ } x = "x of function f"> h <- function() {+ cat("x in h:", x, "n")+ }> g h
  35. 35. > x <- "x of R_GlobalEnv" R_GlobalEnv> f <- function() {+ x <- "x of function f" x = "x of R_GlobalEnv"+ g <- function() {+ cat("x in g:", x, "n")+ }+ g(); h() f+ } x = "x of function f"> h <- function() {+ cat("x in h:", x, "n")+ }> f() g h
  36. 36. > x <- "x of R_GlobalEnv" R_GlobalEnv> f <- function() {+ x <- "x of function f" x = "x of R_GlobalEnv"+ g <- function() {+ cat("x in g:", x, "n")+ }+ g(); h() f+ } x = "x of function f"> h <- function() {+ cat("x in h:", x, "n")+ } g()> f()x in g: x of function f g h parent.env parent.frame
  37. 37. > x <- "x of R_GlobalEnv" R_GlobalEnv> f <- function() {+ x <- "x of function f" x = "x of R_GlobalEnv"+ g <- function() {+ cat("x in g:", x, "n")+ }+ g(); h() f+ } x = "x of function f"> h <- function() {+ cat("x in h:", x, "n") h()+ }> f()x in g: x of function f g hx in h: x of R_GlobalEnv parent.frame parent.env
  38. 38. R• R• parent.env parent.frame••
  39. 39. > search() # R_GlobalEnv base package[1] ".GlobalEnv" "package:stats" "package:graphics"[4] "package:grDevices" "package:utils" "package:datasets"[7] "package:methods" "Autoloads" "package:base"> attach(iris); search() # iris 2 [1] ".GlobalEnv" "iris" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads"[10] "package:base"> parent.env(globalenv()) # 2 R_GlobalEnv<environment: 0x100cd62b0>attr(,"name")[1] "iris"
  40. 40. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> l y = "y of l" R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  41. 41. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> l y = "y of l" R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  42. 42. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] "y of l" l> y = "y of l" R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  43. 43. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] "y of l" l> .Last.value y = "y of l"[1] "y of l"> R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  44. 44. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] "y of l" l> .Last.value y = "y of l"[1] "y of l"> y <- "y of R_GlobalEnv"> R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l") y = "y of R_GlobalEnv"
  45. 45. > x <- "x of R_GlobalEnv" base package> l <- list(y = "y of l") .Last.value> attach(l)> x[1] "x of R_GlobalEnv"> y[1] "y of l" l> .Last.value y = "y of l"[1] "y of l"> y <- "y of R_GlobalEnv"> y R_GlobalEnv[1] "y of R_GlobalEnv" x = "x of R_GlobalEnv" l = list(y = "y of l") y = "y of R_GlobalEnv"
  46. 46. > counter <- function(cnt) {+ function() { # <<-+ cnt <<- cnt + 1; print(cnt)+ }+ }> c1 <- counter(1); c1 #function() { cnt <<- cnt + 1; print(cnt) }<environment: 0x1020374a0>> ls.str(environment(c1)) # cntcnt : num 1> c1(); c1() # cnt[1] 2[1] 3
  47. 47. > counter <- function(cnt) { R_GlobalEnv+ function() {+ cnt <<- cnt + 1+ print(cnt) counter(1)+ } counter+ }> c1 <- counter(1) cnt = 1> c1
  48. 48. > counter <- function(cnt) { R_GlobalEnv+ function() {+ cnt <<- cnt + 1+ print(cnt)+ } counter+ }> c1 <- counter(1) cnt = 2> c1() c1()[1] 2 c1
  49. 49. > counter <- function(cnt) { R_GlobalEnv+ function() {+ cnt <<- cnt + 1+ print(cnt)+ } counter+ }> c1 <- counter(1) cnt = 3> c1() c1()[1] 2> c1() c1[1] 3
  50. 50. R• R• parent.env parent.frame••
  51. 51. • R• parent.env enclosing environment• parent.frame caller’s environment••
  52. 52. • R Language Definition http://cran.r-project.org/doc/manuals/R-lang.pdf 2.1.10 Environment, 3.5 Scope of variables, 4.3.4 Scope• R Internals http://cran.r-project.org/doc/manuals/R-ints.pdf 1.2 Environments and variable lookup• Frames, Environments, and Scope in R and S-PLUS http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf• ,R , C&R , 2012 SECTION 208, 209• U. ( ), ( ), R , , 2006 4.3• R http://www.slideshare.net/shuyo/r-4022379• environment http://user.ecc.u-tokyo.ac.jp/~s105503/p02.html

×