检查是否安装了R包然后加载库

时间:2013-03-01 10:44:09

标签: r

我们的R脚本用于多台计算机上的多个用户,因此每台计算机上都安装了软件包。为了确保每个脚本都适用于所有用户,我想定义一个函数pkgLoad,它将首先测试是否在本地安装软件包,然后再使用抑制的启动消息加载库。以Check for installed packages before running install.packages()为指导,我尝试了

 pkgLoad <- function(x)
  {
    if (!require(x,character.only = TRUE))
    {
      install.packages(x,dep=TRUE, repos='http://star-www.st-andrews.ac.uk/cran/')
      if(!require(x,character.only = TRUE)) stop("Package not found")
    }
    #now load library and suppress warnings
    suppressPackageStartupMessages(library(x))
    library(x)
  }

当我尝试使用pkgLoad(“ggplot2”)加载ggplot2时,我在终端中收到以下错误消息

Error in paste("package", package, sep = ":") : 
  object 'ggplot2' not found
> pkgLoad("ggplot2")
Loading required package: ggplot2
Error in library(x) : there is no package called ‘x’
> pkgLoad("ggplot2")
Error in library(x) : there is no package called ‘x’

为什么x从ggplot2变为普通的旧x?

6 个答案:

答案 0 :(得分:11)

我前几天写了这个函数,我觉得它很有用......

install_load <- function (package1, ...)  {   

   # convert arguments to vector
   packages <- c(package1, ...)

   # start loop to determine if each package is installed
   for(package in packages){

       # if package is installed locally, load
       if(package %in% rownames(installed.packages()))
          do.call('library', list(package))

       # if package is not installed locally, download, then load
       else {
          install.packages(package)
          do.call("library", list(package))
       }
   } 
}

答案 1 :(得分:8)

我维护的CRAN pacman 包可以很好地解决这个问题。使用以下标题(以确保首先安装 pacman ),然后char函数将尝试加载包,然后如果R无法加载包,则从CRAN获取它们

char

答案 2 :(得分:7)

使用library(x,character.only=TRUE)。此外,您不需要最后一行,因为suppressPackageStartupMessages(library(x,character.only=TRUE))已加载包。

编辑:@LarsKotthoff是对的,你已经在if括号内加载了包。在那里你已经使用了选项character.only = TRUE所以如果你只删除函数体的最后一行,一切都很好。

答案 3 :(得分:3)

看看这个不错的功能: klick

答案 4 :(得分:1)

虽然@maloneypatr函数运行正常但是它非常安静,并且不会响应加载的包的成功。我构建了下面的功能,它确实对用户输入进行了一些检查,并且还响应了成功安装的包的数量。

lubripack <- function(...,silent=FALSE){

  #check names and run 'require' function over if the given package is installed
  requirePkg<- function(pkg){if(length(setdiff(pkg,rownames(installed.packages())))==0)
                                    require(pkg, quietly = TRUE,character.only = TRUE)
                            }

  packages <- as.vector(unlist(list(...)))
  if(!is.character(packages))stop("No numeric allowed! Input must contain package names to install and load")

  if (length(setdiff(packages,rownames(installed.packages()))) > 0 )
     install.packages(setdiff(packages,rownames(installed.packages())),
                      repos = c("https://cran.revolutionanalytics.com/", "http://owi.usgs.gov/R/"))

  res<- unlist(sapply(packages, requirePkg))

  if(silent == FALSE && !is.null(res)) {cat("\nBellow Packages Successfully Installed:\n\n")
                    print(res)
                   }
}

注1:

如果silent = TRUE(所有资本无声),它会安装并加载包而不报告。如果silent = FALSE,则报告成功安装包。默认值为silent = FALSE

如何使用

lubripack(“pkg1","pkg2",.,.,.,.,"pkg")

示例1:当所有包都有效且模式不是静默时

lubripack(“shiny","ggvis")

lubripack(“shiny","ggvis", silent = FALSE)

输出

enter image description here

示例2:当所有包都有效且模式为静默时

lubripack(“caret","ggvis","tm", silent = TRUE) 

输出2

enter image description here

示例3:无法找到包时

lubripack(“shiny","ggvis","invalidpkg", silent=FALSE) 

输出3

enter image description here

如何安装包:

运行以下代码下载软件包并从GitHub安装。无需拥有GitHub帐户。

library(devtools)
install_github("espanta/lubripack")

答案 5 :(得分:1)

可以使用以下内容:

check.and.install.Package<-function(package_name){
    if(!package_name%in%installed.packages()){
        install.packages(package_name)
    }
}

check.and.install.Package("RTextTools")
check.and.install.Package("e1071")
相关问题