Confusion with .libPaths during package install - r

This will probably be answered by someone directing me to a help file but I'm clearly missing it thus far. My quandry is that when I install a package (sometimes), it does not appear to default to the contents of my .libPaths - instead it appears to want to install to a network path to which it does not have permission.
Here is my library path
>.libPaths()
[1] "C:/Program Files/R/R-3.3.0/library"
If I try to install this package without specifying the lib argument it seems like it is trying to save to a network folder (I have changed the name of the network drive in the error message but you get the idea):
> install.packages("rmarkdown")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/rmarkdown_0.9.6.zip'
Content type 'application/zip' length 2182803 bytes (2.1 MB)
downloaded 2.1 MB
package ‘rmarkdown’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\my.name\AppData\Local\Temp\RtmpkFVHnn\downloaded_packages
Warning message:
In dir.create(configDir, recursive = TRUE) :
cannot create dir '\\server.name\server_directory', reason 'Permission denied'
However, if I specify .libpaths() in the "lib" argument, then it works. But isn't this the default to the lib argument?
> install.packages("rmarkdown",lib=.libPaths())
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.3/rmarkdown_0.9.6.zip'
Content type 'application/zip' length 2182803 bytes (2.1 MB)
downloaded 2.1 MB
package ‘rmarkdown’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\jordan.watson\AppData\Local\Temp\RtmpkFVHnn\downloaded_packages
This does not happen every time however. For example, I just loaded the "forecast" library no problem by simply writing install.packages("forecast").
I'd just like to understand this behavior better. Can someone explain why this is happening? I am on Windows 7 without full admin privileges - but I do have admin privileges to my R folder on the C drive.
Apologies in advance that I can not make a reproducible example - it's not really that type of question.

Jordan,
My feeling is your setup is incorrect.
Proposed Solution
Try running the following command and see if this fixes the situation.
Note: I have replaced my userid with <user> in the examples below.
.libPaths( c( "C:/Users/<user>/Documents/R/win-library/3.3", .libPaths()) )
Explanation
I would expect .libPaths() to return two directories not one by default.
> .libPaths()
[1] "C:/Users/<user>/Documents/R/win-library/3.3" "C:/Program Files/R/R-3.3.0/library"
When you install R it installs the base libraries under
C:/Program Files/R/R-3.3.0/library
any new libraries are normally added to:
"C:/Users/<user>/Documents/R/win-library/3.3"
if you type View(installed.packages()) you will be able to see where the packages are installed.
The packages that come with the R install (base, stats, and nearly 30 others) are stored in the library subdirectory of wherever you installed R. You can retrieve the location of this with:
> R.home("library")
[1] "C:/PROGRA~1/R/R-33~1.1/library"
> .Library
[1] "C:/PROGRA~1/R/R-33~1.1/library
You also get a user library for installing packages that will only be accessible by you. The location under Windows, for R version x.y.z, it is in the R/win-library/x.y subfolder of the home directory, where the home directory can be found via:
> path.expand("~")
[1] "C:/Users/<user>/Documents"
> Sys.getenv("HOME")
[1] "C:/Users/<user>/Documents"
Issue
Thus, I would expect .libPaths() to return two paths not one which implies that you have somehow misconfigured the R installation libpath.
I hope the above helps.
Refs:
https://stat.ethz.ch/R-manual/R-devel/library/base/html/libPaths.html
Learning R By: Richard Cotton

Related

Installation directory?

