Changing packages directory in Rprofile - r

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

Related

Unable to load an R package when installed from a custom directory

I already referred this post,post,
I am trying to install an R package called Aphrodite.
So, I downloaded the zip folder of R package from github and tried the below command
install.packages("C:/Users/test12/Downloads/Aphrodite.zip",repos=NULL,dependencies=TRUE,type="source")
The above command results in below message and moves the cursor to next line without any error message. Please note that all my packages goes into the below folder only.
Installing package into ‘C:/Users/test12/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)
However, when I try to use the below command, I get an error as shown below
library(Aphrodite)
Error in library(Aphrodite) :
‘Aphrodite’ is not a valid installed package
I also tried the below option but it doesn't display some of the folders which are available in the master directory. The details of it can be referred in this post
install.packages("devtools")
library(devtools)
install_github("ohdsi/Aphrodite") #APHRODITE is the package name
library(Aphrodite)
How can I load the package when it is installed from a custom directory? Why the package is not being recognized even though it is in the usual libpath folder?

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...

Install ggplot2 from local directory

I need to download packages and install from local directory. But I'm having trouble installing ggplot2. I get no warning, but it seems the library is not installed. Output is below, any suggestions? Operating system is Windows. After running, the directory C:...\R\win-library\3.1 contains subdirectory ggplot2-master.
> install.packages("C:/.../packages/ggplot2-master.zip", repos = NULL, type="source")
Installing package into ‘C:/.../R/win-library/3.1’
(as ‘lib’ is unspecified)
> library(ggplot2)
Error in library(ggplot2) : there is no package called ‘ggplot2’
The correct action was to download the binaries instead of the source.
Note, the following command prompts for the file path:
install.packages(file.choose(), repos=NULL)
The computer is behind a firewall, so the install.package command does not work. I should have downloaded the binary install, but downloaded the source by mistake. So, the corrective action is to download the binary files. I want to point out that in my notes on R, it clearly states to download the binary file. Part of the joys of getting older, not remembering why I came into this room.... Also, rambling...

Install R package to a specific directory

I tried this code to install R package to a directory where I stored my R packages.
install.packages("zoo", lib="/data/Rpackages/")
I got this warning message:
Warning in install.packages("zoo", lib = "/data/Rpackages/") : 'lib
= "/data/Rpackages/"' is not writable
Secondly how do I create a file .Renviron in our home area and add the line R_LIBS=/data/Rpackages/ to it?
How do you actually get out of the warning situation? Is there a better way other than (in R Studio menus) Session->Interrupt R and terminating the R session? I tried CTRL-C.
> install.packages("car", lib="/my R packages/")
Warning in install.packages :
'lib = "/my R packages/"' is not writable
My solution to the problem was to load the package within R Studio using the 'Packages' window which then does the following:
> install.packages("car")
Installing package into ‘C:/Users/33386/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.1/car_2.0-21.zip'
Content type 'application/zip' length 1332800 bytes (1.3 Mb)
opened URL
downloaded 1.3 Mb
package ‘car’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\33386\AppData\Local\Temp\RtmpKWnZD4\downloaded_packages
>
Briefly:
Use chmod to change the directory mode: chmod u+w /data/R/packages. You may need sudo (or your admin) to do that.
Use an editor and create and save a file: editor ~/.Renviron. You could also copy Renviron.site from R's etc/ directory and start from that.
Try running R studio as administrator. If you are using ubuntu start R using
sudo R

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