1

I should install many different R packages.

I prepared the file requirements.R (example below):

install.packages("mongolite", repos="https://cran.rstudio.com")
install.packages("xgboost", repos="https://cran.rstudio.com")

How can I install all of them, for example, from command line?

Should I somehow use devtools::load_all?

Fluxy
  • 2,838
  • 6
  • 34
  • 63
  • 4
    `install.packages` is vectorized, you can pass in a character vector into `pkgs` argument – chinsoon12 Jan 15 '20 at 09:24
  • 1
    Alternatively just use the source("requirements.R") command... – Marco Jan 15 '20 at 09:27
  • 1
    `source("requirements.R")` is exactly what I need. But can I run it from command line or using `devtools`, so that in the script I can only do `library...`? – Fluxy Jan 15 '20 at 10:02
  • @David Arenburg: In your reommended posts I didn't find the answer to my question - how to install R packages from command line – Fluxy Jan 15 '20 at 10:28
  • @Fluxy Related post? https://stackoverflow.com/q/4090169/680068 – zx8754 Jan 16 '20 at 08:14

1 Answers1

1

You can write a little function like this one, it also checks if the packages required are already installed and if that's the case it only loads them:

get.packages <- function(packages, Base_R_Best_R = F){
  if(Base_R_Best_R){
    print("No packages required!")
  }
  else{
  for(i in seq.int(length(packages))){
    if(!require(packages[i], character.only = T)){
      install.packages(packages[i])
    }
    library(packages[i], character.only = T)
  }
}
}
#example
#get.packages(c("dplyr", "installr", "Amelia")

Edit Option to not install any packages as Base R is best R.

fabla
  • 1,806
  • 1
  • 8
  • 20