library(shiny)
library(devtools)
# Look at the parent environments for runApp
e <- parenvs(runApp, all=T)
# The first column is the label for the environment, and the second column is the name attribute
# (not all environments have a name attribute).
e
## label name
## 1 <environment: namespace:shiny> ""
## 2 <environment: 0x102f8dc38> "imports:shiny"
## 3 <environment: namespace:base> ""
## 4 <environment: R_GlobalEnv> ""
## 5 <environment: package:devtools> "package:devtools"
## 6 <environment: package:shiny> "package:shiny"
## 7 <environment: package:websockets> "package:websockets"
## 8 <environment: package:digest> "package:digest"
## 9 <environment: package:caTools> "package:caTools"
## 10 <environment: package:bitops> "package:bitops"
## 11 <environment: package:knitr> "package:knitr"
## 12 <environment: package:stats> "package:stats"
## 13 <environment: package:graphics> "package:graphics"
## 14 <environment: package:grDevices> "package:grDevices"
## 15 <environment: package:utils> "package:utils"
## 16 <environment: package:datasets> "package:datasets"
## 17 <environment: package:methods> "package:methods"
## 18 <environment: 0x10093afc0> "Autoloads"
## 19 <environment: base> ""
## 20 <environment: R_EmptyEnv> ""
# e is an envlist, which is just a list of environments.
# This is the namespace environment
e[[1]]
## <environment: namespace:shiny>
# Look in the shiny namespace environment
ls(e[[1]], all.names=T)
## [1] ".__C__Context" ".__C__Dependencies"
## [3] ".__C__FileUploadContext" ".__C__FileUploadOperation"
## [5] ".__C__Map" ".__C__Observable"
## [7] ".__C__Observer" ".__C__ReactiveEnvironment"
## [9] ".__C__ShinyApp" ".__C__TimerCallbacks"
## [11] ".__C__Values" ".__global__"
## [13] ".__NAMESPACE__." ".__S3MethodsTable__."
## [15] ".createOutputWriter" ".createValuesReader"
## [17] ".getReactiveEnvironment" ".globals"
## [19] ".packageName" ".requireCachedGenerics"
## [21] "[.Map" "[.Values"
## [23] "[<-.Map" "[<-.Values"
## [25] "$.reactvaluesreader" "$<-.shinyoutput"
## [27] "a" "addResourcePath"
## [29] "animationOptions" "apps"
## [31] "as.character.shiny.tag" "as.character.shiny.tag.list"
## [33] "as.list.Map" "as.list.reactvaluesreader"
## [35] "bootstrapPage" "br"
## [37] "checkboxGroupInput" "checkboxInput"
## [39] "choicesWithNames" "clearClients"
## [41] "code" "conditionalPanel"
## [43] "Context" "controlLabel"
## [45] "decodeMessage" "Dependencies"
## [47] "div" "download"
## [49] "dynamicHandler" "em"
## [51] "file.path.ci" "fileInput"
## [53] "FileUploadContext" "FileUploadOperation"
## [55] "flushReact" "format.shiny.tag"
## [57] "format.shiny.tag.list" "getCurrentContext"
## [59] "h1" "h2"
## [61] "h3" "h4"
## [63] "h5" "h6"
## [65] "hasDecimals" "headerPanel"
## [67] "helpText" "HTML"
## [69] "htmlEscape" "htmlOutput"
## [71] "httpResponse" "httpServer"
## [73] "img" "invalidateLater"
## [75] "isTag" "joinHandlers"
## [77] "length.Map" "mainPanel"
## [79] "Map" "names.reactvaluesreader"
## [81] "normalizeText" "now"
## [83] "numericInput" "Observable"
## [85] "observe" "Observer"
## [87] "p" "pageWithSidebar"
## [89] "plotOutput" "pre"
## [91] "print.shiny.tag" "print.shiny.tag.list"
## [93] "radioButtons" "reactive"
## [95] "reactive.default" "reactive.function"
## [97] "ReactiveEnvironment" "reactivePlot"
## [99] "reactivePrint" "reactiveTable"
## [101] "reactiveText" "reactiveTimer"
## [103] "reactiveUI" "registerClient"
## [105] "renderPage" "repeatable"
## [107] "resolve" "resourcePathHandler"
## [109] "runApp" "runExample"
## [111] "runGist" "selectInput"
## [113] "serviceApp" "ShinyApp"
## [115] "shinyServer" "shinyUI"
## [117] "sidebarPanel" "singleton"
## [119] "slider" "sliderInput"
## [121] "span" "startApp"
## [123] "staticHandler" "strong"
## [125] "submitButton" "tableOutput"
## [127] "tabPanel" "tabsetPanel"
## [129] "tag" "tagAppendChild"
## [131] "tagList" "tags"
## [133] "tagWrite" "tagWriteChildren"
## [135] "textInput" "textOutput"
## [137] "timerCallbacks" "TimerCallbacks"
## [139] "uiOutput" "unpackMatrix"
## [141] "Values" "verbatimTextOutput"
## [143] "wellPanel" "wsToKey"
# This is the shiny package environment, which is attached and contains all the
# exported objects
e[[5]]
## <environment: package:devtools>
## attr(,"name")
## [1] "package:devtools"
## attr(,"path")
## [1] "/Users/winston/R/devtools"
# Look inside - notice that there are fewer items than in the namespace environment
ls(e[[5]], all.names=T)
## [1] "add_path" "as.envlist" "as.package"
## [4] "bash" "build" "build_vignettes"
## [7] "build_win" "check" "check_cran"
## [10] "check_doc" "clean_dll" "clean_source"
## [13] "clean_vignettes" "compile_dll" "create"
## [16] "dev_example" "dev_help" "dev_meta"
## [19] "dev_mode" "dev_packages" "devtest"
## [22] "document" "get_path" "has_devel"
## [25] "imports_env" "in_dir" "inst"
## [28] "install" "install_bitbucket" "install_github"
## [31] "install_gitorious" "install_url" "install_version"
## [34] "is.package" "load_all" "load_code"
## [37] "load_data" "load_dll" "loaded_packages"
## [40] "missing_s3" "ns_env" "on_path"
## [43] "parenvs" "parse_ns_file" "pkg_env"
## [46] "release" "reload" "revdep"
## [49] "revdep_check" "revdep_maintainers" "run_examples"
## [52] "set_path" "show_news" "show_rd"
## [55] "source_gist" "source_url" "test"
## [58] "unload" "wd" "with_collate"
## [61] "with_debug" "with_env" "with_libpaths"
## [64] "with_locale" "with_options" "with_par"
## [67] "with_path"
The base namespace is special. Notice that namespace:base is a child of the global environment (at position 3). There's another copy of all the same objects, base, which a child of the empty environment (at position 19). They have the same contents:
# Notice 3 and 19
e
## label name
## 1 <environment: namespace:shiny> ""
## 2 <environment: 0x102f8dc38> "imports:shiny"
## 3 <environment: namespace:base> ""
## 4 <environment: R_GlobalEnv> ""
## 5 <environment: package:devtools> "package:devtools"
## 6 <environment: package:shiny> "package:shiny"
## 7 <environment: package:websockets> "package:websockets"
## 8 <environment: package:digest> "package:digest"
## 9 <environment: package:caTools> "package:caTools"
## 10 <environment: package:bitops> "package:bitops"
## 11 <environment: package:knitr> "package:knitr"
## 12 <environment: package:stats> "package:stats"
## 13 <environment: package:graphics> "package:graphics"
## 14 <environment: package:grDevices> "package:grDevices"
## 15 <environment: package:utils> "package:utils"
## 16 <environment: package:datasets> "package:datasets"
## 17 <environment: package:methods> "package:methods"
## 18 <environment: 0x10093afc0> "Autoloads"
## 19 <environment: base> ""
## 20 <environment: R_EmptyEnv> ""
# They have all the same objects!
all(ls(e[[3]], all.names = TRUE) == ls(e[[19]], all.names=TRUE))
## [1] TRUE
The library() function has two phases:
imports:shiny) where the packages imported objects are copied to. This is a child of namespace:base.namespace:shiny) and loading the package's objects into it.package:shiny) and inserting it as the parent of the global environment.Whenever you access objects with :: or :::, R will automatically load the namespace. For example:
# See the list of loaded namespaces
loadedNamespaces()
## [1] "base" "bitops" "caTools" "datasets" "devtools"
## [6] "digest" "evaluate" "formatR" "graphics" "grDevices"
## [11] "httr" "knitr" "memoise" "methods" "parallel"
## [16] "plyr" "RCurl" "RJSONIO" "shiny" "stats"
## [21] "stringr" "tools" "utils" "websockets" "whisker"
## [26] "xtable"
# The list doesn't include gtable
"gtable" %in% loadedNamespaces()
## [1] FALSE
# Run a misc command from gtable
gtable::gtable()
## TableGrob (0 x 0) "layout": 0 grobs
# See the list of loaded namespaces - now includes gtable
"gtable" %in% loadedNamespaces()
## [1] TRUE
R will also automaticaly load a namespace when objects from that namespace are imported into another package that is being loaded. For example, in this session, plyr is loaded because another packages (devtools) imports objects from plyr.
In these cases, even though the namespaces are loaded, they are not attached:
# The search path doesn't include package:plyr
search()
## [1] ".GlobalEnv" "package:devtools" "package:shiny"
## [4] "package:websockets" "package:digest" "package:caTools"
## [7] "package:bitops" "package:knitr" "package:stats"
## [10] "package:graphics" "package:grDevices" "package:utils"
## [13] "package:datasets" "package:methods" "Autoloads"
## [16] "package:base"
# Notice that search() shows you some of the same information as parenvs(), but it returns a char
# vector instead of a list of environments.
parenvs(all=T)
## label name
## 1 <environment: R_GlobalEnv> ""
## 2 <environment: package:devtools> "package:devtools"
## 3 <environment: package:shiny> "package:shiny"
## 4 <environment: package:websockets> "package:websockets"
## 5 <environment: package:digest> "package:digest"
## 6 <environment: package:caTools> "package:caTools"
## 7 <environment: package:bitops> "package:bitops"
## 8 <environment: package:knitr> "package:knitr"
## 9 <environment: package:stats> "package:stats"
## 10 <environment: package:graphics> "package:graphics"
## 11 <environment: package:grDevices> "package:grDevices"
## 12 <environment: package:utils> "package:utils"
## 13 <environment: package:datasets> "package:datasets"
## 14 <environment: package:methods> "package:methods"
## 15 <environment: 0x10093afc0> "Autoloads"
## 16 <environment: base> ""
## 17 <environment: R_EmptyEnv> ""
# Also, search() always starts at the Global environment, whereas with parenvs(), you can pass
# in an object, and it will return the parent envs of that object.
The package can be attached with library() (the namespace is already loaded, so it won't load the namespace again):
library('plyr')
# The search path now includes package:plyr
search()
## [1] ".GlobalEnv" "package:plyr" "package:devtools"
## [4] "package:shiny" "package:websockets" "package:digest"
## [7] "package:caTools" "package:bitops" "package:knitr"
## [10] "package:stats" "package:graphics" "package:grDevices"
## [13] "package:utils" "package:datasets" "package:methods"
## [16] "Autoloads" "package:base"