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
550
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
410
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
선언형 UI에서의 상태관리
l2hyunwoo
0
250
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
210
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
360
103 Early Hints
sugi_0000
1
330
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
530
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
870
Оптимизируем производительность блока Казначейство
lamodatech
0
880
Compose UIテストを使った統合テスト
hiroaki404
0
120
バグを見つけた?それAppleに直してもらおう!
uetyo
0
210
GitHubで育つ コラボレーション文化 : ニフティでのインナーソース挑戦事例 - 2024-12-16 GitHub Universe 2024 Recap in ZOZO
niftycorp
PRO
0
1.1k
為你自己學 Python
eddie
0
500
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
240
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
340
Writing Fast Ruby
sferik
628
61k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
How GitHub (no longer) Works
holman
312
140k
Adopting Sorbet at Scale
ufuk
74
9.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
The Invisible Side of Design
smashingmag
299
50k
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