Thanks @josh for installing clang++. The rstan package seems to work now, at least for simple examples. To use it, execute the following R code once
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
MAKEVARS <- file.path(dotR, "Makevars")
if (!file.exists(MAKEVARS)) file.create(MAKEVARS)
cat(
"\nCXXFLAGS=-Os -mtune=native -march=native",
"CXXFLAGS += -Wno-unused-variable -Wno-unused-function -Wno-unknown-pragmas",
"CXX=clang++",
file = MAKEVARS,
sep = "\n",
append = TRUE
)
to configure your C++ toolchain as mentioned at
https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Customizing-package-compilation
Then, do
install.packages("rstan")
but you may also want to install brms, shinystan, etc.
To test that it is working correctly, you can run an example such as
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))
library(rstan)
mc <-
'
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta;
theta = mu + tau * eta;
}
model {
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
}
'
post <- stan(model_code = mc, data = schools_dat, seed = 12345678)
post
@josh Do you think it is possible for RStudio Cloud to detect whether the rstan package is loaded and if so
Sys.setenv(R_MAKEVARS_USER = "/read-only/path/to/Makevars")
behind the scenes (if it is unset originally) where /read-only/path/to/Makevars just contains
CXXFLAGS=-Os -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-unknown-pragmas
CXX=clang++
which would make it unnecessary for each user to run the code at the top of this post to self-configure their toolchain?