如何判断某个软件包是否已安装? [英] How can I tell if a certain package was already installed?
问题描述
当我安装 yaml 包时,如果之前安装过 RStudio,则会在 RStudio 中弹出一条烦人的错误消息.如何判断软件包是否已安装,以便我可以在代码中决定是否安装软件包?
When I install the yaml package, an annoying error message pops up in RStudio if it had been previously installed. How can I tell if the package was already installed so I can decide in my code whether to install the package or not?
消息在一个弹出窗口中,它是:
The message is in a pop up window and it is:
此安装将更新的一个或多个软件包当前正在加载.在更新这些包之前重新启动 R 是强烈推荐.RStudio可以重新启动R然后自动重新启动后继续安装(所有工作和数据将被在重新启动期间保留).您想在此之前重新启动 R安装?
One or more of the packages that will be updated by this installation are currently loaded. Restarting R prior to updating these packages is strongly recommended. RStudio can restart R and then automatically continue the installation after restarting (all work and data will be preserved during the restart). Do you want to restart R prior to installing?
推荐答案
这将加载 yaml
,如果尚未安装,请先安装:
This will load yaml
, installing it first if its not already installed:
if (!require(yaml)) {
install.packages("yaml")
library(yaml)
}
或者如果你想参数化它:
or if you want to parameterize it:
pkg <- "yaml"
if (!require(pkg, character.only = TRUE)) {
install.packages(pkg)
if (!require(pkg, character.only = TRUE)) stop("load failure: ", pkg)
}
更新.参数化.
这篇关于如何判断某个软件包是否已安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!