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
470
R Object
dritoshi
2
430
Other Decks in Programming
See All in Programming
利用者視点で考える、イテレータとの上手な付き合い方
syumai
4
210
Quarto Clean Theme
nicetak
0
220
ファーストペンギンBot @Qiita Hackathon 2024 予選
dyson_web
0
210
フロントエンドの現在地とこれから
koba04
8
3.9k
Assembling the Future: crafting the missing pieces of the Ruby on Wasm puzzle
skryukov
0
130
5年分のツケを一気に払った話
soogie
3
1.1k
CSC305 Lecture 01
javiergs
PRO
1
140
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
190
Unlocking Python's Core Magic
leew
0
110
DjangoNinjaで高速なAPI開発を実現する
masaya00
0
440
Cancel Next.js Page Navigation: Full Throttle
ypresto
1
110
データサイエンスのフルサイクル開発を実現する機械学習パイプライン
xcnkx
2
460
Featured
See All Featured
Music & Morning Musume
bryan
46
6.1k
A designer walks into a library…
pauljervisheath
201
24k
How to name files
jennybc
77
98k
Navigating Team Friction
lara
183
14k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Clear Off the Table
cherdarchuk
91
320k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
7
560
Happy Clients
brianwarren
97
6.7k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Why Our Code Smells
bkeepers
PRO
334
57k
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