Environment and Scope rule in R

Environment and Scope rule in R

Learning Environment and Lexical Scope rule in R

8cf15712e8fbabc8c619ed4e823b7a80?s=128

Itoshi NIKAIDO

September 02, 2012
Tweet

Transcript

  1. 3ͷ؀ڥͱείʔϓ *UPTIJ/*,"*%0ESJUPTIJ!HNBJMDPN 1

  2. 3͸͢΂͕ͯΦϒδΣΫτ WFDUPS NBUSJY ؔ਺ ͻͱͭͷΫϥεʹଐ͢Δ 2

  3. ΦϒδΣΫτΛ্ॻ͖͞Εͨ͘ͳ͍ ؔ਺Λݺͼग़͢ ύοέʔδΛಡΈࠐΉ 3

  4. ͲͷΑ͏ͳ ΦϒδΣΫτ͕ར༻Մೳ͔ 4

  5. -FYJDBMTDPQJOH ෳ਺ͷ؀ڥ͔ΒΦϒδΣΫτΛ୳͢ɾ؀ڥΛ࡞੒ ͢Δنଇ 5

  6. FOWJSPONFOU ؀ڥ 6

  7. FOWJSPONFOU GSBNF FODMPTVSF 7

  8. FOWJSPONFOU GSBNF FODMPTVSF TZNCPM㱻WBMVF lYz㱻 8

  9. FOWJSPONFOU GSBNF FODMPTVSF ؀ڥ΁ͷQPJOUFS Ͳͷ؀ڥͱ݁ͼ͍͍ͭͯΔͷ͔ 9

  10. globalenv() .GlobalEnv baseenv() emptyenv() ... ... ͍ΘΏΔϫʔΫεϖʔε ίϯιʔϧ΍εΫϦϓτ͔Βͷೖྗ͞ ΕͨΦϒδΣΫτ͕֨ೲ͞ΕΔ ※

    ؔ਺΍ύοέʔδ͸ผ ؀ڥͷߏ଄ ઢܗͷπϦʔ 10
  11. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmptyEnv ... ... search path:

    ΦϒδΣΫτ Λݕࡧ͢Δ؀ڥͷॱং search() 4FBSDI1BUI ΦϒδΣΫτΛݕࡧ͢Δॱং 11
  12. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ϩʔυ͞ΕͨΦϒδΣΫτ͸base ͱ.GlobalEnvͷؒʹஔ͔ΕΔ

    library() attach() 1BDLBHF΍σʔλ͕ϩʔυ͞ΕΔͱ MJCSBSZ ͱBUUBDI 12
  13. globalenv() R_GlobalEnv baseenv() base emptyenv() R_EmpltyEnv ... ... ؔ਺Λݺͼग़͢ Ϋϩʔδϟʔ؀ڥΛݩʹ؀ڥ

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

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

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

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

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

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