R and RStudio
Configurations of RStudio on Windows
Using R and RStudio on Windows in our domain can throw up a few unexpected errors, this page is intended to help work around the few issues that we have seen.
CSCS are happy to help support the use of R and RStudio. We are able to support the general principles of installation and operation, but we are not expert in each of the diverse range of community created analysis packages.
R does not work well with UNC paths, e.g. folder locations that start \\me-filer... In most cases you will be able to browse to the mapped path e.g. U:\My Documents rather than, for example, \\me-filer1\home$\%CRSID%\My Documents
Often users ask for administrative privileges to run R, whilst this may be necessary in some cases, it may also present unexpected results. If you right click the RStudio icon and select run as Administrator, although the application has admin rights and can now write to the default library at C: \Program Files\... the software no longer recognises the user documents folder or group drives and as such cannot load libraries from these locations.
Conversely if the software is run on Windows 10 (even if the user has admin rights) the central library at C:\Program Files is not writable (so new modules cannot be installed there) and is not available to subprocesses called by some modules (e.g. rstan)
To install packages to the C:\Program Files
Install R (in this example we are using R3.6.xx) please note the version number
If you will be requiring R build tools (Rtools) then also install this
Install RStudio https://www.rstudio.com/products/rstudio/download/#download
in RStudio go to Tools > Global Options
if the Default Working Directory starts with something like \\me-biofiler... then replace that with U:/My Documents; (or if using a laptop C:/Users/###/Documents
In the Console area of RStudio copy and paste the following two lines then press Enter;
R <- file.path(Sys.getenv(
A new file will open in the File area, in the top left quadrant of RStudio. In that File area paste the following lines and then save the file, following the format shown below;
Save this file. Note that in the example above we are including a group drive mounted as V:, this may or may not be necessary or desirable for all use cases. Also note that if you are using a laptop then rather than referencing the U: drive it might be better to use e.g. C:/Users/###/Documents/R/win-library/3.6. This is because a laptop may not always be connected to your backed up network drive usually found at U:
If you wish to install libraries to Program Files then you will need to run RStudio as an Administrator (hold Ctl-Shift-Enter with program icon selected) or right click the icon and select Run As Administrator.
Now close and reopen RStudio, when you type getwd() into the console it should return;
should return something similar to;
the second entry may be a little different, depending on the group drive folder you are using.
When you install packages, ensure that you are selecting either your personal (U:/My Documents), or a shared repository (e.g. V:/Rlib_shared) as the destination;
If using the C++ toolchain (for packages similar to rstan or brms) then you will also need to do the following. If you are unsure whether this is applicable, then run these commands anyway, it simply creates a text file which is used by the C++ toolchain and will be ignored of not required.
In the Console area of RStudio copy and paste the following three lines then press Enter;
dotR <- file.path(Sys.getenv(
M <- file.path(Sys.getenv(
A new file will open in the File area, in the top left quadrant or RStudio. In the File area paste the following 3 lines and then save the file
CXX14=$(BINPREF)g++ -m$(WIN) -std=c++1y -march=
Now run the following command in the Console;
This will either return TRUE or FALSE, in the latter case RStudio will offer to install the build tools. Note that you will need to be an Administrator on the computer to install the build tools, but if already installed you do not need to be an admin to use them. You can also install the build tools from software.medschl.cam.ac.uk
When using the library you may wish to set options in order that rstan use all the available processor cores and save the compiled Stan program to disk;
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
to return the number of cores in use run;
these options are described in more detail at;
Now in the bottom left window of RStudio run the following commands;
, dependencies = TRUE)
pkgbuild::has_build_tools(debug = TRUE)
this should prompt to download and install the C++ toolchain to C:\RBuildTools, just accept any defaults offered and let it install
at the end of the process the command line should return
Having got to this point now please run
to load the library, this should return something similar to;
: ggplot2rstan (Version
, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend callingoptions(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend callingrstan_options(auto_write = TRUE)
For improved execution time, we recommend callingSys.setenv(LOCAL_CPPFLAGS =
causes Stan to
an error on a few processors.
In RStudio select Fine > New File > Text then paste the following into the top left window;
Save this file as U:\My Documents\8schools.stan. Now in the lower left R prompt paste the following and press Enter;
schools_dat <- list(J =
y = c(
sigma = c(
finally run the following command in the lower left R window;
fit <- stan(file =
, data = schools_dat)
the expected behavior is that it will appear that nothing is happening for a minute or two then text similar to the following wil indicate success;
SAMPLING FOR MODEL
: Gradient evaluation took
leapfrog steps per transition would take
: Adjust your expectations accordingly!
RAM & CPU usage
memory.limit() and memory.size() will report RAM limit and in use sizes respectively, in MB
RStudio & R 3.6 - 3.6.1 seem to have a bug where RStudio will report RAM usage incorrectly as e.g. 1.759219e+13 rather than 16270 on a 16GB system. Rgui will report the correct RAM allocation in MB