2013-03-01 137 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")