使用先决条件一起在 R 中加载本地库
Loading Local Library in R with Prerequisites Together
比如我有一个本地的broom
包,里面有几个必备库比如backports
.
install.packages("broom",lib=file.path(Sys.getenv("userprofile"),"desktop","project"))
我发现下面的library
没有一起加载必要的先决条件
library(broom,lib.loc=file.path(Sys.getenv("userprofile"),"desktop","project"))
Error: package or namespace load failed for ‘broom’:
.onLoad failed in loadNamespace() for 'pillar', details:
call: loadNamespace(name)
error: there is no package called ‘crayon’
我已经具备这些先决条件,因为 install.packages
将它们一起下载。是否应该在加载我要使用的包(即 broom
)之前加载所有先决条件库(例如backports
)?
我做了这个函数,它基本上加载了传递包的依赖项,现在你需要做的就是运行它在加载之前broom
然后它会加载依赖项。
# loads dependencies of passed packages
# can be used in two forms :
# load.dep(broom, tidyr)
#. load.dep("broom", "tidyr")
load.dep <- function(...){
x <- as.list(substitute(...()))
stopifnot(length(x)>0)
x <- sapply(x, as.character)
avp <- available.packages()
pkgs <- unlist(strsplit(avp[x, c("Imports","Depends")], split=" ?(\([^)]+?\))?, ?"), use.names=F)
invisible(lapply(pkgs[!grepl('^(\n|R )', pkgs)], library, character.only=T))
}
比如我有一个本地的broom
包,里面有几个必备库比如backports
.
install.packages("broom",lib=file.path(Sys.getenv("userprofile"),"desktop","project"))
我发现下面的library
没有一起加载必要的先决条件
library(broom,lib.loc=file.path(Sys.getenv("userprofile"),"desktop","project"))
Error: package or namespace load failed for ‘broom’:
.onLoad failed in loadNamespace() for 'pillar', details:
call: loadNamespace(name)
error: there is no package called ‘crayon’
我已经具备这些先决条件,因为 install.packages
将它们一起下载。是否应该在加载我要使用的包(即 broom
)之前加载所有先决条件库(例如backports
)?
我做了这个函数,它基本上加载了传递包的依赖项,现在你需要做的就是运行它在加载之前broom
然后它会加载依赖项。
# loads dependencies of passed packages
# can be used in two forms :
# load.dep(broom, tidyr)
#. load.dep("broom", "tidyr")
load.dep <- function(...){
x <- as.list(substitute(...()))
stopifnot(length(x)>0)
x <- sapply(x, as.character)
avp <- available.packages()
pkgs <- unlist(strsplit(avp[x, c("Imports","Depends")], split=" ?(\([^)]+?\))?, ?"), use.names=F)
invisible(lapply(pkgs[!grepl('^(\n|R )', pkgs)], library, character.only=T))
}