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
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
Remix on Hono on Cloudflare Workers
yusukebe
1
290
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Click-free releases & the making of a CLI app
oheyadam
2
120
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Agile that works and the tools we love
rasmusluckow
327
21k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Rails Girls Zürich Keynote
gr2m
94
13k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Adopting Sorbet at Scale
ufuk
73
9.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Faster Mobile Websites
deanohume
305
30k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
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