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
 "C:/Users/Admin/R/win-library/3.4"  "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
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.