Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
R
 
 
man
 
 
 
 
 
 
 
 
 
 

README.md

librarian

Because missing packages shouldn't trip up scripts

How often have you been running a little R script, minding your own business, when out of nowhere...

Error in library(somePackage) : there is no package called ‘somePackage’

And the whole process stops. You know it's not a huge deal, but it feels like this:

This isn't a complete solution for that problem, but it's a fist step.

Installation

devtools::install_github('aaboyles/librarian')
library('librarian')

Usage

load_package('somePackage')

Wait, why?

Loading dependencies in R is terrible. Here's why:

library isn't context-revealing

In R, a chunk of code you can bring into your session is called a library. Oh crap, wait, actually it's not. It's called a package. You use the library function to load a package. This is because *mumble mumble* reasons.

require is usually poor form

Since it feels like a misnomer to use library to load your R packages, what about require instead? require appeals to intuition, since it doesn't name the thing you're requiring, but it's a perfectly logical choice in any language for bringing in new modular chunks.

Unfortunately, R's require is terrible for a much more important reason: it doesn't stop the script if it fails. This is a huge pain in the neck if you require at the top of a script and the thing that didn't get loaded is much later.

Neither tries very hard

Neither library nor require makes any attempt to rectify a missing package. So, to solve this, lots of R hackers have written some variation of this:

if(!require(somePackage)){
  install.packages(somePackage)
  library(somePackage)
}

In other words, if you haven't already, install a package, and attach it. But no more! Use this instead!

library(librarian)
load_package(somePackage)

This does it for you!

About

An R Package to smooth out other loading other R packages

Resources

Releases

No releases published

Packages

No packages published

Languages

You can’t perform that action at this time.