最近我似乎和合作者分享了很多代码。他们中的许多人是新手/中级R用户,并没有意识到他们必须安装他们还没有的包。
是否有一种优雅的方式来调用installed.packages(),比较那些我正在加载和安装如果丢失?
最近我似乎和合作者分享了很多代码。他们中的许多人是新手/中级R用户,并没有意识到他们必须安装他们还没有的包。
是否有一种优雅的方式来调用installed.packages(),比较那些我正在加载和安装如果丢失?
当前回答
你可以使用require的返回值:
if(!require(somepackage)){
install.packages("somepackage")
library(somepackage)
}
我在安装后使用library,因为如果安装不成功或由于其他原因无法加载包,它将抛出异常。您可以使其更加健壮和可重用:
dynamic_require <- function(package){
if(eval(parse(text=paste("require(",package,")")))) return(TRUE)
install.packages(package)
return(eval(parse(text=paste("require(",package,")"))))
}
此方法的缺点是必须以引号传递包名,而对于真正的require则不这样做。
其他回答
在我的例子中,我想要一个可以从命令行(实际上是通过Makefile)运行的一行程序。下面是一个安装“VGAM”和“feather”的例子,如果它们还没有安装:
R -e 'for (p in c("VGAM", "feather")) if (!require(p, character.only=TRUE)) install.packages(p, repos="http://cran.us.r-project.org")'
在R内部,它就是:
for (p in c("VGAM", "feather")) if (!require(p, character.only=TRUE)) install.packages(p, repos="http://cran.us.r-project.org")
除了前面的解之外,这里没有别的解:
我只说一句 我硬编码回购参数(以避免任何弹出窗口询问使用的镜像) 我不想费心定义一个在其他地方使用的函数
还要注意重要的字符。only=TRUE(如果没有它,require将尝试加载包p)。
达森·k和我有一个吃豆人包可以很好地做到这一点。包中的p_load函数执行此操作。第一行代码只是为了确保安装了pacman。
if (!require("pacman")) install.packages("pacman")
pacman::p_load(package1, package2, package_n)
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')
“ggplot2”是包。它检查包是否安装,如果没有安装,就安装它。然后不管它采用哪个分支,它都会加载包。
今天,我偶然发现了rlang包提供的两个方便函数,即is_installed()和check_installed()。
从帮助页面(强调添加):
These functions check that packages are installed with minimal side effects. If installed, the packages will be loaded but not attached. is_installed() doesn't interact with the user. It simply returns TRUE or FALSE depending on whether the packages are installed. In interactive sessions, check_installed() asks the user whether to install missing packages. If the user accepts, the packages are installed [...]. If the session is non interactive or if the user chooses not to install the packages, the current evaluation is aborted.
interactive()
#> [1] FALSE
rlang::is_installed(c("dplyr"))
#> [1] TRUE
rlang::is_installed(c("foobarbaz"))
#> [1] FALSE
rlang::check_installed(c("dplyr"))
rlang::check_installed(c("foobarbaz"))
#> Error:
#> ! The package `foobarbaz` is required.
由reprex包在2022-03-25创建(v2.0.1)
这就是rbundler包的目的:提供一种方法来控制为特定项目安装的包。现在,这个包使用devtools功能将包安装到项目的目录中。该功能类似于Ruby的捆绑器。
如果你的项目是一个包(推荐),那么你所要做的就是加载rbundler并捆绑包。bundle函数将查看包的DESCRIPTION文件,以确定要捆绑哪些包。
library(rbundler)
bundle('.', repos="http://cran.us.r-project.org")
现在这些包将安装在. rbundle目录中。
如果你的项目不是一个包,那么你可以在项目的根目录中创建一个DESCRIPTION文件,其中包含一个Depends字段,列出你想要安装的包(包括可选的版本信息):
Depends: ggplot2 (>= 0.9.2), arm, glmnet
如果你对这个项目感兴趣,这里是github回购:rbundler。