I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and don't realize that they have to install packages they don't already have.
Is there an elegant way to call installed.packages()
, compare that to the ones I am loading and install if missing?
Although the answer of Shane is really good, for one of my project I needed to remove the ouput messages, warnings and install packages automagically. I have finally managed to get this script:
Use:
The coming version of RStudio (1.2), already available as a preview, will include a feature to detect missing packages in
library()
andrequire()
calls, and prompt the user to install them:This seems to address the original concern of OP particularly well:
Use
packrat
so that the shared libraries are exactly the same and not changing other's environment.In terms of elegance and best practice I think you're fundamentally going about it the wrong way. The package
packrat
was designed for these issues. It is developed by RStudio by Hadley Wickham. Instead of them having to install dependencies and possibly mess up someone's environment system,packrat
uses its own directory and installs all the dependencies for your programs in there and doesn't touch someone's environment.https://rstudio.github.io/packrat/