检查丢失的软件包并安装它们的优雅方法?

本文翻译自:Elegant way to check for missing packages and install them?

I seem to be sharing a lot of code with coauthors these days. 这些天,我似乎与合著者分享了很多代码。 Many of them are novice/intermediate R users and don't realize that they have to install packages they don't already have. 他们中的许多人是R新手/中级用户,他们没有意识到他们必须安装尚未拥有的软件包。

Is there an elegant way to call installed.packages() , compare that to the ones I am loading and install if missing? 有没有一种优雅的方法来调用installed.packages() ,将其与我正在加载和安装的方法进行比较installed.packages()如果缺少的话)?


#1楼

参考:https://stackoom.com/question/HA2T/检查丢失的软件包并安装它们的优雅方法


#2楼

I use the following which will check if package is installed and if dependencies are updated, then loads the package. 我使用以下命令检查软件包是否已安装以及依赖项是否已更新,然后加载该软件包。

p<-c('ggplot2','Rcpp')
install_package<-function(pack)
{if(!(pack %in% row.names(installed.packages())))
{
  update.packages(ask=F)
  install.packages(pack,dependencies=T)
}
 require(pack,character.only=TRUE)
}
for(pack in p) {install_package(pack)}

completeFun <- function(data, desiredCols) {
  completeVec <- complete.cases(data[, desiredCols])
  return(data[completeVec, ])
}

#3楼

This is the purpose of the rbundler package : to provide a way to control the packages that are installed for a specific project. 这是rbundler软件包的目的:提供一种方法来控制为特定项目安装的软件包。 Right now the package works with the devtools functionality to install packages to your project's directory. 现在,该软件包可与devtools功能一起使用,以将软件包安装到项目目录中。 The functionality is similar to Ruby's bundler . 该功能类似于Ruby的bundler

If your project is a package (recommended) then all you have to do is load rbundler and bundle the packages. 如果您的项目是一个软件包(推荐),那么您要做的就是加载rbundler并将其打包。 The bundle function will look at your package's DESCRIPTION file to determine which packages to bundle. bundle功能将查看您包裹的DESCRIPTION文件,以确定要捆绑的包裹。

library(rbundler)
bundle('.', repos="http://cran.us.r-project.org")

Now the packages will be installed in the .Rbundle directory. 现在,这些软件包将安装在.Rbundle目录中。

If your project isn't a package, then you can fake it by creating a DESCRIPTION file in your project's root directory with a Depends field that lists the packages that you want installed (with optional version information): 如果您的项目不是软件包,则可以通过在项目的根目录中创建一个DESCRIPTION文件来伪造它,该文件的Depends字段列出了要安装的软件包(带有可选的版本信息):

Depends: ggplot2 (>= 0.9.2), arm, glmnet

Here's the github repo for the project if you're interested in contributing: rbundler . 如果您有兴趣提供以下项目的github 仓库rbundler


#4楼

This solution will take a character vector of package names and attempt to load them, or install them if loading fails. 此解决方案将采用包名称的字符向量并尝试加载它们,或者在加载失败时安装它们。 It relies on the return behaviour of require to do this because... 它依赖于require的返回行为来执行此操作,因为...

require returns (invisibly) a logical indicating whether the required package is available require返回(不可见)逻辑,指示所需包是否可用

Therefore we can simply see if we were able to load the required package and if not, install it with dependencies. 因此,我们可以简单地查看是否能够加载所需的软件包,如果不能加载,请使用依赖项进行安装。 So given a character vector of packages you wish to load... 因此,给定要加载的软件包的字符向量...

foo <- function(x){
  for( i in x ){
    #  require returns TRUE invisibly if it was able to load package
    if( ! require( i , character.only = TRUE ) ){
      #  If package was not able to be loaded then re-install
      install.packages( i , dependencies = TRUE )
      #  Load package after installing
      require( i , character.only = TRUE )
    }
  }
}

#  Then try/install packages...
foo( c("ggplot2" , "reshape2" , "data.table" ) )

#5楼

You can just use the return value of require : 您可以只使用require的返回值:

if(!require(somepackage)){
    install.packages("somepackage")
    library(somepackage)
}

I use library after the install because it will throw an exception if the install wasn't successful or the package can't be loaded for some other reason. 我在安装后使用library ,因为如果安装不成功或由于其他原因而无法加载软件包,它将引发异常。 You make this more robust and reuseable: 您可以使它更加健壮和可重用:

dynamic_require <- function(package){
  if(eval(parse(text=paste("require(",package,")")))) return True

  install.packages(package)
  return eval(parse(text=paste("require(",package,")")))
}

The downside to this method is that you have to pass the package name in quotes, which you don't do for the real require . 这种方法的缺点是必须用引号将包名传递,而对于真正的require


#6楼

Dason K. and I have the pacman package that can do this nicely. Dason K.和我有pacman软件包,可以很好地做到这一点。 The function p_load in the package does this. 包中的函数p_load做到这一点。 The first line is just to ensure that pacman is installed. 第一行只是为了确保已安装pacman。

if (!require("pacman")) install.packages("pacman")
pacman::p_load(package1, package2, package_n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值