【问题标题】:best way to install required packages in r [duplicate]在 r [重复] 中安装所需软件包的最佳方法
【发布时间】:2019-03-05 13:45:37
【问题描述】:
library(proto)
library(gsubfn)
library(tidyr)
library(dplyr)
library(ggplot2)
library(stringr)
library(magrittr)
library(usmap)
library(RCurl)
library(RJSONIO)
library(sqldf)

对于上面的库列表,我没有相应的软件包安装代码行。我最终搜索了包名称并手动安装它们。

我很好奇,当您的代码集有很长的库列表并且您不确定哪些已经安装在您的工作空间中或者只是不知道要安装哪些包时,安装所有必需包的最佳方法是什么安装。

你使用 require() 函数吗?如果原作者最初使用 install.package() 函数,我不确定是否要更改加载包的函数。

我想知道一种更有效的方法来安装软件包,而无需手动谷歌并安装它们。

【问题讨论】:

标签: r package


【解决方案1】:

只需将引用的包名称括在c() 中,例如:

pkgs <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
          "stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")

# Install:
install.packages(pkgs)

那么,如果你还想加载包:

# Load:
lapply(pkgs, require, character.only = TRUE)

【讨论】:

  • 谢谢。我没有意识到我可以一次获得安装的软件包列表。
【解决方案2】:

你在寻找这样的东西吗?

listOfPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
                    "stringr","magrittr","usmap","RCurl",
                    "RJSONIO","sqldf")
for (i in listOfPackages){
     if(! i %in% installed.packages()){
         install.packages(i, dependencies = TRUE)
     }
     require(i)
}

您可以使用libraryrequire 加载包。如果包已经加载,最后一个不会强制加载,而第一个会。

【讨论】:

  • 另一种我没有想到的好方法。谢谢。
【解决方案3】:

查看librarian 包。

# attach packages to the search path, installing them from CRAN or GitHub if needed
librarian::shelf(plyr, tidyverse, knitr, ggplot2, scales, sqldf)

# List of all loaded packages
# (.packages()) 
librarian:::check_attached()

# unload
librarian::unshelf(plyr, tidyverse, knitr, ggplot2, scales, reshape2, also_depends = TRUE)
# print(.Last.value)

【讨论】:

    【解决方案4】:

    我个人更喜欢使用下面的代码sn-p,这只会安装当前没有安装的包[节省大量时间],同时后续加载所有列出的软件包。

    我还建议您通过调用 install.packages(&lt;package list, dependencies = TRUE) 显式加载包依赖项

    示例代码片段

    requiredPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
                          "stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
    
    ipak <- function(pkg){
      new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
      if (length(new.pkg))
        install.packages(new.pkg, dependencies = TRUE)
      sapply(pkg, require, character.only = TRUE)
    }
    
    ipak(requiredPackages)
    

    控制台输出

    在第一次调用时,所有内容都已安装和/或加载,在第二次运行时,如果尚未加载所有内容。

    > requiredPackages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
    +                       "stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
    > ipak <- function(pkg){
    +   new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    +   if (length(new.pkg))
    +     install.packages(new.pkg, dependencies = TRUE)
    +   sapply(pkg, require, character.only = TRUE)
    + }
    > ipak(requiredPackages)
       proto   gsubfn    tidyr    dplyr  ggplot2  stringr magrittr    usmap 
        TRUE     TRUE     TRUE     TRUE     TRUE     TRUE     TRUE     TRUE 
       RCurl  RJSONIO    sqldf 
        TRUE     TRUE     TRUE 
    

    【讨论】:

      【解决方案5】:

      仅安装系统中尚不可用的软件包。然后加载所有需要的包。

      #Installing Packages that are not already available in the system 
      list.of.packages <- c("proto","gsubfn","tidyr","dplyr","ggplot2",
                "stringr","magrittr","usmap","RCurl","RJSONIO","sqldf")
      new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
      if(length(new.packages)) install.packages(new.packages)
      
      #Loading Packages
      invisible(lapply(list.of.packages, require, character.only = TRUE))
      

      【讨论】:

        猜你喜欢
        • 2019-07-21
        • 1970-01-01
        • 2013-11-19
        • 2015-08-11
        • 2014-10-25
        • 1970-01-01
        • 1970-01-01
        • 2021-03-13
        相关资源
        最近更新 更多