I'm trying to install Tidytext package. It seems to me that R is installing the package into my OneDrive. I've been using R and I've not run into this problem before. I've unsynchronized One Drive and done a variety of things to change my working drive, but I still get the following message when installing Tidytext package -
Installing package into "C:/Users/Anjan/OneDrive/Documents/R/win-library/3.5"
(as "lib" is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/tidytext_0.2.0.zip'
Content type 'application/zip' length 2836849 bytes (2.7 MB)
downloaded 2.7 MB
I've unsynchronized One Drive from my Documents folder on This PC.
I've checked getwd()
I've set setwd()
I've used Tools and Global Options and changed my working directory.
But R still uses One Drive. How do I get R to not use One Drive?
install.packages("tidytext")
Installing package into "C:/Users/Anjan/OneDrive/Documents/R/win-library/3.5"
(as "lib" is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/tidytext_0.2.0.zip'
Content type 'application/zip' length 2836849 bytes (2.7 MB)
downloaded 2.7 MB
package ‘tidytext’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Anjan\AppData\Local\Temp\RtmpYDylTE\downloaded_packagesinstall.packages("magritte")
Installing package into "C:/Users/Anjan/OneDrive/Documents/R/win-library/3.5"
(as "lib"is unspecified)
Warning in install.packages :
package ‘magritte’ is not available (for R version 3.5.3)
getwd()
[1] "C:/Users/Anjan/OneDrive/Documents"
Use this to see actual library paths:
.libPaths()
And the same to set another destination folder to install:
.libPaths(PATH_THAT_YOU_WANT)
You can also specify a location like we see here:
Install R package to a specific directory
Take a look into the docs:
https://rdocumentation.org/packages/base/versions/3.6.1/topics/libPaths
It's a bug, reported at https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17842.
R should never put libraries in a user's documents folder. That is an inappropriate use of that folder, and there are better places for this kind of data.

Changing packages directory in Rprofile

I have a project called "R installation info"
And a sub directory called "Project Packages".
I am trying to configure R in a manner that when I
Install a new package it will be installed to "Project Packages".
Also when I have an R session with that project I would like to see in RStudio in Packages folder only the packages that were installed in "Project Packages".
I created an Rprofile file in the project directory
And put the following lines in it:
.libPaths = paste0(getwd(),"/","Project Packages")
Sys.setenv(R_LIBS_USER = paste0(getwd(),"/","Project Packages"))
However when I started a new session with that project
And installed a new package like that:
> install.packages("magrittr")
Installing package into ‘C:/Users/Rafael Zanzoori/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/magrittr_1.5.zip'
Content type 'application/zip' length 155955 bytes (152 KB)
downloaded 152 KB
package ‘magrittr’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Rafael Zanzoori\AppData\Local\Temp\RtmpOQWtgi\downloaded_packages
I see that the package was installed to a different directory from what I specified:
‘C:/Users/Rafael Zanzoori/Documents/R/win-library/3.4’
Instead of:
C:/Users/Rafael Zanzoori/Documents/R Projects/R Installation info/Project Packages
We can see from the following that the path for new packages is
> .libPaths
[1] "C:/Users/Rafael Zanzoori/Documents/R Projects/R Installation info/Project Packages"
> Sys.getenv("R_LIBS_USER")
[1] "C:/Users/Rafael Zanzoori/Documents/R Projects/R Installation info/Project Packages"
Also when I click on "install" button on package folder in RStudio
I see in "Install to Library" textbox :
‘C:/Users/Rafael Zanzoori/Documents/R/win-library/3.4’
Can you please help me understand why my Rprofile definitions for installing packages are ignored by R and how to fix it?
I tried to search for relevant posts but could not find anything that will help
Thanks
Rafael
Finally i managed to solve the problem
by renaming
‘C:/Users/Rafael Zanzoori/Documents/R/win-library/3.4’
to something else.
Since then R is using the definitions in my project's Rprofile.
I don't know what made R to override my project Rprofile since i didn't have any other Rprofile
But changing the old directory name solved the problem

installation path not writable R, unable to update packages

I am trying to install Bioconductor into R, using the code on their website. When I type in the code (see bellow) I get an error message saying that some packages can't be updated, the installation path is unwriteable.
> ## try http:// if https:// URLs are not supported
> source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help
> biocLite()
BioC_mirror: https://bioconductor.org
Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.2 (2016-10-31).
installation path not writeable, unable to update packages: Matrix, mgcv,
survival
I can install these package by going to packages/install packages.
> utils:::menuInstallPkgs()
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/Matrix_1.2-8.zip'
Content type 'application/zip' length 2775038 bytes (2.6 MB)
downloaded 2.6 MB
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/mgcv_1.8- 16.zip'
Content type 'application/zip' length 2346257 bytes (2.2 MB)
downloaded 2.2 MB
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/survival_2.40-1.zip'
Content type 'application/zip' length 5109948 bytes (4.9 MB)
downloaded 4.9 MB
package ‘Matrix’ successfully unpacked and MD5 sums checked
package ‘mgcv’ successfully unpacked and MD5 sums checked
package ‘survival’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\stxeb8\AppData\Local\Temp\Rtmp2tQZ4v\downloaded_packages
I can then go to packages/ load packages and load them succesfully and search and see that the packages are there.
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
Loading required package: nlme
This is mgcv 1.8-16. For overview type 'help("mgcv-package")'.
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> search()
[1] ".GlobalEnv" "package:survival" "package:mgcv"
[4] "package:nlme" "package:Matrix" "package:BiocInstaller"
[7] "package:stats" "package:graphics" "package:grDevices"
[10] "package:utils" "package:datasets" "package:methods"
[13] "Autoloads" "package:base"
But then when I go to install bioconductor it gives me the same error message that Matrix, mgcv and survival aren't able to be updated.
> ## try http:// if https:// URLs are not supported
> source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help
> biocLite()
BioC_mirror: https://bioconductor.org
Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.2 (2016-10-31).
installation path not writeable, unable to update packages: Matrix, mgcv,
survival
What can I do to be able to update these packages so I can install bioconductor?
In general, I would advise against changing the permission in system folders, because R should work without additional administrative rights.
Thus I would likewise advise against installing packages using administrative rights, as you would be required to do so in the future every time you have to update these packages!
To backtrace this issue and prevent to minimize it in future updates, you should perform the following steps:
Note which packages that fail to update (already shown in the error message).
Locate the folders where all R packages are installed using .libPaths(). This should provide two results, a destination in your home folder and a system folder.
"/home/USER/R/x86_64-pc-linux-gnu-library/X.X" "/usr/lib/R/library"
Install the packages with install.packages(c("PKG1", "PKG2", "PKG3")) or BiocManager::install(c("PKG1", "PKG2", "PKG3"))*
Only If you have administrator rights: Manually remove the older package folders from the system folder ("/usr/lib/R/library"), using administrator rights (sudo) OR enter R with administrator rights One last time and run remove.packages(c("PKG1", "PKG2", "PKG3"), lib = "/usr/lib/R/library").
*If there are issues with the installation path, add the argument , lib = "/home/USER/R/x86_64-pc-linux-gnu-library/X.X" to either of the install functions in step 3. This argument explicitly states to install in your home folder.
There is a single issue with this approach, at least with the official R repository on Arch Linux: Whenever R is updated, the updated version still includes packages in the system folder, that can't be updated without administrative rights. Therefore for each R update, this procedure must be repeated. I'm especially looking at you survival!!!
*Edit: It is important to note that biocLite is no longer the recommended tool for installing BioConductor packages. You should instead use BiocManager, which is in the official CRAN repository (install.packages("BiocManager")).
**Second edit: As this answer still receives votes, I have updated and cleaned up the answer.
It was a permission issue for me. First, I identified where the packages were installed using installed.packages()[, c("Package", "LibPath")]. This outputs a long 2 column matrix with the names and locations of the packages. Then you will see where the offending packages are. In my case, they were at /usr/lib/R/site-library and /usr/lib/R/library. Then I changed the permission of these folders by chmod (I used chmod -R 777 on the main R folder, this is my personal computer, so security is not a big concern here I think).
If you are running R/Rstudio on Windows, then just open R/Rstudio as administrator. Right click on the icon then run as administrator
It looks like several 'recommended' packages are installed in two places -- maybe by an administrator account in a directory you don't have write access to, and then by RStudio in a directory where you do have write access. biocLite() is complaining about the former.
Unless biocLite() complains about a Bioconductor package that cannot be installed (different from cannot be updated), there is no problem and basic Bioconductor packages have been successfully installed. Check out https://support.bioconductor.org for future Bioconductor-related support.
One solution is to open a terminal and load R using admin rights
sudo R
update.packages()
source("https://bioconductor.org/biocLite.R")
biocLite()
Then you can update. But careful. This can create packages by the admin in a directory supposed to be owned by a user.
In this case, instead of loading R as root (which is solving the problem until the next update), check the .libPaths(). You will have a list of directories.
.libPaths()
"/home/it_s_me/R/x86_64-pc-linux-gnu-library/3.4" "/usr/lib/R/library"
In my case, all packages in "/usr/lib/R/library" are owned by root, and all except one are owned by a normal user (not root) at "/home/itsame/R/x86_64-pc-linux-gnu-library/3.4".
If you have admin rights, an easy solution may be to run chown in all the places: For example, I had trouble updating the curl package. I used:
sudo chown -R it_s_me /home/it_s_me/R/x86_64-pc-linux-gnu-library/3.4/curl/
I had a similar problem with other Bioconductor packages and the solution was simpler than I thought. I had to install some OS packages pointed out on my log: libcurl-dev, libcurl4-openssl-dev, libssl-dev.
My advice is to check your R log and look for "Configuration failed because [PACKAGE] was not found. Try installing".
Since with the version of R 3.6.1 the script http://bioconductor.org/biocLite.R returns this message " Error: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install " I solved the problem with the following steps:
Get the list of directories used by R to install libraries and
choose the one with write permissions using: .libPaths()
Installing the "BiocManager" library using: install.packages("BiocManager")
Installation of the library "bioconductor" by forcing the directory
with write permissions using: BiocManager::install("Rgraphviz", lib = "C:/Users/tizbet/Documents/R/win-library/3.6")
I worked on the following R installation:
platform x86_64-w64-mingw32, arch x86_64, os mingw32, system x86_64, mingw32, version.string R version 3.6.1 (2019-07-05)
I was inspired by the Kasper Thystrup Karstensen answer
Whenever you want to work on an R-code where packages need to be installed: -
Windows: Open R-studio as 'Run as administrator' [Right click on the icon]
Linux: Run as sudo user; else get writing rights from the administrator
Ubuntu:
sudo apt install libcurl-dev
Fedora:
sudo dnf install libcurl-devel

Error in loadNamespace Rstudio

I am having real trouble understanding what my problem is installing ggplot package in Rstudio. I have tried a number of the post here and here but I still can't fix the problem.
I understand that I have a library directory on my Windows machine which can be found using:
.libPaths() which returns
[1] "C:/Users/Bo Bo/Documents/R/win-library/3.2" "C:/Program Files/R/R-3.2.3/library"
I'm not sure what is stored in this directory using .Library but it looks to be pointing to the wrong directory, 3.2.3 and 32~1.3???:
[1] "C:/PROGRA~1/R/R-32~1.3/library"
Upon installing ggplot2
install.packages("ggplot2") returns:
Installing package into ‘C:/Users/Bo Bo/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.2/ggplot2_2.1.0.zip'
Content type 'application/zip' length 2001769 bytes (1.9 MB)
downloaded 1.9 MB
package ‘ggplot2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Bo Bo\AppData\Local\Temp\Rtmp4QKdwV\downloaded_packages
So is it dumping the package in a temp directory because the 'lib' is unspecified? If so how do I specify the 'lib' and which library needs to change.
By the way I followed #Paweł Rumian post in link 2 above meaning I have a new User variable called R_LIBS_USER pointing to C:/Program Files/R/R-3.2.3/library
Can anyone make a suggestion as to how I can resolve this conflict and move on with my life :)
So I ended up re-installing windows after hours of trying to modify/fix the permissions issue I was having with all folders on the local disk.
Since re-installing I have installed Rstudio as per before and I am back in business, no messing around with library locations, just plain vanilla for me please...

R not finding package even after package installation

I have always worked with the zoo package, that I have installed a long time ago. Today, I created a new R script, and ran library(zoo) and got the following error:
> library(zoo)
Error in library(zoo) : there is no package called ‘zoo’
Odd.. Still, I tried reinstalling the package using install.packages("zoo"), and get the following:
> install.packages("zoo")
Installing package(s) into ‘C:/Users/U122337.BOSTONADVISORS/Documents/R/win-library/2.15’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.cnr.Berkeley.edu/bin/windows/contrib/2.15/zoo_1.7-10.zip'
Content type 'application/zip' length 874474 bytes (853 Kb)
opened URL
downloaded 853 Kb
package ‘zoo’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘zoo’
The downloaded binary packages are in
C:\Users\U122337.BOSTONADVISORS\AppData\Local\Temp\Rtmp404t8Y\downloaded_packages
When I try uploading the package, I get the error again that the zoo package doesn't exist.
I have no idea what's happening. I exited the GUI and restarted it, same problem. I have always worked with this package, and I have no idea why this is happening now.
Do .libPaths(), close every R runing, check in the first directory, remove the zoo package restart R and install zoo again. Of course you need to have sufficient rights.
When you run
install.packages("whatever")
you got message that your binaries are downloaded into temporary location (e.g. The downloaded binary packages are in
C:\Users\User_name\AppData\Local\Temp\RtmpC6Y8Yv\downloaded_packages ). Go there. Take binaries (zip file). Copy paste into location which you get from running the code:
.libPaths()
If libPaths shows 2 locations, then paste into second one. Load library:
library(whatever)
Fixed.
I had this problem and the issue was that I had the package loaded in another R instance. Simply closing all R instances and installing on a fresh instance allowed for the package to be installed.
Generally, you can also install if every remaining instance has never loaded the package as well (even if it installed an old version).
So the package will be downloaded in a temp folder C:\Users\U122337.BOSTONADVISORS\AppData\Local\Temp\Rtmp404t8Y\downloaded_packages from where it will be installed into your library folder, e.g. C:\R\library\zoo
What you have to do once install command is done: Open Packages menu -> Load package...
You will see your package on the list. You can automate this:
How to load packages in R automatically?
go to this folder C:\Users\hp\Documents\R\win-library\4.0 in your PC.
And delete '00LOCK' named folder.
then It will work properly, THis folder is being locked all incoming file.IF your delete this , then it will work.

Resources