Setting R's Library Path on UF Classroom PCs

And Maybe on Your Office PC Too

What Is This About?

After a very long hiatus from needing to run R in the classrooms, I am back at it. The classroom (Windows 10) PCs at UF are refreshed every night and all local files created by users are wiped. By default, R packages installed by users are placed in C:/Program Files/R/R-3.5.2/library,1 and like everything else that users create, they are wiped every night. Thus, if you accept the default behavior, you have to resintall any packages you need every time you need them.2 This has been the source of some frustration for me and at least one of my colleagues, so I decided to do something about it. This post describes my current (partial) solution, which I think should also be useful for folks running R or RStudio on a desktop PC in their office (in my department at UF anyway). It's nothing fancy, and if someone has a better solution I would be happy to hear about it.

My Solution

Download this file and save it under the name .Rprofile in your home directory on the departmental file server (so ~/.Rprofile in linux and H:/.Rprofile in Windows), or use a text editor to create the file yourself with with the following contents:

if (grepl("Windows", Sys.info()["sysname"])) {
    my.lib.path <- file.path("H:", "R", "win-library",
                             paste(R.version$major,
                                   sub("\\..*$", "", R.version$minor),
                                   sep="."))
    if (!dir.exists(my.lib.path)) dir.create(my.lib.path, recursive = TRUE)
    .libPaths(my.lib.path)
    rm(my.lib.path)
}

Now, whenever you start R in the classroom, run the command

source("H:/.Rprofile")

This first checks that you are running R in Windows. If so, then it checks for the existence of the directory H:/R/win-library/X.Y, where X is the major version number and Y is the minor version of the current R process, and it creates this directory if it doesn't already exist. It then adds this directory to the front of R's library path so that (1) R will look for packages there before trying the system packages, and (2) R will install any new packages into this directory rather than the system directory.

If anything about that is not ok with you, then you will need to modify .Rprofile to suit your specific needs. If you already have a directory of installed R packages, you can either move/rename it to match this scheme, or you can modify the script to match, assuming that your directory is on your H: drive.

If You Run R in Windows in Your Office

If you place a copy of this .Rprofile file in whatever directory is returned by the R command path.expand('~') (probably C:/Users/<yourusername>/Documents), then R will run it automatically upon startup (on your office PC) and any libraries you install in your office will be saved to your H: drive. Also, as long as your office PC and the classroom PCs are running the same version of R, they will share the same set of user-installed R packages.

Further Background and Explanation

You can skip this if you like …

When I login to a classroom machine, my home directory from the departmental (virtual) file server is mounted as the H: drive in Windows. So, to sidestep the problem of repeatedly installing packages, I initially created the folder R/win-library/3.53 in my home directory on the file server, and in the classroom, I just ran the R command .libPaths("H:/R/win-library/3.5") immediately after starting R (or RStudio). With this, any libraries I install are saved on the departmental file server and they are found again the next time I teach (once I have run that command).

Of course this is still pretty annoying and I wanted to arrange things so that it would be done automatically by an .Rprofile startup file. This works if the file is placed in C:/Users/myusername/Documents, which the R command path.expand('~') returns as be the default user home on our classroom Windows PCs, but of course that file would also get wiped every night, so placing the file there doesn't help.

Nevertheless, to accomodate differing/changing versions of R and to automate the process as much as possible, I decided to create a .Rprofile that checks if I am running Windows and if so, sets my R library path to .libPaths("H:/R/win-library/X.Y"). The X.Y is replaced by whatever "major.minor" version of R is running (currently 3.5) and the directory is created if it doesn't already exist. Because this is done only if I am running Windows, it has no effect on my usual, completely satisfactory R environment in Linux.

The contents of my current .Rprofile file are given in the previous section. If you already have a non-empty .Rprofile, you could add this to it, and of course you could also create different versions for different classes and put them in the appropriate places if that tickles your fancy. In the classroom I still have to run

source("H:/.Rprofile")

after starting R, but I guess I'm going to have to live with that.

Footnotes

1Coming from the UNIX/Linux world this already seems a bit crazy. Why am I running R as root here? BTW, you should mentally replace "3.5.2" with whatever version of R is currently installed on the PC, and Windows/DOS users may be more comfortable replacing the "/" path separator by "\".

2The classroom IT folks suggest using a USB thumbdrive as a workaround. insert sad face here

3From what I saw on the classroom PCs, it appears that win-library is the R default name for the user's R libraries in Windows, so I decided to stick with that. (On Linux it's something like x86_64-pc-linux-gnu-library.)


Brett Presnell
Brett Presnell
Emeritus Professor of Statistics

My research interests include nonparametric and computationally intensive statistics, model misspecification, statistical computing, and the analysis of directional data.