Could anyone change this function listing environments in the current session so that it would list packages' namespaces too?
env_list <- function(start_env) {
last <- function(s) { s[[length(s)]]}
result <- list(start_env)
while(!identical(emptyenv(), last(result))) {
result <- c(result, list(parent.env(last(result))))
}
result
}
env_list(globalenv())
The output of this function should look something like this:
[[1]]
<environment: R_GlobalEnv>
[[2]]
<environment: package:igraph>
attr(,"name")
[1] "package:igraph"
attr(,"path")
[1] "/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/igraph"
[[6]]
<environment: base>
[[7]]
<environment: R_EmptyEnv>
[[8]]
<environment: namespace:igraph>
[[9]]
<environment: 0x10420cdd8>
[[15]]
<environment: namespace:base>
[[26]]
<environment: namespace:methods>
[[49]]
<environment: 0x1041b3de8>
attr(,"name")
[1] "imports:igraph"
CodePudding user response:
Try this
env_list <- function(start_env) {
last <- function(s) { s[[length(s)]]}
result <- list(start_env)
nmspcs <- list()
while(!identical(emptyenv(), last(result))) {
env <- parent.env(last(result))
if(startsWith(environmentName(env) , "package")){
nm <- sub("package:" , "" , environmentName(env))
result <- c(result, list(env))
nmspcs <- c(nmspcs,list(loadNamespace(nm)))
}
else {
result <- c(result, list(env))
}
}
c(result , nmspcs)
}
env_list(globalenv())
#> [[1]]
#> <environment: R_GlobalEnv>
#>
#> [[2]]
#> <environment: package:stats>
#> attr(,"name")
#> [1] "package:stats"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/stats"
#>
#> [[3]]
#> <environment: package:graphics>
#> attr(,"name")
#> [1] "package:graphics"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/graphics"
#>
#> [[4]]
#> <environment: package:grDevices>
#> attr(,"name")
#> [1] "package:grDevices"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/grDevices"
#>
#> [[5]]
#> <environment: package:utils>
#> attr(,"name")
#> [1] "package:utils"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/utils"
#>
#> [[6]]
#> <environment: package:datasets>
#> attr(,"name")
#> [1] "package:datasets"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/datasets"
#>
#> [[7]]
#> <environment: package:methods>
#> attr(,"name")
#> [1] "package:methods"
#> attr(,"path")
#> [1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/methods"
#>
#> [[8]]
#> <environment: 0x7fc8622bd600>
#> attr(,"name")
#> [1] "Autoloads"
#>
#> [[9]]
#> <environment: 0x7fc863090ba8>
#> attr(,"name")
#> [1] "tools:callr"
#>
#> [[10]]
#> <environment: base>
#>
#> [[11]]
#> <environment: R_EmptyEnv>
#>
#> [[12]]
#> <environment: namespace:stats>
#>
#> [[13]]
#> <environment: namespace:graphics>
#>
#> [[14]]
#> <environment: namespace:grDevices>
#>
#> [[15]]
#> <environment: namespace:utils>
#>
#> [[16]]
#> <environment: namespace:datasets>
#>
#> [[17]]
#> <environment: namespace:methods>
Created on 2022-06-04 by the reprex package (v2.0.1)