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
400
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
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
Jakarta EE meets AI
ivargrimstad
0
240
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
130
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
540
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
Security_for_introducing_eBPF
kentatada
0
110
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
From Translations to Multi Dimension Entities
alexanderschranz
2
130
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
120
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
200
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Building Your Own Lightsaber
phodgson
103
6.1k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
KATA
mclloyd
29
14k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
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