bcss
home

Customizing temp and library directories

  On Windows, it helps to use customized folders for temporary files and for packages, instead of the usual defaults. This avoids a couple of problems we have encountered, but is generally a good idea.

Windows defaults

On recent versions of Windows, the path to the user’s Documents directory is typically C:/Users/<username>/Documents and R packages are stored in Documents\R\win-library\x.y where "x.y" is the major.minor R version, eg, 3.6. The temporary directory is at C:/Users/<username>/AppData/Local/Temp.

The issues

We have had issues with certain R functions where
   1. the user’s area was configured on a network drive, even though the actual location was on the local machine;
   2. the <username> included CJK (Chinese-Japanese-Korean) characters (this should be fixed in R 4.0.2).

After installing R, you can check this by running

tempdir() ; Sys.getenv("R_USER")

If "\\\\" or "//" or non-Western characters or unicode hex values (eg, <U+611B>) appear in the output, some R functions will fail.

Other advantages

Even if the defaults work for you, R will sometimes 'lose' its temporary files. I think this is because the OS cleans up temporary files periodically. It also makes the temporary directory easy to find if you have to clean up manually after R crashes.

It helps that the temp file is not buried deep in hidden folders and the library files are not cluttering up the Documents folder.

Customizing folders

This involves creating directories on the C: drive and then pointing R to use them.

Create a new folder on the C: drive, call it "R", then do sub-directories named R_temp and R_libs, so you have "C:\R\R_temp" and "C:\R\R_libs". (You can use different names, but keep them short and without spaces or symbols.)

You may be able to use the .Renviron or .Rprofile files to point R to these at start up, provided there is no problem with the path to Documents. See ?Startup in R for details of the process. I prefer to set environment variables.

Go to Control Panel > System > Advanced system settings > Environment variables -or- go to Settings and search for "environment variable".

Create a new User environment variable named TMPDIR with value C:\R\R_temp. You should already have TEMP and TMP variables: do not change those. R looks first for TMPDIR; if not found, it uses TEMP or TMP. I think it's the only software to use TMPDIR, so setting this won't affect other programs.

For the package libraries, create another environment variable with the name R_LIBS_USER and value C:\R\R_libs\%v.

The next time you run install.packages(), you will see pop-up boxes asking if you want to create a new directory. A new subdirectory, C:\R\R_libs\x.y, will be created, where "x.y" indicate the R version (major.minor), currently 4.0.  Packages will be installed in the appropriate subdirectory for the version of R being run.

Feedback

If you have suggestions, comments or questions about installing the software needed for the workshop please email stats.bcss@gmail.com.

 

Page updated 11 Oct 2020 by Mike Meredith