如何重新排序搜索路径?

时间:2018-10-19 12:50:27

标签: r tidyverse r-environment

我想将环境(包装或其他)附加到位置2,并希望将其保留在那里。

在大多数情况下,我可以将librarypos=3配合使用,但是tidyverse却有问题:

search()
# [1] ".GlobalEnv"        "tools:rstudio"     "package:stats"    
# [4] "package:graphics"  "package:grDevices" "package:utils"    
# [7] "package:datasets"  "package:methods"   "Autoloads"        
# [10] "package:base" 

something <- list()
attach(something)
library(tidyverse,pos = 3)
search()
# [1] ".GlobalEnv"        "package:forcats"   "package:stringr"  
# [4] "package:dplyr"     "package:purrr"     "package:readr"    
# [7] "package:tidyr"     "package:tibble"    "package:ggplot2"  
# [10] "something"         "package:tidyverse" "tools:rstudio"    
# [13] "package:stats"     "package:graphics"  "package:grDevices"
# [16] "package:utils"     "package:datasets"  "package:methods"  
# [19] "Autoloads"         "package:base" 

tidyverse将其子程序包附加在pos = 2上,我希望以search开头的路径为:

search()
# [1] ".GlobalEnv" "something" "package:forcats"...

我该如何实现?

1 个答案:

答案 0 :(得分:1)

我可以通过定义2个自定义函数来解决它:move_pkg_env移动一个package:*环境,而move_env(使用move_pkg_env)移动搜索路径中的任何环境,只要因为它的位置和目的地之间只有package:*个环境:

这里是使用方法:

something <- list()
attach(something)
library(tidyverse,pos = 3)

search()
# [1] ".GlobalEnv"        "package:forcats"   "package:stringr"  
# [4] "package:dplyr"     "package:purrr"     "package:readr"    
# [7] "package:tidyr"     "package:tibble"    "package:ggplot2"  
# [10] "something"         "package:tidyverse" "tools:rstudio"    
# [13] "package:stats"     "package:graphics"  "package:grDevices"
# [16] "package:utils"     "package:datasets"  "package:methods"  
# [19] "Autoloads"         "package:base"

move_env("something", 2)

search()
# [1] ".GlobalEnv"        "something"         "package:forcats"  
# [4] "package:stringr"   "package:dplyr"     "package:purrr"    
# [7] "package:readr"     "package:tidyr"     "package:tibble"   
# [10] "package:ggplot2"   "package:tidyverse" "tools:rstudio"    
# [13] "package:stats"     "package:graphics"  "package:grDevices"
# [16] "package:utils"     "package:datasets"  "package:methods"  
# [19] "Autoloads"         "package:base" 

功能:

move_pkg_env <- function(env, at, after = FALSE){
  if(is.numeric(env)) env <- search()[env]
  if(!startsWith(env, "package:"))
    stop("env must be a 'package::*' environment")
  if(!env %in% search())
    stop(paste(env,"is not on the search path"))

  pkg <- sub('package:','',env)
  pos <- which(env == search())[1]
  if(is.character(at)){
    at = which(at == search())[1]
    if(is.na(at)) stop(paste(pkg,"is not on the search path"))
  }
  detach(env,character.only = TRUE)
  at <- at + after - (pos < at)
  suppressWarnings(library(pkg,pos=at,
                           warn.conflicts = FALSE, 
                           quietly = TRUE,
                           character.only = TRUE,
                           verbose = FALSE))
}

move_env <- function(env, at, after = FALSE){
  if(is.numeric(env)) env <- search()[env]
  if(!env %in% search())
    stop(paste(env,"is not on the search path"))

  if(is.character(at)){
    at = which(at == search())[1]
    if(is.na(at)) stop(paste(pkg,"is not on the search path"))
  }
  at <- at + after

  pos <- which(env == search())[1]
  while(pos > at){
    move_pkg_env(pos-1,pos+1)
    pos <- which(env == search())[1] 
  }
  while(pos < at){
    move_pkg_env(pos+1,pos-1)
    pos <- which(env == search())[1] 
  }
}

相关的github问题

https://github.com/tidyverse/tidyverse/issues/159