SlideShare une entreprise Scribd logo
1  sur  54
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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" />
 

Plus de 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
 

Plus de 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のデータを取得する準備
 

Dernier

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Dernier (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

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