如何判断某个软件包是否已安装?

时间:2022-12-30 07:21:16

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?

当我安装yaml软件包时,如果以前安装过,则会在RStudio中弹出恼人的错误消息。如何判断软件包是否已安装,以便我可以在代码中决定是否安装软件包?

The message is in a pop up window and it is:

消息在弹出窗口中,它是:

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?

当前加载了将由此安装更新的一个或多个软件包。强烈建议在更新这些软件包之前重新启动R. RStudio可以重新启动R,然后在重新启动后自动继续安装(重启期间将保留所有工作和数据)。您想在安装之前重启R吗?

4 个解决方案

#1


12  

This will load yaml, installing it first if its not already installed:

这将加载yaml,如果尚未安装,首先安装它:

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)
}

UPDATE. Parametrization.

UPDATE。参数化。

#2


8  

you can use installed.packages() to find installed packages

您可以使用installed.packages()来查找已安装的软件包

#3


3  

Alternatively, you can use the require function. It will try to load the package and silently return a logical stating whether or not the package is available. There is also a warning if the package cannot be loaded.

或者,您可以使用require函数。它将尝试加载包并静默返回一个逻辑,说明包是否可用。如果无法加载包,也会发出警告。

test1 <- require("stats")
test1

test2 <- require("blah")
test2

#4


1  

I am using the following construction in my code. The essential part is about calling library within tryCatch and installing it if it fails:

我在我的代码中使用以下构造。关键部分是关于在tryCatch中调用库并在失败时安装它:

lib.auto <- function(lib, version=NULL, install.fun=install.packages, ...) {
  tryCatch(
    library(lib, character.only=T),
    error=function(e) {
      install.fun(lib, ...)
      library(lib, character.only=T)
    }
  )
  if (!is.null(version)) {
    if (packageVersion(lib) < version) {
      require(devtools)
      detach(paste0('package:', lib), unload=T, character.only=T)
      install.fun(lib, ...)
      library(lib, character.only=T)
      if (packageVersion(lib) < version) {
        stop(sprintf('Package %s not available in version %s. Installed version: %s', lib, version,
                     packageVersion(lib)))
      }
    }
  }
}

lib.auto('BiocInstaller',
         install.fun=function(lib) {
           source("http://bioconductor.org/biocLite.R")
           biocLite(lib)
         })

options(repos=biocinstallRepos())
lib.auto.bioc <- lib.auto

lib.auto.github <- function(lib, version=NULL, user, subdir=NULL, repo=lib)
  lib.auto(lib, version=version,
           install.fun=function(l, u, s, r) {
             require(devtools)
             install_github(r, u, subdir=s)
           },
           user, subdir, repo           
  )

The lib.auto function installs from CRAN and Bioconductor, if required. The lib.auto.github installs from GitHub.

如果需要,lib.auto函数将从CRAN和Bioconductor安装。 lib.auto.github安装自GitHub。

I am thinking about pushing this code into a package.

我正在考虑将此代码推送到一个包中。

#1


12  

This will load yaml, installing it first if its not already installed:

这将加载yaml,如果尚未安装,首先安装它:

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)
}

UPDATE. Parametrization.

UPDATE。参数化。

#2


8  

you can use installed.packages() to find installed packages

您可以使用installed.packages()来查找已安装的软件包

#3


3  

Alternatively, you can use the require function. It will try to load the package and silently return a logical stating whether or not the package is available. There is also a warning if the package cannot be loaded.

或者,您可以使用require函数。它将尝试加载包并静默返回一个逻辑,说明包是否可用。如果无法加载包,也会发出警告。

test1 <- require("stats")
test1

test2 <- require("blah")
test2

#4


1  

I am using the following construction in my code. The essential part is about calling library within tryCatch and installing it if it fails:

我在我的代码中使用以下构造。关键部分是关于在tryCatch中调用库并在失败时安装它:

lib.auto <- function(lib, version=NULL, install.fun=install.packages, ...) {
  tryCatch(
    library(lib, character.only=T),
    error=function(e) {
      install.fun(lib, ...)
      library(lib, character.only=T)
    }
  )
  if (!is.null(version)) {
    if (packageVersion(lib) < version) {
      require(devtools)
      detach(paste0('package:', lib), unload=T, character.only=T)
      install.fun(lib, ...)
      library(lib, character.only=T)
      if (packageVersion(lib) < version) {
        stop(sprintf('Package %s not available in version %s. Installed version: %s', lib, version,
                     packageVersion(lib)))
      }
    }
  }
}

lib.auto('BiocInstaller',
         install.fun=function(lib) {
           source("http://bioconductor.org/biocLite.R")
           biocLite(lib)
         })

options(repos=biocinstallRepos())
lib.auto.bioc <- lib.auto

lib.auto.github <- function(lib, version=NULL, user, subdir=NULL, repo=lib)
  lib.auto(lib, version=version,
           install.fun=function(l, u, s, r) {
             require(devtools)
             install_github(r, u, subdir=s)
           },
           user, subdir, repo           
  )

The lib.auto function installs from CRAN and Bioconductor, if required. The lib.auto.github installs from GitHub.

如果需要,lib.auto函数将从CRAN和Bioconductor安装。 lib.auto.github安装自GitHub。

I am thinking about pushing this code into a package.

我正在考虑将此代码推送到一个包中。