Как изменить порядок поиска?
Я хочу прикрепить среду (пакет или другое) к позиции 2 и хочу, чтобы она там оставалась.
я могу использовать library
с pos=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 ответ
Я мог бы решить это, определив 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