Why?

November 6, 2010

Installing R packages

Filed under: R — Tags: , — csgillespie @ 9:00 am

Part of the reason R has become so popular is the vast array of packages available at the cran and bioconductor repositories. In the last few years, the number of packages has grown exponentially!

This is a short post giving steps on how to actually install R packages. Let’s suppose you want to install the ggplot2 package. Well nothing could be easier. We just fire up an R shell and type:

> install.packages("ggplot2")

In theory the package should just install, however:

  • if you are using Linux and don’t have root access, this command may not work. If you use Ubuntu then this isn’t a problem.
  • if you have limited space in /home/ then you may want to install the package in the non-default directory.
  • you will be asked to select your local mirror, i.e. which server should you use to download the package.
  • if you have a proxy, then you may run into problems.

Installing packages in a different directory

First, you need to designate a directory where you will store the downloaded packages. On my machine, I use the directory /data/Rpackages/ After creating a package directory, to install a package we use the command:

> install.packages("ggplot2"
, lib="/data/Rpackages/")
> library(ggplot2, lib.loc="/data/Rpackages/")

It’s a bit of a pain having to type /data/Rpackages/ all the time. To avoid this burden,  we create a file .Renviron in our home area, and add the line R_LIBS=/data/Rpackages/ to it. This means that whenever you start R, the directory /data/Rpackages/ is added to the list of places to look for R packages and so:

> install.packages("ggplot2")
> library(ggplot2)

just works!

Setting the repository

Every time you install a R package, you are asked which repository R should use. To set the repository and avoid having to specify this at every package install, simply:

  • create a file .Rprofile in your home area.
  • Add the following piece of code to it:


cat(".Rprofile: Setting UK repository\n")
r = getOption("repos") # hard code the UK repo for CRAN
r["CRAN"] = "http://cran.uk.r-project.org"
options(repos = r)
rm(r)

Or as Hadley pointed out, this could be condensed to:

cat(".Rprofile: Setting UK repository\n")
options(repos = c(CRAN = "http://cran.uk.r-project.org/"))

but you would deleted any other repositories you have set – say the r-forge repository (thanks to Kevin for pointing this out).

Obviously, you don’t need the cat statement, but I find it helpful to remind me that I’ve set this default. Especially if you have a laptop and you a lot of travelling.

I found this tip in a stackoverflow answer .

Setting your http proxy

This section is an update and follows a comment from Sean Carmody below.

If you have a http proxy, then one method of setting the proxy in R is to use the sys.system command, viz.

Sys.setenv(http_proxy="http://myproxy.example.com:8080")

Sys.setenv(http_proxy=”http://myproxy.example.com:8080″)

About these ads

9 Comments »

  1. That’s a really convoluted way to go about setting the CRAN mirror. Try comething like options(repos = c(CRAN = "http://cran.r-project.org/")

    Comment by Hadley Wickham — November 7, 2010 @ 1:09 pm

    • Thanks. I’ve updated the post.

      Comment by csgillespie — November 7, 2010 @ 2:23 pm

    • Hadley, your method erases the existing options. The “convoluted” way preserves existing options.

      Comment by Kevin Wright — January 24, 2011 @ 6:12 pm

      • To preserve current repos while adding another you could write the following:
        options(repos = c(getOption(“repos”),CRAN = “http://cran.uk.r-project.org”))

        Comment by dgrapovdgrapov — September 19, 2013 @ 10:47 pm

  2. I can not speak for other distros, but install.packages() works just fine on Ubuntu without root access. When used without root access, it creates a folder (on my 64-bit machine) called ‘~/R/x86_64-pc-linux-gnu-library/’.

    If you are using Ubuntu, this complexity seems unnecessary.

    Comment by gunksta — November 7, 2010 @ 1:46 pm

    • Thanks for the comment. We’re about to move to Ubuntu from our current setup (a bespoke debian stable), so that’s good to know. The other reason I would continue to set R_LIB is that we back up /home/ fairly regularly (every 12 hours) and have a fairly limited storage for our roaming profile. Since I use a lot of large bioconductor packages, it would be easy for me to go over this limit.

      Anyway, I’ll a note in the post mentioning that this may not be necessary. Thanks again.

      Comment by csgillespie — November 7, 2010 @ 2:13 pm

  3. If you are behind a corporate proxy and you know the address of the http proxy, you can still do all of this if you first set

    Sys.setenv(http_proxy="http://myproxy.example.com:8080")

    You can even pass a command line argument http_proxy="http://myproxy.example.com:8080" when you launch R to ensure it’s always set.

    Comment by seancarmody — November 8, 2010 @ 3:13 am

    • Good tip. I tend to set the proxy in my .cshrc init file. But there have been occasions where I need to set in R explicitly – for example if I use Windows or a Mac.

      Comment by csgillespie — November 8, 2010 @ 10:11 am

  4. You wrote:

    Or as Hadley pointed out, this could be condensed to:
    cat(".Rprofile: Setting UK repository\n")
    options(repos = c(CRAN = "http://cran.uk.r-project.org/"))

    This will only set ONE repository, and erases all others. The tip previous to this one would ADD one repository to the existing repositories.

    Comment by Kevin Wright — January 24, 2011 @ 6:10 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.