SlideShare a Scribd company logo
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
•
•
Rのスコープとフレームと環境と
> 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のスコープとフレームと環境と
• 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

PDF
Rデバッグあれこれ
PDF
はじめてのGroovy
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
PDF
Refactoring to Macros with Clojure
PPTX
Poor Man's Functional Programming
PDF
Building Real Time Systems on MongoDB Using the Oplog at Stripe
PDF
The Ring programming language version 1.7 book - Part 73 of 196
PDF
The Ring programming language version 1.8 book - Part 75 of 202
Rデバッグあれこれ
はじめてのGroovy
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Refactoring to Macros with Clojure
Poor Man's Functional Programming
Building Real Time Systems on MongoDB Using the Oplog at Stripe
The Ring programming language version 1.7 book - Part 73 of 196
The Ring programming language version 1.8 book - Part 75 of 202

What's hot (20)

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

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

PPTX
gen math chaptesdf sdf sdfr 1 lesson1-4.pptx
PPT
Python 101 language features and functional programming
PPTX
GE8151 Problem Solving and Python Programming
PPTX
The Algebric Functions
PDF
PPTX
Week 4 Day 3 Composition of Functions.pptx
PDF
Functional JS for everyone - 4Developers
PPT
functions limits and continuity
PDF
GoCracow #5 Bartlomiej klimczak - GoBDD
PDF
Functions ……..alalalalaoaoakajaiaiaaiaiaiakak
PDF
20181020 advanced higher-order function
PPT
Functions limits and continuity
PPTX
Things about Functional JavaScript
PDF
Swift 함수 커링 사용하기
PDF
仕事で使うF#
PDF
From Javascript To Haskell
PDF
Something about Golang
PDF
jQuery%20on%20Rails%20Presentation
PDF
Rails Presentation - Technology Books, Tech Conferences
PDF
jQuery%20on%20Rails%20Presentation
gen math chaptesdf sdf sdfr 1 lesson1-4.pptx
Python 101 language features and functional programming
GE8151 Problem Solving and Python Programming
The Algebric Functions
Week 4 Day 3 Composition of Functions.pptx
Functional JS for everyone - 4Developers
functions limits and continuity
GoCracow #5 Bartlomiej klimczak - GoBDD
Functions ……..alalalalaoaoakajaiaiaaiaiaiakak
20181020 advanced higher-order function
Functions limits and continuity
Things about Functional JavaScript
Swift 함수 커링 사용하기
仕事で使うF#
From Javascript To Haskell
Something about Golang
jQuery%20on%20Rails%20Presentation
Rails Presentation - Technology Books, Tech Conferences
jQuery%20on%20Rails%20Presentation
Ad

More from Takeshi Arabiki (16)

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

Recently uploaded (20)

PDF
Advanced IT Governance
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Machine learning based COVID-19 study performance prediction
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced IT Governance
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
cuic standard and advanced reporting.pdf
Spectral efficient network and resource selection model in 5G networks
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
GamePlan Trading System Review: Professional Trader's Honest Take
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Weekly Chronicles - August'25 Week I
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Machine learning based COVID-19 study performance prediction
Reach Out and Touch Someone: Haptics and Empathic Computing

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 • •
  • 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 • •
  • 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