Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Environment and Scope rule in R
Search
Itoshi NIKAIDO
September 02, 2012
Programming
2
540
Environment and Scope rule in R
Learning Environment and Lexical Scope rule in R
Itoshi NIKAIDO
September 02, 2012
Tweet
Share
More Decks by Itoshi NIKAIDO
See All by Itoshi NIKAIDO
Opening Remarks HPC OPS 2nd (Shinagawa)
dritoshi
1
390
Dong X. et. al. "Modeling gene expression using chromatin features in various cellular contexts"
dritoshi
1
480
R Object
dritoshi
2
430
Other Decks in Programming
See All in Programming
Realtime API 入門
riofujimon
0
150
受け取る人から提供する人になるということ
little_rubyist
0
230
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Ethereum_.pdf
nekomatu
0
460
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
100
cmp.Or に感動した
otakakot
2
150
Featured
See All Featured
Thoughts on Productivity
jonyablonski
67
4.3k
GitHub's CSS Performance
jonrohan
1030
460k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How to Ace a Technical Interview
jacobian
276
23k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Optimizing for Happiness
mojombo
376
70k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
What's new in Ruby 2.0
geeforr
343
31k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Building Your Own Lightsaber
phodgson
103
6.1k
Transcript
3ͷڥͱείʔϓ *UPTIJ/*,"*%0ESJUPTIJ!HNBJMDPN 1
3͕ͯ͢ΦϒδΣΫτ WFDUPS NBUSJY ؔ ͻͱͭͷΫϥεʹଐ͢Δ 2
ΦϒδΣΫτΛ্ॻ͖͞Εͨ͘ͳ͍ ؔΛݺͼग़͢ ύοέʔδΛಡΈࠐΉ 3
ͲͷΑ͏ͳ ΦϒδΣΫτ͕ར༻Մೳ͔ 4
-FYJDBMTDPQJOH ෳͷڥ͔ΒΦϒδΣΫτΛ୳͢ɾڥΛ࡞ ͢Δنଇ 5
FOWJSPONFOU ڥ 6
FOWJSPONFOU GSBNF FODMPTVSF 7
FOWJSPONFOU GSBNF FODMPTVSF TZNCPM㱻WBMVF lYz㱻 8
FOWJSPONFOU GSBNF FODMPTVSF ڥͷQPJOUFS Ͳͷڥͱ݁ͼ͍͍ͭͯΔͷ͔ 9
globalenv() .GlobalEnv baseenv() emptyenv() ... ... ͍ΘΏΔϫʔΫεϖʔε ίϯιʔϧεΫϦϓτ͔Βͷೖྗ͞ ΕͨΦϒδΣΫτ͕֨ೲ͞ΕΔ ※
ؔύοέʔδผ ڥͷߏ ઢܗͷπϦʔ 10
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmptyEnv ... ... search path:
ΦϒδΣΫτ Λݕࡧ͢Δڥͷॱং search() 4FBSDI1BUI ΦϒδΣΫτΛݕࡧ͢Δॱং 11
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ϩʔυ͞ΕͨΦϒδΣΫτbase ͱ.GlobalEnvͷؒʹஔ͔ΕΔ
library() attach() 1BDLBHFσʔλ͕ϩʔυ͞ΕΔͱ MJCSBSZ ͱBUUBDI 12
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ؔΛݺͼग़͢ ΫϩʔδϟʔڥΛݩʹڥ
(ධՁڥ)͕࡞ΒΕͯɺ 1൪ʹஔ͔ΕΔ ؔΛఆٛ͢Δͱ Ϋϩʔδϟڥ͕࡞ΒΕɺݺͼग़͞ΕΔͨͼʹධՁڥ͕ੜ͞ΕΔ ؔΛఆٛ͢Δ ͦͷڥΛؚΉڥ͕࡞ΒΕΔ = closure ڥ ͕ؔڥͱηοτʹͳ͍ͬͯΔͷͰɺؔࣗͷॴΛ͍ͬͯΔɻ R ͜ΕΛར༻ͯ͠ΦϒδΣΫτΛ୳͢ɻ ͜ͷΑ͏ͳείʔϓنଇͷ͜ͱΛ Lexical scope ͱݺͿ ͜ͷΑ͏ͳؔͷ͜ͱΛΫϩʔδϟؔͱݺͿ 13
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ؔΛݺͼग़͢ ΫϩʔδϟʔڥΛݩʹڥ
(ධՁڥ)͕࡞ΒΕͯɺ 1൪ʹஔ͔ΕΔ ؔͷҾ͍ͭධՁ͞ΕΔͷ͔ DBMMCZWBMVFͱMB[ZFWBMVBUJPO ؔΛఆٛ͢Δ ͦͷڥΛؚΉڥ͕࡞ΒΕΔ = closure ڥ 1. ͕ؔݺͼग़͞ΕΔͱɺධՁڥͷҾϚονϯάنଇͰ ԾҾͱ࣮Ҿ͕র߹͞ΕΔ 2. ͕ؔධՁ͞ΕΔ 3. Ҿ͕ඞཁʹͳͬͨΒɺҾͰ༩͑ΒΕͨʹॳظԽ͞ΕΔ (ԆධՁ) ͜ͷͱ͖ͷϙΠϯλͰͳ͘ɺͦͷͷʹஔ͖มΘΔ (͠) (ԆධՁ༧ΦϒδΣΫτͰୡ͍ͯ͠Δ͕આ໌ΛׂѪ) 14
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... αʔνύεΛ੍ޚ͍ͨ͠ αʔνύεͷྫ֎తͳॲཧͰ͋Δ໊લۭؒΛར༻͢Δ 0.
ΦϒδΣΫτΛఏڙ͢Δύοέʔδ ͷ໊લۭؒ 1. Πϯϙʔτ͞ΕΦϒδΣΫτ 2. base ύοέʔδͷ໊લۭؒ 3. search path - ಉ໊ͷΦϒδΣΫτΛ۠ผ͢Δ - ΦϒδΣΫτͷΧϓηϧԽ Λ͢ΔͨΊͷྫ֎ = namespace 15
globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... 3ͰڥΛૢ࡞͢Δ ڥͷใΛಘΔ search()
searchpaths() topenv() parent.env() ݕࡧ͢Δ environment(ls) ଘࡏͷ֬ೝ exists(“+”, env = baseenv()) ڥΛ࡞Δ e1 <- new.env() e1 16
baseenv() base emptyenv() R_EmpltyEnv 3ͰڥΛૢ࡞͢Δ ڥʹର͢Δมͷૢ࡞ # มΛׂΓͯΔ > assign("a",
1:10, envir = baseenv()) # ݕࡧ͢Δ > exists("a", envir = globalenv(), inherits = FALSE) [1] FALSE > exists("a", envir = baseenv(), inherits = FALSE) [1] TRUE # มΛݕࡧͯ͠ɺΛऔΓग़͢ > mget("a", envir = baseenv()) $a [1] 1 2 3 4 5 6 7 8 9 10 > get("a", envir = baseenv()) [1] 1 2 3 4 5 6 7 8 9 10 17
3Ͱ໊લۭؒΛૢ࡞͢Δ ໊લۭؒͷใΛಘΔɾύοέʔδͷมʹΞΫηε͢Δ # มʹΞΫηε͢Δ # Package::name # export() ͞Ε͍ͯΔؔ base::log
# Package:::name # export() ͞Ε͍ͯͳ͍ؔ library(“Package”) Package:::name # ؔΛద༻ eapply(env, FUN) # ໊લۭؒͷใ loadedNamespaces() # ໊લۭؒͷૢ࡞ loadNamespace(ns) unloadNamespace(ns) attachNamespace(ns) # ໊લۭؒͷ࡞ asNamespace(“name”) # ΤΫεϙʔτ namespaceExport(ns) 18
3ͰڥͲͷΑ͏ʹදݱ͞Ε͍ͯΔ͔ ࣮୯ͳΔMJTU R> e <- new.env() R> e <environment: 0x103186150>
R> e$a <- 1 R> e$b <- "hoge" R> e$b # ڥΛ list ʹ͢Δ R> as.list(e) $a [1] 1 $b [1] "hoge" 19
ڥࢀর͠ 3جຊతʹ͠Ͱ͋Δ͕ڥྫ֎ R> e <- new.env() R> e2 <- e
R> e <environment: 0x103186150> R> e2 <environment: 0x103186150> # ಉ͡ϙΠϯλΛ͍ͯ͞͠Δ R> e$a NULL # e2 ʹ a Λͭ͘Δ R> e2$a <- 1 R> e2$a 1 # ࢀর͠ͳͷͰe ʹ a ͕Ͱ͖͍ͯΔ R> e$a [1] 1 20
·ͱΊ ΦϒδΣΫτΛ্ॻ͖ͨ͘͠ͳ͔ͬͨ • R Lexical scope rule Ͱ͑ΔΦϒδΣΫτΛΔ͜ͱ͕Ͱ͖Δ • RڥʹΑͬͯ
Lexical scope Λୡ͍ͯͨ͠ • Ϣʔβ͕ڥΛ࡞Δ͜ͱͰείʔϓϧʔϧʹج͍ͨΦϒδΣΫτΛੜ ɾཧͰ͖Δ • NamespaceͰείʔϓϧʔϧͷྫ֎Λ࡞ΓɺϢʔβ͕ͦͷείʔϓൣғ ΛܾΊΔ͜ͱ͕Ͱ͖ΔɻRͰޙ͚ͷ࣮ͳͷͰࠓͰ༷͕एׯม Խத 21
ࢀߟࢿྉ จɺΦϯϥΠϯυΩϡϝϯτɺॻ੶ • Lexical Scope and Statistical Computing • The
R Language Definition • RϓϩάϥϛϯάϚχϡΞϧ • Rͷجૅͱϓϩάϥϛϯάٕ๏ 22