2
library(proto)
library(gsubfn)
library(tidyr)
library(dplyr)
library(ggplot2)
library(stringr)
library(magrittr)
library(usmap)
library(RCurl)
library(RJSONIO)
library(sqldf)

For the list of libraries above I did not have corresponding line of code for package installations. I ended up googling the package names and installing them manually.

I am curious what's the best way to install all the required packages when you have a long list of library for your code set and you are not sure which are already installed in your work space or just don't know what packages to install.

Do you use require() function? Not sure if I would want to change the function to load package if the original author would have used install.package() function initially.

I would like to know a more efficient way to getting the packages installed without having to manually google and install them.

  • 1
    Using `pacman::p_load()` is better than the accepted answer imo. `p_load` can check and install both packages from `CRAN` and `BioConductor` https://cran.r-project.org/web/packages/pacman/vignettes/Introduction_to_pacman.html – Tung Sep 30 '18 at 15:28
  • 1
    Thanks Tung. P_load seems a great alternative too. – R_and_Python_noob Oct 01 '18 at 12:39

5 Answers5

6

Simply enclose the quoted package names in c() for example:

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

# Install:
install.packages(pkgs)

Then, if you also want to load the packages:

# Load:
lapply(pkgs, require, character.only = TRUE)
krads
  • 1,350
  • 8
  • 14
6

are you looking for something like this?

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

You can load a package with either library or require. The last one will not force the loading, if the package is already load, while the first one will.

PavoDive
  • 6,322
  • 2
  • 29
  • 55
  • Another great way I had not thought of. Thank you. – R_and_Python_noob Sep 30 '18 at 03:39
  • How to specify the version from one package of that list? – L F Jan 17 '23 at 20:14
  • 1
    @LF you could have two different vectors: one for the package names and the other for the version numbers and instead of the line `install.packages(i)` use `versions::install.versions(packageNames[i], packageVersions[i])`. You may want to check [this page](https://www.geeksforgeeks.org/working-with-different-versions-of-an-r-package/) – PavoDive Jan 17 '23 at 22:33
5

Check out the librarian package.

# 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)
gd047
  • 29,749
  • 18
  • 107
  • 146
2

Install only Packages that are not already available in the system. Followed by loading all the required packages.

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

Personally, I prefer to use the code snippet below, this only installs the packages that are not currently installed [Saves lots of time], while subsequently loading all the listed packages.

I would also recommend you load the package dependencies explicitly via the call to install.packages(<package list, dependencies = TRUE)

Example Code Snippet

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)

Console output

On the first call, everything is either installed and/or loaded, on the second run everything is loaded if not already loaded.

> 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 
Technophobe01
  • 8,212
  • 3
  • 32
  • 59