2
votes
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.

5
Using pacman::p_load() is better than the accepted answer imo. p_load can check and install both packages from CRAN and BioConductor cran.r-project.org/web/packages/pacman/vignettes/…Tung
Thanks Tung. P_load seems a great alternative too.R_and_Python_noob

5 Answers

6
votes

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.

5
votes

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)
5
votes

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

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 
1
votes

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