Help! I can’t find my library…

I’m not sure about others, but I’ve frequently encountered this problem: you try to attach a package with library() and you get an error telling you the package doesn’t exist, when you know very well that you recently installed it.

The first thing to do is to ascertain where R is looking for its libraries using .libPaths(). In my case, it gave me this output

[1] "C:/Users/Admin/R/win-library/3.4"
[2] "C:/Program Files/R/R-3.4.1/library"

The character vector on display contains 2 paths that R searches for packages. The first is supposed to be the user library, while the second is the library that contains the basic packages that come with every ‘factory-fresh’ R installation. When I looked inside “user library” with


list.files(.libPaths()[1])

I found that it was totally empty!

The problem here is that the packages I was looking for were installed in a totally different folder, which in my case was a sub-folder of “…/Documents/R/…“. And the problem arose due to how the R startup was configured (or misconfigured – only God knows what I might have done to mess things up). On reading the R documentation i.e. ?Startup the answers were there but I, for one, couldn’t really understand what was written there, but I found this page as well as this question on Stack Overflow.

One of the easier solutions is to set the environment variable R_LIBS_USER to the appropriate folder, which on Windows 7 (the machine I tried this on) can be done through Control Panel. To get this done go like this

Control Panel > User Account > Change my environment variables

and then click New/Edit (whichever is appropriate) to set/adjust the path to the folder containing the installed packages. There are other, more programmatic ways to solve this, but this is relatively simple for Windows users. The information here tells us more about how to edit PATH on different versions of Windows and it applies to any other environment variable.

When done, I quit and restarted R and found that my packages could now be loaded.

Update: I tried out the above scenario on another of my computers running on Windows 10 and it also worked. There may be a need to reboot the machine for the necessary changes to take effect.

2 thoughts on “Help! I can’t find my library…

  1. A few comments that might make life a bit easier. First, you don’t have to mess with the Windows Settings to set R_USER_LIBS and most other environment variables that are used by R. Instead, you can set it in your personal ~/.Renviron file. To find where ~/ is on Windows, do normalizePath("~"). On Windows, it typically defaults to C:/Users/alice/Documents.

    So, if you add a line R_LIBS_USER=~/R/%p-library/%v to ~/.Renviron, which you can do from R itself as cat("R_LIBS_USER=~/R/%p-library/%v\n", file = "~/.Renviron", append = TRUE), then next time you start R you should see that Sys.getenv("R_LIBS_USER") shows this settings.

    Now, the %p and %v are special and really handy because R expands them to R.version$platform and the R major and minor version, which on Windows machine I guess is x86_64-w64-mingw32 and 3.4. Thus, R_LIBS_USER=~/R/%p-library/%v will result in C:/Users/alice/Documents/R/x86_64-w64-mingw32-library/3.4. These settings are handy because you don’t have to worry updating them when you the update R version.

    Finally, if that expanded folder exists, the .libPaths()[1] will also point to it. If it does not exist, your R_LIBS_USER setting will be ignored by R.

    Liked by 1 person

    1. Thanks. I was actually hoping for this kind of feedback, because deep down in me I wasn’t happy tinkering with those settings. It’s 3 am here – later today I will try out your suggestions. Once again, thanks a lot!

      Like

Comments

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s