Install and Load Multiple R Packages

December 2, 2018
By

(This article was first published on ListenData, and kindly contributed to R-bloggers)

In enterprise environment, we generally need to automate the process of installing multiple R packages so that user does not have to install them separately before submitting your program.
The function below performs the following operations –
  1. First it finds all the already installed R packages
  2. Check packages which we want to install are already installed or not.
  3. If package is already installed, it does not install it again.
  4. If package is missing (not installed), it installs the package.
  5. Loop through steps 2, 3 and 4 for multiple packages we want to install
  6. Load all the packages (both already available and new ones).

Install_And_Load <- function(packages) {
  k <- packages[!(packages %in% installed.packages()[,”Package”])];
  if(length(k))
  {install.packages(k, repos=’https://cran.rstudio.com/’);}

  for(package_name in packages)
  {library(package_name,character.only=TRUE, quietly = TRUE);}
}
Install_And_Load(c(“fuzzyjoin”, “quanteda”, “stringdist”, “stringr”, “stringi”))

Explanation

1. installed.packages() returns details of all the already installed packages. installed.packages()[,”Package”] returns names of these packages.

To see version of the packages, submit the following command

installed.packages()[,c(“Package”,”Version”)]

2.  You can use any of the following repositories (URL of a CRAN mirror). You can experiment with these 3 repositories if one of them is blocked in your company due to firewall restriction.

https://cloud.r-project.org
https://cran.rstudio.com
http://www.stats.ox.ac.uk/pub/RWin

3. quietly = TRUE tells R not to print errors/warnings if package attaching (loading) fails.

How to check version of R while installation

In the program below, the package RDCOMClient refers repository – http://www.omegahat.net/R if R version is greater than or equal to 3.5. Else refers the repository http://www.stats.ox.ac.uk/pub/RWin

if (length(“RDCOMClient”[!(“RDCOMClient” %in% installed.packages()[,”Package”])])) {
  if (as.numeric(R.Version()$minor)>= 5)
    install.packages(“RDCOMClient”, repos = “http://www.omegahat.net/R”)
  else
    install.packages(“RDCOMClient”, repos = “http://www.stats.ox.ac.uk/pub/RWin”)
}
library(“RDCOMClient”)

To leave a comment for the author, please follow the link and comment on their blog: ListenData.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)