Quickly checking for (and installing) required R packages

Jacob Kreider
Understanding Data Science
2 min readFeb 12, 2019

If you’re anything like me, at some point in your life you have gone to run an R script that you either found online or were checking for a friend or coworker, only to realize that it required several packages you’ve never used before. Manually installing packages is easy, of course, but it can be even easier with just a few lines of code.

By checking to see if the required packages are installed on the machine in use, then automatically installing those that are missing, we get (and give) several benefits:

  • Reproducibility increases- scripts can be easily shared and tested by others, without wasting their time, forcing them to install the required packages manually;
  • Upgrading or switching to a new machine is less of a hassle- no more, “Ok, I know I use {ggplot2}, {data.table}, {caret}, and {tidyverse} all the time- but what others should I add to this mass install?” Just run some old scripts and you’re done (or, check here for a simple Python script to search your system for every package you’ve ever used and install them all);
  • For folks moving into cloud versions of R studio, that may not save your added packages permanently, it reduces time spent replenishing your library when starting a new instance.

The steps are pretty straightforward:

  • First, we’ll create an object called list.of.packages that contains every package we’ll need to use;
  • Then, we’ll pull out any packages already installed on our system and save the ones left as…wait for it…new.packages;
  • Finally, assuming new.packages isn’t empty, we’ll install everything in it. And, just for good measure, we’ll go ahead and require all packages in list.of.packages with a single line.

Let’s see how it works:

Pretty straightforward, right? We’ve checked the system for what we need, installed what we didn’t have, then loaded everything up at once.

Here’s another version, this time with real packages listed. Try it out on your own system and see what you think. (As an added bonus, for those of you just starting out with data science and R, this is a very useful list of packages to have in your toolbox- enjoy!

Let me know in the comments what you think, or share any methods you use to handle this.

--

--

Jacob Kreider
Understanding Data Science
0 Followers

I am a data scientist living in southeast PA. Currently enrolled in the MSDS program at Northwestern University.