[R, R packages, automatic installation, project setup, code automation, dependency management, gist, automation]


How to Automatically Install R Packages Used in a Project

1 mins

Learning goals

  • Detect required packages from R scripts and R markdown files.
  • Compare and install any missing packages from the CRAN repository.

Overview

Configuring the essential R packages for your project can be time-consuming. Rather than installing each package individually, there is a simplified approach. You can identify every R package your project requires and automatically auto-install the ones missing from your system.

All you need to do is save the code block shown below as an Rscript file in the root directory of your R project, as an example, let’s name it install_packages.R.

After that, you can either run it directly or execute the command: Rscript install_packages.R in the command line.

Warning

Before proceeding, ensure that Rscript.exe is added to your system’s PATH environment variable. This ensures that the Rscript command can be recognized and executed from any command prompt or terminal session. If you’re unsure how to do this, consult the R installation guide or your system’s documentation on modifying PATH variables.

Make sure Rscript.exe command is recognized

Code

# Identify all source code files in (sub)folders
files <- list.files(pattern='[.](R|rmd)$', all.files=T, recursive=T, full.names = T, ignore.case=T)

# Extract the source code
code = unlist(sapply(files, scan, what = 'character', quiet = TRUE))

# Filter out only source code that starts with the library command
code <- code[grepl('^library', code, ignore.case=T)]
code <- gsub('^library[(]', '', code)
code <- gsub('[)]', '', code)
code <- gsub('^library$', '', code)
code <- gsub('["\']', '', code)  # Remove quotation marks

# Retain only unique package names and trim any white spaces
uniq_packages <- unique(trimws(code))

# Remove any "empty" package names
uniq_packages <- uniq_packages[!uniq_packages == '']

# Organize the list alphabetically
uniq_packages <- uniq_packages[order(uniq_packages)]

cat('Required packages: \n')
cat(paste0(uniq_packages, collapse= ', '), fill=T)
cat('\n\n\n')

# Fetch the list of currently installed packages
installed_packages <- installed.packages()[, 'Package']

# Check availability of packages for the current R version
available_packages <- rownames(available.packages(repos = 'https://cloud.r-project.org'))
to_be_installed <- setdiff(uniq_packages, intersect(uniq_packages, available_packages))

if (length(to_be_installed) == length(uniq_packages)) {
  cat('All packages need to be installed.\n')
} else if (length(to_be_installed) > 0) {
  cat('Some packages already exist; installing remaining packages.\n')
} else {
  cat('All packages installed already!\n')
}

# Install the missing packages
if (length(to_be_installed) > 0) {
  install.packages(to_be_installed, repos = 'https://cloud.r-project.org')
}

cat('\nDone!\n\n')

The output should look like this:

Succesfull packages installation

Additional Resources