SlideShare a Scribd company logo
1 of 54
Download to read offline
R
    parent.env   parent.frame
                  Tokyo.Lang.R #0 (2012/02/19)
                                     @a_bicky
• Takeshi Arabiki
    ‣

    ‣ Twitter &          : @a_bicky & id:a_bicky

•
                                R

•
                  http://d.hatena.ne.jp/a_bicky/
R

        Tokyo.R #16                                Tsukuba.R #9                            R                      2011




http://www.slideshare.net/abicky/r-9034336 http://www.slideshare.net/abicky/r-10128090 http://www.slideshare.net/abicky/rtwitter
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()
+     #                                      f     x
+     h <- function() cat("h:", x)
+     h()
+ }
> #                                  R_GlobalEnv       x
> g <- function() cat("g:", x)
> f() #     f        g, h
g: x of R_GlobalEnv
h: x of function f
> g() #                   g
g: x of R_GlobalEnv
> 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, h
g: x of function f
h: x of function f
> g() #                    g
g: x of R_GlobalEnv
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
parent.env            parent.frame
 parent.env




enclosing environment

parent.frame



caller’s environment
parent.env            parent.frame
 parent.env




enclosing environment

parent.frame



caller’s environment
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL
>
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL
>   f()
                            f   f()
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL   callee   g
                                         g()
>   f()
                            caller   f
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
                            callee   h1
                                          h1()
>   h1 <- function() NULL
>   h2 <- function() NULL   caller   g
>   f()
                                     f
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL   g
>   f()
                            f
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
                            callee   h2
                                          h2()
>   h1 <- function() NULL
>   h2 <- function() NULL   caller   g
>   f()
                                     f
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL   g
>   f()
                            f
>   f <- function() {
+       g()
+   }
>   g <- function() {
+       h1()
+       h2()
+   }
>   h1 <- function() NULL
>   h2 <- function() NULL
>   f()
                            f
> f <- function() {
+     g()
+ }
> g <- function() {
+     h1()
+     h2()
+ }
> h1 <- function() NULL
> h2 <- function() NULL
> f()
NULL
R

• R
• parent.env   parent.frame
•
•
parent.env   parent.frame
parent.env        parent.frame
 parent.env




enclosing environment

parent.frame



caller’s environment
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>
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>
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>
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>
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>
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>
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>
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>
>   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
>   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
>   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
>   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                  h
x   in h: x of R_GlobalEnv

                                              parent.frame   parent.env
R

• R
• parent.env   parent.frame
•
•
> 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"
> 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")
> 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")
> 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")
> 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")
> 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"
> 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"
> 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)) #             cnt
cnt : num 1
> c1(); c1() #                    cnt
[1] 2
[1] 3
> counter <- function(cnt) {   R_GlobalEnv
+     function() {
+         cnt <<- cnt + 1
+         print(cnt)                counter(1)
+     }
                                  counter
+ }
> c1 <- counter(1)              cnt = 1
>

                                   c1
> counter <- function(cnt) {   R_GlobalEnv
+      function() {
+          cnt <<- cnt + 1
+          print(cnt)
+      }
                                  counter
+ }
> c1 <- counter(1)              cnt = 2
> c1()
                                          c1()
[1] 2
                                   c1
> 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
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.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

More Related Content

What's hot

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212Mahmoud Samir Fayed
 
Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Tsuyoshi Yamamoto
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptLoïc Knuchel
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecturezefhemel
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingUtsav Patel
 
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."sjabs
 
mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomerzefhemel
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macrosMarina Sigaeva
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184Mahmoud Samir Fayed
 
Gearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copyGearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copyBrian Aker
 
Gearman, from the worker's perspective
Gearman, from the worker's perspectiveGearman, from the worker's perspective
Gearman, from the worker's perspectiveBrian Aker
 
The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189Mahmoud Samir Fayed
 

What's hot (20)

Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察Jggug 2010 330 Grails 1.3 観察
Jggug 2010 330 Grails 1.3 観察
 
Programmation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScriptProgrammation fonctionnelle en JavaScript
Programmation fonctionnelle en JavaScript
 
mobl - model-driven engineering lecture
mobl - model-driven engineering lecturemobl - model-driven engineering lecture
mobl - model-driven engineering lecture
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
Kamil Chmielewski, Jacek Juraszek - "Hadoop. W poszukiwaniu złotego młotka."
 
Java Program
Java ProgramJava Program
Java Program
 
mobl presentation @ IHomer
mobl presentation @ IHomermobl presentation @ IHomer
mobl presentation @ IHomer
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
 
Typelevel summit
Typelevel summitTypelevel summit
Typelevel summit
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184The Ring programming language version 1.5.3 book - Part 25 of 184
The Ring programming language version 1.5.3 book - Part 25 of 184
 
Gearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copyGearmam, from the_worker's_perspective copy
Gearmam, from the_worker's_perspective copy
 
Pdxpugday2010 pg90
Pdxpugday2010 pg90Pdxpugday2010 pg90
Pdxpugday2010 pg90
 
Gearman, from the worker's perspective
Gearman, from the worker's perspectiveGearman, from the worker's perspective
Gearman, from the worker's perspective
 
