Tuesday 16 April 2013

Getting RStudio to include your `R_LIBS_USER` in its library paths.

I recently installed RStudio Desktop version on my Linux computer, as I wanted to test it out. Previously I'd been happily using R from the command-line.

However, it kept complaining that I didn't have the knitr package installed.

Starting R from the command-line, I found that I did have knitr installed - library(knitr) worked fine! But library(knitr) from RStudio gave the errror:

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

Upon further inspection, I realised that my package library paths were between R-command-line and RStudio, despite the R executable being identical between the two.

My usual R library for command-line R is ~/R/library, and this was in the .libPaths() as expected (executed from R from the command line).

However, when I executed .libPaths() from RStudio, I got:

[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"            "/usr/lib/rstudio/R/library"

So why did the same R binary produce different library paths?

It turns out that I set my default path ~/R/library by setting my R_LIBS_USER environment variable in my .bashrc file:

export R_LIBS_USER=$HOME/R/library

but RStudio was not reading my .bashrc file when it started up (makes sense I guess, as it's not running from the terminal).

The solution was to create a file ~/.Renviron and set the R_LIBS_USER variable there. R looks for this file upon starting up to set environment variables (see also ?Startup):

R_LIBS_USER=~/R/library

(Note - I could also just do .libPaths(c('~/R/library', .libPaths()) in an .Rprofile file, but I don't use those in general).

Now I start up RStudio and hey presto! It all works.

Thanks to the folk at StackOverflow, in particular flodel and Dirk EddelBuettel, who helped me work this out (the question has probably been deleted since then as it was too localized and probably should have been asked at the RStudio support page).

2 comments:

  1. please i am still struggling to change the default library to my own on my mac. please help

    ReplyDelete
  2. Please, most of the command lines are not explicit. if you could just type what i can copy to my command line, it will be great for me. i've tried all those above (may be my understanding too is limited) but please come down and give something i can copy and paste on my command line

    ReplyDelete