1

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:

  • .Renviron file R_LIBS_USER=C:/Users/[user]/R/%p-library/%v
  • Each user calls this command after a new R install 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.

  • What's wrong with using the Rprofile file? That was the easiest way we did it for our team where we just put the line `.libPaths("C:/R/R-3.6.1/library")` – Ben G Aug 08 '19 at 12:55
  • 1
    This doesn't solve the problem for me, because this also doesn't create the directory when it doesn't exist. I want the path to be dynamic (with version number). The linked question says using `dir.create()` in rprofile is not best practice. I'm wondering what is. – Jelger van Zaane Aug 08 '19 at 13:14
  • Yep, that makes a lot of sense. – Ben G Aug 08 '19 at 13:55

1 Answers1

0

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.

A simple example

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
Community
  • 1
  • 1
stevec
  • 41,291
  • 27
  • 223
  • 311
  • Hi thanks. This still needs a manual action after installing (running the script). My problem is that each user should run this after a new R version has been installed. The way I do this now is by asking the user to run this in R: `dir.create(Sys.getenv('R_LIBS_USER'), recursive = TRUE)` I'm looking for an automated way to recognize the folder doesn't exist (maybe on startup of R), and create it when needed. – Jelger van Zaane Aug 08 '19 at 13:03
  • @Jelger I see. I think you will always have to do something in addition to installing R in order to create the directory (so your current solution may be as automated as possible already). If you wanted to do both an R installation *and* creation of directory in one go, you could use a [powershell script to install R](https://medium.com/@steve.condylios/how-to-install-r-and-rstudio-on-windows-with-powershell-b56581457577), and simply add a line to the end of that script to create the desired directory. I would suspect creating the directory once on installation of an R version is ample? – stevec Aug 08 '19 at 13:10