Reputation: 69
I'm trying to find the best way to setup R for my team. Because our institution has the user home directory on a network share, the R user library is defaulted to this network share. After some research, I found that setting R_LIBS_USER in the .Renviron file is most useful (like stated at the rstudio forums ) As stated in the same post, it doesn't automatically create this directory after installing a new version of R, so .libPaths() defaults to C:/Program Files/R/../.. (the R_LIBS_USER is ignored)
In the question below, the same problem is asked 6 years ago. The accepted answer is not helping because it suggests making a version-independent user library. I don't want these old packages in my library. Why do I have to create the directory "~/R/%p-library/%v" by hand on each R upgrade?
I also tried setting .libPaths in the .Rprofile, but using the .Renviron file feels more efficient, so I prefer using this. This also allows the users to use their own .Rprofile settings in their projects.
My current way of working is:
R_LIBS_USER=C:/Users/[user]/R/%p-library/%v
dir.create(Sys.getenv('R_LIBS_USER'), recursive = TRUE)
I want to know what's the best/cleanest setup for automatically creating the R_LIBS_USER folder, so reinstalling R doesn't need any manual actions that can be forgotten by the users.
Upvotes: 1
Views: 2144
Reputation: 52268
You could write a small powershell script to run after installing R that would create whatever directories/files you like. A simple example can be found here - just adjust the code to include whatever environment variables you like.
Place the following line of code in a text file, and save it with .ps1
file extension (or just copy/paste the code directly into a powershell prompt if you want to test it).
Add-Content c:\Users\$env:USERNAME\Documents\.Renviron "TEST_VARIABLE_1=my_username"
The line of code above will create a file called .Renviron
in the directory specified, as well as append the environment variable TEST_VARIABLE
into that file (so it's loaded into R through .Renviron
in the standard way).
Obviously you can add as many variables in the same way (just copy the line of code to include whatever environment variables you like).
You can also easily adjust the location of the .Renviron
file to whatever you desire, including a network drive.
Note also that you can easily create a directory in powershell (as opposed to a file) like so
e.g.
New-Item -ItemType directory -Path C:\Scripts\newDir
Upvotes: 0