mobl
moblmobl
mobl
 
The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189The Ring programming language version 1.6 book - Part 15 of 189
The Ring programming language version 1.6 book - Part 15 of 189
 

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

Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
The Algebric Functions
The Algebric FunctionsThe Algebric Functions
The Algebric Functionsitutor
 
Functions
FunctionsFunctions
FunctionsJJkedst
 
Functional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersFunctional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersBartek Witczak
 
functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuityPume Ananda
 
GoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDGoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDBartłomiej Kiełbasa
 
20181020 advanced higher-order function
20181020 advanced higher-order function20181020 advanced higher-order function
20181020 advanced higher-order functionChiwon Song
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScriptChengHui Weng
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#bleis tift
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskellujihisa
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingIstanbul Tech Talks
 
Rails Presentation - Technology Books, Tech Conferences
 Rails Presentation - Technology Books, Tech Conferences Rails Presentation - Technology Books, Tech Conferences
Rails Presentation - Technology Books, Tech Conferencestutorialsruby
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentationguestcf600a
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentationguestcf600a
 
JQuery-Tutorial" />
  JQuery-Tutorial" />  JQuery-Tutorial" />
JQuery-Tutorial" />tutorialsruby
 

Similar to Rのスコープとフレームと環境と (20)

Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
The Algebric Functions
The Algebric FunctionsThe Algebric Functions
The Algebric Functions
 
functions
functionsfunctions
functions
 
Functions
FunctionsFunctions
Functions
 
Functional JS for everyone - 4Developers
Functional JS for everyone - 4DevelopersFunctional JS for everyone - 4Developers
Functional JS for everyone - 4Developers
 
functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuity
 
GoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDDGoCracow #5 Bartlomiej klimczak - GoBDD
GoCracow #5 Bartlomiej klimczak - GoBDD
 
20181020 advanced higher-order function
20181020 advanced higher-order function20181020 advanced higher-order function
20181020 advanced higher-order function
 
Functions limits and continuity
Functions limits and continuityFunctions limits and continuity
Functions limits and continuity
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
 
From Javascript To Haskell
From Javascript To HaskellFrom Javascript To Haskell
From Javascript To Haskell
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
Rails Presentation - Technology Books, Tech Conferences
 Rails Presentation - Technology Books, Tech Conferences Rails Presentation - Technology Books, Tech Conferences
Rails Presentation - Technology Books, Tech Conferences
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 
jQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20PresentationjQuery%20on%20Rails%20Presentation
jQuery%20on%20Rails%20Presentation
 
JQuery-Tutorial" />
  JQuery-Tutorial" />  JQuery-Tutorial" />
JQuery-Tutorial" />
 

More from Takeshi Arabiki

クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜
クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜
クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜Takeshi Arabiki
 
Introduction to Japanese Morphological Analysis
Introduction to Japanese Morphological AnalysisIntroduction to Japanese Morphological Analysis
Introduction to Japanese Morphological AnalysisTakeshi Arabiki
 
R による文書分類入門
R による文書分類入門R による文書分類入門
R による文書分類入門Takeshi Arabiki
 
Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Takeshi Arabiki
 
HTML5 Canvas で学ぶアフィン変換
HTML5 Canvas で学ぶアフィン変換HTML5 Canvas で学ぶアフィン変換
HTML5 Canvas で学ぶアフィン変換Takeshi Arabiki
 
Introduction to Favmemo for Immature Engineers
Introduction to Favmemo for Immature EngineersIntroduction to Favmemo for Immature Engineers
Introduction to Favmemo for Immature EngineersTakeshi Arabiki
 
twitteRで快適Rライフ!
twitteRで快適Rライフ!twitteRで快適Rライフ!
twitteRで快適Rライフ!Takeshi Arabiki
 
RではじめるTwitter解析
RではじめるTwitter解析RではじめるTwitter解析
RではじめるTwitter解析Takeshi Arabiki
 
R版Getopt::Longを作ってみた
R版Getopt::Longを作ってみたR版Getopt::Longを作ってみた
R版Getopt::Longを作ってみたTakeshi Arabiki
 
Rデータフレーム自由自在
Rデータフレーム自由自在Rデータフレーム自由自在
Rデータフレーム自由自在Takeshi Arabiki
 
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜Takeshi Arabiki
 
はじめてのまっぷりでゅ〜す
はじめてのまっぷりでゅ〜すはじめてのまっぷりでゅ〜す
はじめてのまっぷりでゅ〜すTakeshi Arabiki
 
TwitterのデータをRであれこれ
TwitterのデータをRであれこれTwitterのデータをRであれこれ
TwitterのデータをRであれこれTakeshi Arabiki
 
Twitterのデータを取得する準備
Twitterのデータを取得する準備Twitterのデータを取得する準備
Twitterのデータを取得する準備Takeshi Arabiki
 

More from Takeshi Arabiki (16)

開発の心得
開発の心得開発の心得
開発の心得
 
クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜
クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜
クックパッド特売情報 における自然言語処理 〜固有表現抽出を利用した検索システム〜
 
