在R中安装所需软件包的最佳方法

时间:2018-09-30 03:21:21

标签: r package

//confirm button on click actions
$('#btnConfirm').on('click', function () {



  var Items = {};
  var invTrClassesObj = {};
  var _invTrClasses = $('.invTr');
  $.each(_invTrClasses, function (i, invTrClasse) {
    invTrClassesObj.ProductId.push(invTrClasse.attr('data-productId'));
  }); 


  //console.log(Items); //I want output object like this-> { ProductId:1 { Price:10.00, Quantiry:1 }, ProductId:2 { Price:20.00, Quantiry:3 } }

});

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

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

您是否使用require()函数?如果原始作者最初会使用install.package()函数,则不确定是否要更改该功能以加载软件包。

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

5 个答案:

答案 0 :(得分:2)

例如,将引号中的软件包名称简单地括在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)

答案 1 :(得分: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加载软件包。如果包装已经装载,则最后一个不会强制装载,而第一个会强制装载。

答案 2 :(得分:2)

检出<repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> 软件包。

librarian

答案 3 :(得分:1)

我个人而言,我更喜欢使用下面的代码段,这只会安装当前未安装的软件包[ 节省大量时间 ],随后再加载所有列出的软件包。

我还建议您通过调用install.packages(<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 

答案 4 :(得分:0)

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

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