Changing R library path

I’ve been using R to do data analysis for process improvement for some time now. I’ve learned so much in the past year that I want to write many posts in a specific order, but they end up being kept in my mind. That’s no good. This year, I’ve decided to start posting every little new thing I learn in no particular order and no particular importance. Even if it’s simple, if I’ve just learned it, I’ll share it.

So, here is my most recent problem:

I use R Studio in a corporate machine in which I’m not the admin. There, the packages were installed in a temporary folder that is cleaned from time to time. I didn’t pay attention to that until the packages were deleted and I was blocked on re-installing them. I had to repair the application from time to time. And today,  I got a new warning stating that the library was not writable.

> install.packages("lubridate")
Installing package into ‘C:/Users/Public/R/libraries_i386’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "C:/Users/Public/R/libraries_i386"' is not writable

I thought the best would be just to install the packages in a local folder that I have control over. So, I’ve added a new library to my library path.

> .libPaths( c( .libPaths(), "H:/My Documents/data analysis/R") )  
> .libPaths() 
[1] "C:/Users/Public/R/libraries_i386" 
"Q:/RSTUD301.001/R-3.2.2/library" "H:/My Documents/data analysis/R"

Alternatively, if you just add a new path:

.libPaths("H:/My Documents/data analysis/R")

The .libPaths function adds the new one as the first path to be used:

[1] "H:/My Documents/data analysis/R" "Q:/RSTUD301.001/R-3.3.1/library"

Now, every time I install a package, I select my local folder and the packages will not be deleted.

Source in StackOverflow.

2 thoughts on “Changing R library path

  1. Hi Loren,
    Yes, that is the title of the source I had included in the post as a reference.
    I’ve added an alternative at the end of the post, which works better for me than the selected answer.
    Thanks, Kenia

Leave a Reply

Your email address will not be published. Required fields are marked *