Introduction to Japanese Morphological Analysis
Introduction to Japanese Morphological AnalysisIntroduction to Japanese Morphological Analysis
Introduction to Japanese Morphological Analysis
 
R による文書分類入門
R による文書分類入門R による文書分類入門
R による文書分類入門
 
Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理Rのデータ構造とメモリ管理
Rのデータ構造とメモリ管理
 
HTML5 Canvas で学ぶアフィン変換
HTML5 Canvas で学ぶアフィン変換HTML5 Canvas で学ぶアフィン変換
HTML5 Canvas で学ぶアフィン変換
 
Introduction to Favmemo for Immature Engineers
Introduction to Favmemo for Immature EngineersIntroduction to Favmemo for Immature Engineers
Introduction to Favmemo for Immature Engineers
 
twitteRで快適Rライフ!
twitteRで快適Rライフ!twitteRで快適Rライフ!
twitteRで快適Rライフ!
 
RではじめるTwitter解析
RではじめるTwitter解析RではじめるTwitter解析
RではじめるTwitter解析
 
R版Getopt::Longを作ってみた
R版Getopt::Longを作ってみたR版Getopt::Longを作ってみた
R版Getopt::Longを作ってみた
 
Rデータフレーム自由自在
Rデータフレーム自由自在Rデータフレーム自由自在
Rデータフレーム自由自在
 
HMM, MEMM, CRF メモ
HMM, MEMM, CRF メモHMM, MEMM, CRF メモ
HMM, MEMM, CRF メモ
 
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜
文字列カーネルによる辞書なしツイート分類 〜文字列カーネル入門〜
 
はじめてのまっぷりでゅ〜す
はじめてのまっぷりでゅ〜すはじめてのまっぷりでゅ〜す
はじめてのまっぷりでゅ〜す
 
TwitterのデータをRであれこれ
TwitterのデータをRであれこれTwitterのデータをRであれこれ
TwitterのデータをRであれこれ
 
Twitterのデータを取得する準備
Twitterのデータを取得する準備Twitterのデータを取得する準備
Twitterのデータを取得する準備
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

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

  • 1. R parent.env parent.frame Tokyo.Lang.R #0 (2012/02/19) @a_bicky
  • 2. • Takeshi Arabiki ‣ ‣ Twitter & : @a_bicky & id:a_bicky • R • http://d.hatena.ne.jp/a_bicky/
  • 3. R Tokyo.R #16 Tsukuba.R #9 R 2011 http://www.slideshare.net/abicky/r-9034336 http://www.slideshare.net/abicky/r-10128090 http://www.slideshare.net/abicky/rtwitter
  • 4. R • R • parent.env parent.frame • •
  • 5. R • R • parent.env parent.frame • •
  • 6. R
  • 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, h g: x of R_GlobalEnv h: x of function f > g() # g g: x of R_GlobalEnv
  • 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, h g: x of function f h: x of function f > g() # g g: x of R_GlobalEnv
  • 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. parent.env parent.frame parent.env enclosing environment parent.frame caller’s environment
  • 13. parent.env parent.frame parent.env enclosing environment parent.frame caller’s environment
  • 14. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL >
  • 15. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL > f() f f()
  • 16. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL callee g g() > f() caller f
  • 17. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } callee h1 h1() > h1 <- function() NULL > h2 <- function() NULL caller g > f() f
  • 18. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL g > f() f
  • 19. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } callee h2 h2() > h1 <- function() NULL > h2 <- function() NULL caller g > f() f
  • 20. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL g > f() f
  • 21. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL > f() f
  • 22. > f <- function() { + g() + } > g <- function() { + h1() + h2() + } > h1 <- function() NULL > h2 <- function() NULL > f() NULL
  • 23. R • R • parent.env parent.frame • •
  • 24. parent.env parent.frame
  • 25. parent.env parent.frame parent.env enclosing environment parent.frame caller’s environment
  • 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. 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. 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. 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. 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. 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. 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. 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. > 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. > 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. > 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. > 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 h x in h: x of R_GlobalEnv parent.frame parent.env
  • 38. R • R • parent.env parent.frame • •
  • 39.
  • 40. > 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"
  • 41. > 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")
  • 42. > 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")
  • 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 > y = "y of l" R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  • 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" > R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l")
  • 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" > R_GlobalEnv x = "x of R_GlobalEnv" l = list(y = "y of l") y = "y of R_GlobalEnv"
  • 46. > 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"
  • 47. > 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)) # cnt cnt : num 1 > c1(); c1() # cnt [1] 2 [1] 3
  • 48. > counter <- function(cnt) { R_GlobalEnv + function() { + cnt <<- cnt + 1 + print(cnt) counter(1) + } counter + } > c1 <- counter(1) cnt = 1 > c1
  • 49. > counter <- function(cnt) { R_GlobalEnv + function() { + cnt <<- cnt + 1 + print(cnt) + } counter + } > c1 <- counter(1) cnt = 2 > c1() c1() [1] 2 c1
  • 50. > 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
  • 51. R • R • parent.env parent.frame • •
  • 52.
  • 53. • R • parent.env enclosing environment • parent.frame caller’s environment • •
  • 54. 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