本文翻译自: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 packageif( ! require( i , character.only = TRUE ) ){#  If package was not able to be loaded then re-installinstall.packages( i , dependencies = TRUE )#  Load package after installingrequire( 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 Trueinstall.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)

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

  1. python安装包-Python软件包的安装(3种方法)

    有些 Python 软件包是系统自带的,如 sys,这些包在安装 Python 时已自动安装.但有些包是需要自行下载安装的,如 PIL.这些第三方的软件有的以源代码的形式提供,有的以安装包的形式提供. ...

  2. shell实例第5讲:检查软件包是否安装

    检查软件包是否安装 #!/bin/bash if rpm -q $! &>/dev/null; thenecho "$1 is already installed." ...

  3. linux 软件包的安装

    软件包的安装: Debian: 编译成二进制,管理工具,软件包管理器 vim bash mount/umount 封装成一中特定的格式! rpm:Redhat Package Manager   RP ...

  4. 如何查看linux 是否安装软件包,linux 查看软件包是否安装 linux查看软件包

    Linux下怎样检查,如何查看某软件包是否已经安装 1.rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字" ...

  5. 第八章:软件包的安装与管理

    第八章:软件包的安装与管理 1.Linux常见软件包格式 RPM:Redhat PackageManagement 红帽包管理.Redhat Linux专用的软件包格式,扩展名为.rpm DEB:De ...

  6. linux里用dnf安装软件,Linux 软件包管理器 DNF 的使用方法

    DNF是新一代的RPM软件包管理器.他首先出现在 Fedora 18 这个发行版中,最近取代了YUM正式成为包管理器,克服了YUM包管理器的一些瓶颈,提升了包括用户体验,内存占用,依赖分析,运行速度等 ...

  7. Linux软件包管理(安装、升级、卸载软件包,管理软件源)

    文章目录 Linux软件包分为二进制包和源码包 源码包 优点缺点 安装过程 二进制包 优点缺点 依赖性 安装方法 RPM RPM包获取方法 RPM包格式 RPM安装 RPM查询 RPM升级 RPM卸载 ...

  8. linux的软件包是独立的,Linux系统下软件包的安装

    (以下内容是云课堂Linux课程的笔记,个人纯手工记录,课程以RedHat系列为主) Linux系统下软件包的安装方式包括:源代码安装.本地二进制包安装(rpm命令手工安装).在线二进制包安装(yum ...

  9. yum安装软件包提示Error Downloading Packages解决方法

    yum安装软件包提示Error Downloading Packages解决方法 参考文章: (1)yum安装软件包提示Error Downloading Packages解决方法 (2)https: ...

最新文章

  1. c语言中malloc分配矩阵,malloc,分配矩阵
  2. 城市需要建什么样的能源数据中心?
  3. 《货币是个什么东西》笔记
  4. 一文看懂集群、分布式与负载均衡的关系
  5. 定义列表的特点html,HTML的列表表格表单知识点
  6. android switch 未定义,在switch语句中初始化时未定义的变量?
  7. python代码安全性问题_这个python代码对注入安全吗?
  8. ArcGIS 生成要素轮廓线掩膜
  9. Seaborn学习(一)------- 构建结构化多绘图网格(FacetGrid()、map())详解
  10. 网关支付、银联代扣通道、快捷支付、银行卡支付分别是怎么样进行支付的?...
  11. vscode使用教程-开始学习前端开发啦~
  12. 深度森林实现时间序列预测(Python)
  13. 常用字典代码推荐标准
  14. python曲线拟合预测_python曲线拟合
  15. http响应报文,如果响应的内容比较大,客户端怎么样判断接收完了呢?
  16. 下载MySQL驱动程序
  17. 复习笔记1-java基础
  18. 均匀分布 卡方分布_指数分布和卡方分布转换以及初试专业课试题拆封视频
  19. 搭建电商系统平台需要多少钱?
  20. 我们在讲的 Database Plus,到底能解决什么样的问题?

热门文章

  1. 黄聪:一个拼图工具的制作思路
  2. Phoenix Framework 1.4.7,自动化测试平台
  3. IUS database
  4. PHP数组foreach后使用current取值的问题
  5. Java的一些基础小知识之JVM与GC (转)
  6. Cent Os下安装软件之---源码安装
  7. Delphi的StringReplace 字符串替换函数
  8. SSH自动生成数据库
  9. 中国武侠片的50条爆笑定律
  10. input type=file图片上传时,先预览