Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

...

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:

...

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

...

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

...