Page tree

Site archived

The content on this site is no longer updated and may be out of date. For the latest version, visit https://cscs-itsupport.atlassian.net/wiki/home

Skip to end of metadata
Go to start of metadata

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

Run RStudio


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("HOME"), ".Rprofile")
file.edit(R)

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;

.libPaths(c(
 
"U:/My\ Documents/R/win-library/3.6",
"V:/Rlib_shared/3.6",
"C:/Program\ Files/R/R-3.6.1/library"
))

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;

[1] "U:/My Documents"

And typing;

.libPaths()

should return something similar to;

[1] "U:/My Documents/R/win-library/3.6"
[2] "V:/Rlib_shared/3.6"
[3] "C:/Program Files/R/R-3.6.1/library"

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;

install.packages("brms", lib="V:/Rlib_shared/3.6")


RBuildTools

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("HOME"), ".R")
M <- file.path(Sys.getenv("HOME"), ".R", "Makevars.win"))
edit(M)

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

CXX14FLAGS=-O3 -march=native
 
CXX14=$(BINPREF)g++ -m$(WIN) -std=c++1y -march=native -mtune=native
CXX11FLAGS=-O3 -march=native

Now run the following command in the Console;

pkgbuild::has_build_tools()

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;

library("rstan")
 
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)

to return the number of cores in use run;

getOption("mc.cores", 1L)

these options are described in more detail at;

https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

RStan

Now in the bottom left window of RStudio run the following commands;

install.packages("rstan", dependencies = TRUE)

then run;

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

[1] TRUE

Having got to this point now please run

library("rstan")

to load the library, this should return something similar to;

Loading required package: StanHeadersLoading
required package: ggplot2rstan (Version 2.19.2, 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 = '-march=native')
although this causes Stan to throw an error on a few processors.

In RStudio select Fine > New File > Text then paste the following into the top left window;

// saved as 8schools.standata {  int<lower=0> J;         
// number of schools   real y[J];              
// estimated treatment effects  real<lower=0> sigma[J];
// standard error of effect estimates }parameters {  real mu;                
// population treatment effect  real<lower=0> tau;      
// standard deviation in treatment effects  vector[J] eta;          
// unscaled deviation from mu by school}transformed parameters {  vector[J] theta = mu + tau * eta;        
// school treatment effects}model {  target += normal_lpdf(eta | 0, 1);       
// prior log-density  target += normal_lpdf(y | theta, sigma);
// log-likelihood}

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 = 8,                     
y = c(28,  8, -3,  7, -1,  1, 18, 12),                    
sigma = c(15, 10, 16, 11,  9, 11, 10, 18)) 

finally run the following command in the lower left R window;

fit <- stan(file = '8schools.stan', 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 '8schools' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 0 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 1: 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

https://github.com/rstudio/rstudio/issues/4986