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?
You can just use the return value of
require
:I use
library
after the install because it will throw an exception if the install wasn't successful or the package can't be loaded for some other reason. You make this more robust and reuseable:The downside to this method is that you have to pass the package name in quotes, which you don't do for the real
require
.You can simply use the
setdiff
function to get the packages that aren't installed and then install them. In the sample below, we check if theggplot2
andRcpp
packages are installed before installing them.In one line, the above can be written as:
This is the purpose of the rbundler package: to provide a way to control the packages that are installed for a specific project. Right now the package works with the devtools functionality to install packages to your project's directory. The functionality is similar to Ruby's bundler.
If your project is a package (recommended) then all you have to do is load rbundler and bundle the packages. The
bundle
function will look at your package'sDESCRIPTION
file to determine which packages to bundle.Now the packages will be installed in the .Rbundle directory.
If your project isn't a package, then you can fake it by creating a
DESCRIPTION
file in your project's root directory with a Depends field that lists the packages that you want installed (with optional version information):Here's the github repo for the project if you're interested in contributing: rbundler.
Yes. If you have your list of packages, compare it to the output from
installed.packages()[,"Package"]
and install the missing packages. Something like this:Otherwise:
If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.
A lot of the answers above (and on duplicates of this question) rely on
installed.packages
which is bad form. From the documentation:So, a better approach is to attempt to load the package using
require
and and install if loading fails (require
will returnFALSE
if it isn't found). I prefer this implementation:which can be used like this:
This way it loads all the packages, then goes back and installs all the missing packages (which if you want, is a handy place to insert a prompt to ask if the user wants to install packages). Instead of calling
install.packages
separately for each package it passes the whole vector of uninstalled packages just once.Here's the same function but with a windows dialog that asks if the user wants to install the missing packages
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 your 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 environment systempackrat
uses its own directory and installs all the dependencies for your programs in their and doesn't touch someone's environment.https://rstudio.github.io/packrat/