Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Environment and Scope rule in R

Environment and Scope rule in R

Learning Environment and Lexical Scope rule in R

Itoshi NIKAIDO

September 02, 2012
Tweet

More Decks by Itoshi NIKAIDO

Other Decks in Programming

Transcript

  1. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmptyEnv ... ... search path:

    ΦϒδΣΫτ Λݕࡧ͢Δ؀ڥͷॱং search() 4FBSDI1BUI ΦϒδΣΫτΛݕࡧ͢Δॱং 11
  2. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ؔ਺Λݺͼग़͢ Ϋϩʔδϟʔ؀ڥΛݩʹ؀ڥ

    (ධՁ؀ڥ)͕࡞ΒΕͯɺ 1൪໨ʹஔ͔ΕΔ ؔ਺Λఆٛ͢Δͱ Ϋϩʔδϟ؀ڥ͕࡞ΒΕɺݺͼग़͞ΕΔͨͼʹධՁ؀ڥ͕ੜ੒͞ΕΔ ؔ਺Λఆٛ͢Δ ͦͷ؀ڥΛؚΉ؀ڥ͕࡞ΒΕΔ = closure ؀ڥ ؔ਺͕؀ڥͱηοτʹͳ͍ͬͯΔͷͰɺؔ਺͸ࣗ෼ͷ৔ॴΛ஌͍ͬͯΔɻ R ͸͜ΕΛར༻ͯ͠ΦϒδΣΫτΛ୳͢ɻ ͜ͷΑ͏ͳείʔϓنଇͷ͜ͱΛ Lexical scope ͱݺͿ ͜ͷΑ͏ͳؔ਺ͷ͜ͱΛΫϩʔδϟؔ਺ͱݺͿ 13
  3. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ؔ਺Λݺͼग़͢ Ϋϩʔδϟʔ؀ڥΛݩʹ؀ڥ

    (ධՁ؀ڥ)͕࡞ΒΕͯɺ 1൪໨ʹஔ͔ΕΔ ؔ਺ͷҾ਺͸͍ͭධՁ͞ΕΔͷ͔ DBMMCZWBMVFͱMB[ZFWBMVBUJPO ؔ਺Λఆٛ͢Δ ͦͷ؀ڥΛؚΉ؀ڥ͕࡞ΒΕΔ = closure ؀ڥ 1. ؔ਺͕ݺͼग़͞ΕΔͱɺධՁ؀ڥͷҾ਺ϚονϯάنଇͰ ԾҾ਺ͱ࣮Ҿ਺͕র߹͞ΕΔ 2. ؔ਺͕ධՁ͞ΕΔ 3. Ҿ਺͕ඞཁʹͳͬͨΒɺҾ਺Ͱ༩͑ΒΕͨ஋ʹॳظԽ͞ΕΔ (஗ԆධՁ) ͜ͷͱ͖஋ͷϙΠϯλͰ͸ͳ͘ɺ஋ͦͷ΋ͷʹஔ͖มΘΔ (஋౉͠) (஗ԆධՁ͸༧໿ΦϒδΣΫτͰୡ੒͍ͯ͠Δ͕આ໌ΛׂѪ) 14
  4. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... αʔνύεΛ੍ޚ͍ͨ͠ αʔνύεͷྫ֎తͳॲཧͰ͋Δ໊લۭؒΛར༻͢Δ 0.

    ΦϒδΣΫτΛఏڙ͢Δύοέʔδ ͷ໊લۭؒ 1. Πϯϙʔτ͞ΕΦϒδΣΫτ 2. base ύοέʔδͷ໊લۭؒ 3. search path - ಉ໊ͷΦϒδΣΫτΛ۠ผ͢Δ - ΦϒδΣΫτͷΧϓηϧԽ Λ͢ΔͨΊͷྫ֎ = namespace 15
  5. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... 3Ͱ؀ڥΛૢ࡞͢Δ ؀ڥͷ৘ใΛಘΔ search()

    searchpaths() topenv() parent.env() ݕࡧ͢Δ environment(ls) ଘࡏͷ֬ೝ exists(“+”, env = baseenv()) ؀ڥΛ࡞Δ e1 <- new.env() e1 16
  6. 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
  7. 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
  8. 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
  9. ؀ڥ͸ࢀর౉͠ 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
  10. ·ͱΊ ΦϒδΣΫτΛ্ॻ͖ͨ͘͠ͳ͔ͬͨ • R͸ Lexical scope rule Ͱ࢖͑ΔΦϒδΣΫτΛ஌Δ͜ͱ͕Ͱ͖Δ • R͸؀ڥʹΑͬͯ

    Lexical scope Λୡ੒͍ͯͨ͠ • Ϣʔβ͕؀ڥΛ࡞Δ͜ͱͰείʔϓϧʔϧʹج͍ͨΦϒδΣΫτΛੜ ੒ɾ؅ཧͰ͖Δ • NamespaceͰείʔϓϧʔϧͷྫ֎Λ࡞ΓɺϢʔβ͕ͦͷείʔϓൣғ ΛܾΊΔ͜ͱ͕Ͱ͖ΔɻRͰ͸ޙ෇͚ͷ࣮૷ͳͷͰࠓͰ΋࢓༷͕एׯม Խத 21
  11. ࢀߟࢿྉ ࿦จɺΦϯϥΠϯυΩϡϝϯτɺॻ੶ • Lexical Scope and Statistical Computing • The

    R Language Definition • RϓϩάϥϛϯάϚχϡΞϧ • Rͷجૅͱϓϩάϥϛϯάٕ๏ 22