函数列表包的命名空间

Function listing packages' namespaces

任何人都可以在当前会话中更改此函数列表环境,以便它也列出包的名称空间吗?

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())

此函数的输出应如下所示:

[[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"

试试这个

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>

reprex package (v2.0.1)

创建于 2022-06-04