1

I want to make sure I always have my most commonly used R packages installed without my intervention. This is useful as I work across multiple computers, often reformat my computers, and sometimes have new packages that I want to add to this "commonly used" list and don't want to have to manually install it on every machine. Basically, I just want R to install missing packages for me whenever it starts up.

Currently I'm using a .Rprofile that installs my package list whenever I start R. It contains the following:

packages <- c("list", "of", "packages")

for (p in packages) {
  if(!suppressWarnings(require(p, character.only = TRUE))){
    utils::install.packages(p, repos='http://cran.us.r-project.org')
  }
}

There are a couple of issues:

  1. Running install.packages when the package is already installed seems to download the package (or meta data?) from the repo, and maybe attempts a reinstall
  2. I'm using require to check whether a package is already installed, but that has the side effect of also loading the package. I don't want any package loading to happen, only install (if needed)

How do I set this up so that 1) a package will be installed if it isn't already, and 2) not load the package at startup? I think ultimately what I'm looking for is a way to check whether a package is installed without having to load it

I've considered two ways around this, but neither are ideal and would like to avoid them as solutions:

  • Use a package manager like Packrat. I'm not a fan of essentially creating managed virtual environments for all of my projects. It would take up unnecessary space to have multiple copies of the same package files on my machines
  • A common solution to this problem (e.g. Elegant way to check for missing packages and install them?) is to check whether a package is found in installed.packages(). But this ends up being really slow (which is especially a problem at startup) if you have a lot of installed packages

I've seen many questions about this, but all of the solutions I've seen either load packages using require or checks against installed.packages(). Is there another way?

Simon
  • 9,762
  • 15
  • 62
  • 119

0 Answers0