Batch installation of R packages

Recently, I was writing a shell script to automate the installation of some bioinformatics tools on a particular platform. One of the tools depended on the R package “VGAM”. Installing packages manually is trivial—you simply fire up R, type install.packages("YourPackageName"), select your closest mirror, and then BAM! you’re done.

I’ve used the shebang-able Rscript before to run R one-liners on the command line (try Rscript -e 'rnorm(5)' on your prompt), so I figured batch installation of packages would be just as simple, right? Well, yes and no. If you try to just run the install.packages function as is using Rscript, the command will fail since no mirror is specified.

[standage@lappy ~] Rscript -e 'install.packages("VGAM")'
Error in contrib.url(repos, type) : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> .install.macbinary -> contrib.url
Execution halted

A bit of Google searching provided a couple of solutions to this problem: either use the chooseCRANmirror function…

# Use the getCRANmirrors() function for a list of all possible mirrors

…or simply pass the mirror URL as the repos argument.

install.packages("VGAM", repos="")

I ended up going with the second one, and it worked beautifully!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s