2013-03-01 140 views
13

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

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’ 

任何以下错误消息为何从GGPLOT2 X变化到普通的老x?

+3

在我看来就像最后两调用'library'是多余的 - 在这一点上,如果它存在于包装应该已经被加载。 – 2013-03-01 11:04:56

回答

6

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

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

3

看一看这个漂亮的功能: klick

+0

很酷,那很漂亮! – Contango 2013-10-10 21:21:15

7

我有一天,我认为将是有益的写了这个功能...

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

谢谢你创建这个功能。我对这个函数做了一些修改,并在GitHub上发布(https://github.com/iembry-USGS/install.load)。 – iembry 2015-04-30 19:42:40

+1

非常酷!谢谢你让我知道! – maloneypatr 2015-04-30 23:17:17

+2

非常欢迎。我的下一步将是将这个软件包发布到CRAN。 – iembry 2015-05-01 01:03:05

6

的CRAN 吃豆子包,我可以保持很好地解决这个问题。使用以下头文件(确保首先安装pacman),然后p_load函数将尝试加载该程序包,然后在R无法加载程序包的情况下从CRAN中获取它们。

if (!require("pacman")) install.packages("pacman"); library(pacman) 
p_load(qdap, ggplot2, fakePackage, dplyr, tidyr) 
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") 
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")