RStan script works interactively but not in command line

I am running into an issue where I am able to run my rstan code interactively (e.g., in jupyter notebook), but when I run the same script in the command line through Rscript I receive the following error:

Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.18.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! In file included from /shared/centos7/gcc/10.1.0/lib/gcc/x86_64-pc-linux-gnu/10.1.0/include/immintrin.h:55,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/RcppEigen/include/Eigen/Core:210,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/RcppEigen/include/Eigen/Dense:1,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:4,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/StanHeaders/include/stan/math/rev/mat/fun/Eigen_NumTraits.hpp:4,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/StanHeaders/include/stan/math/rev/core/matrix_vari.hpp:4,
                 from /home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/library/StanHeaders/include/stan/math/rev/core.hpp:14,
             
Calls: stan ... cxxfunctionplus -> <Anonymous> -> cxxfunction -> compileCode
In addition: Warning message:
In system(cmd, intern = !verbose) :
  running command '/home/shawn/anaconda3/envs/crowdsourcing-r/lib/R/bin/R CMD SHLIB file35914625e290.cpp 2> file35914625e290.cpp.err.txt' had status 1
Error in sink(type = "output") : invalid connection
Calls: stan -> stan_model -> cxxfunctionplus -> sink
Execution halted


Below is the barebone script I am using:

library(rstan)
y <- rbinom(20, size = 1, prob = 0.3)


bernoulli_string <- "
data {
  int n;
  int y[n];
}
parameters {
  real<lower=0, upper=1> theta;
}
model {
  y ~ bernoulli(theta);
}"

stan_samples <- stan(model_code = bernoulli_string, data = list(y = y, n = length(y)) )
stan_samples

Below are some system info. The session info and Makevars are identical when I run it interactively and through the command line.

─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       CentOS Linux 7 (Core)       
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2021-07-12          

─ Packages ───────────────────────────────────────────────────────────────────
 package      * version   date       lib source        
 backports      1.2.1     2020-12-09 [1] CRAN (R 3.6.3)
 BH             1.69.0-1  2019-01-07 [1] CRAN (R 3.6.0)
 callr          3.7.0     2021-04-20 [1] CRAN (R 3.6.3)
 checkmate      1.9.1     2019-01-15 [1] CRAN (R 3.6.0)
 cli            2.5.0     2021-04-26 [1] CRAN (R 3.6.3)
 colorspace     2.0-1     2021-05-04 [1] CRAN (R 3.6.3)
 crayon         1.4.1     2021-02-08 [1] CRAN (R 3.6.3)
 desc           1.3.0     2021-03-05 [1] CRAN (R 3.6.3)
 digest         0.6.27    2020-10-24 [1] CRAN (R 3.6.3)
 ellipsis       0.3.2     2021-04-29 [1] CRAN (R 3.6.3)
 fansi          0.4.0     2018-10-05 [1] CRAN (R 3.6.0)
 farver         2.1.0     2021-02-28 [1] CRAN (R 3.6.3)
 ggplot2      * 3.3.3     2020-12-30 [1] CRAN (R 3.6.3)
 glue           1.4.2     2020-08-27 [1] CRAN (R 3.6.3)
 gridExtra      2.3       2017-09-09 [1] CRAN (R 3.6.0)
 gtable         0.3.0     2019-03-25 [1] CRAN (R 3.6.3)
 inline         0.3.15    2018-05-18 [1] CRAN (R 3.6.0)
 isoband        0.2.4     2021-03-03 [1] CRAN (R 3.6.3)
 labeling       0.4.2     2020-10-20 [1] CRAN (R 3.6.3)
 lattice        0.20-44   2021-05-02 [1] CRAN (R 3.6.3)
 lifecycle      1.0.0     2021-02-15 [1] CRAN (R 3.6.3)
 loo            2.1.0     2019-03-13 [1] CRAN (R 3.6.0)
 magrittr       2.0.1     2020-11-17 [1] CRAN (R 3.6.3)
 MASS           7.3-54    2021-05-03 [1] CRAN (R 3.6.3)
 Matrix         1.2-18    2019-11-27 [1] CRAN (R 3.6.2)
 matrixStats    0.54.0    2018-07-23 [1] CRAN (R 3.6.0)
 mgcv           1.8-28    2019-03-21 [1] CRAN (R 3.6.0)
 munsell        0.5.0     2018-06-12 [1] CRAN (R 3.6.3)
 nlme           3.1-152   2021-02-04 [1] CRAN (R 3.6.3)
 pillar         1.6.1     2021-05-16 [1] CRAN (R 3.6.3)
 pkgbuild       1.0.3     2019-03-20 [1] CRAN (R 3.6.0)
 pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 3.6.3)
 prettyunits    1.1.1     2020-01-24 [1] CRAN (R 3.6.3)
 processx       3.5.2     2021-04-30 [1] CRAN (R 3.6.3)
 ps             1.6.0     2021-02-28 [1] CRAN (R 3.6.3)
 R6             2.5.0     2020-10-28 [1] CRAN (R 3.6.3)
 RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 3.6.3)
 Rcpp           1.0.6     2021-01-15 [1] CRAN (R 3.6.3)
 RcppEigen      0.3.3.5.0 2018-11-24 [1] CRAN (R 3.6.0)
 rlang          0.4.11    2021-04-30 [1] CRAN (R 3.6.3)
 rprojroot      2.0.2     2020-11-15 [1] CRAN (R 3.6.3)
 rstan        * 2.18.2    2018-11-07 [1] CRAN (R 3.6.0)
 scales         1.1.1     2020-05-11 [1] CRAN (R 3.6.3)
 StanHeaders  * 2.18.1    2019-01-28 [1] CRAN (R 3.6.0)
 tibble         3.1.2     2021-05-16 [1] CRAN (R 3.6.3)
 utf8           1.2.1     2021-03-12 [1] CRAN (R 3.6.3)
 vctrs          0.3.8     2021-04-29 [1] CRAN (R 3.6.3)
 viridisLite    0.4.0     2021-04-13 [1] CRAN (R 3.6.3)
 withr          2.4.2     2021-04-18 [1] CRAN (R 3.6.3)

# --------
# Makevars
# --------
CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
CXX14=/shared/centos7/gcc/8.1.0/bin/g++
LDFLAGS+=-L/shared/centos7/gcc/8.1.0/lib64, -L/shared/centos7/gcc/8.1.0/libexec/gcc/x86_64-pc-linux-gnu/8.1.0, -L/shared/centos7/gcc/8.1.0/lib/gcc/x86_64-pc-linux-gnu/8.1.0,-rpath
CFLAGS+=-I/shared/centos7/gcc/8.1.0/lib/gcc/x86_64-pc-linux-gnu/8.1.0/include, -I/shared/centos7/gcc/8.1.0/include/c++/8.1.0, -I/shared/centos7/gcc/8.1.0/include/c++/8.1.0/x86_64-pc-linux-gnu,-rpath
# Make using all cores (set # to # of cores on your machine)
MAKE=make -j4

Resolved. The server was picking up gcc/10.1.0 while .R/Makevars specifies gcc/8.1.0.
Unloading gcc/10.1.0 resolved the issue.

1 Like