Compilation Error (Please Help!)

Summary:

Cannot compile rstan.

Description:

I tried the 8schools example of rstan: [https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started] and ran into problems.

Markvars.win File

CXX14=$(BINPREF)g++
CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

Stan File:

// saved as 8schools.stan
data {
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
}

Reproducible Steps:

library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
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))
fit <- stan(file = ‘8schools.stan’, data = schools_dat)

Current Output:

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/src/stan/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file4a3473ca4c69.cpp 2> file4a3473ca4c69.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

Expected Output:

If applicable, the output you expected from RStan.

Environment:

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] rstan_2.18.2 StanHeaders_2.18.1 ggplot2_3.1.1

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 pillar_1.4.1 compiler_3.6.0 plyr_1.8.4 prettyunits_1.0.2 tools_3.6.0 pkgbuild_1.0.3
[8] tibble_2.1.2 gtable_0.3.0 pkgconfig_2.0.2 rlang_0.3.4 cli_1.1.0 rstudioapi_0.10 parallel_3.6.0
[15] loo_2.1.0 gridExtra_2.3 withr_2.1.2 dplyr_0.8.1 stats4_3.6.0 grid_3.6.0 tidyselect_0.2.5
[22] glue_1.3.1 inline_0.3.15 R6_2.4.0 processx_3.3.1 callr_3.2.0 purrr_0.3.2 magrittr_1.5
[29] scales_1.0.0 ps_1.3.0 matrixStats_0.54.0 assertthat_0.2.1 colorspace_1.4-1 lazyeval_0.2.2 munsell_0.5.0
[36] crayon_1.3.4

Checking the C++ Toolchain

pkgbuild::has_build_tools(debug = TRUE)
[1] TRUE

Try the test from the wiki

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’
return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;’ )

make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file4a3425103b7b.dll] Error 127

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file4a3425103b7b( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file4a3425103b7b( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21: Rf_warning(“your C++ program does not return anything”);
22: return R_NilValue ;
23: }
24:
25:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file4a3425103b7b.dll] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file4a3425103b7b.cpp 2> file4a3425103b7b.cpp.err.txt’ had status 1

It could not find make. What does Sys.getenv('BINPREF') return?

It is an empty string. Then I create a .Rprofile file with only one line in it as in the following:

Sys.setenv(BINPREF = “C:/Rtools/mingw_$(WIN)/bin/”)

Now Sys.getenv('BINPREF') returns the exact string I write: "C:/Rtools/mingw_$(WIN)/bin/". How can I let R to translate $(WIN)?

R should do that automatically but it is empty except on Windows.

After this fix, now when I run fit <- stan(file = ‘8schools.stan’, data = schools_dat). RStudio asks me to install additional build tools. After clicking yes, RStudio goes ahead to download and install Rtools again. Why does it happen? I have already get it installed. Furthermore, after the reinstallation of Rtools, RStudio asks me to install Rtools another time. How to solve this problem?

After this fix, the same error still exists. How to solve it?

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ’

  •                        return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;' )
    

make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file552c5d1d4c4f.dll] Error 127

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1:
2: // includes from the plugin
3: #include <R.h>
4: #include <Rdefines.h>
5: #include <R_ext/Error.h>
6:
7:
8: // user includes
9:
10:
11: // declarations
12: extern “C” {
13: SEXP file552c5d1d4c4f( SEXP x, SEXP y) ;
14: }
15:
16: // definition
17:
18: SEXP file552c5d1d4c4f( SEXP x, SEXP y ){
19:
20: return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
21: Rf_warning(“your C++ program does not return anything”);
22: return R_NilValue ;
23: }
24:
25:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! make: *** [C:/PROGRA~1/R/R-36~1.0/share/make/winshlib.mk:13: file552c5d1d4c4f.dll] Error 127
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file552c5d1d4c4f.cpp 2> file552c5d1d4c4f.cpp.err.txt’ had status 1

You can try first doing

options(buildtools.check = NULL)

Now I get the following error message:

fit <- stan(file = ‘8schools.stan’, data = schools_dat)

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! In file included from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/integer_log2.hpp:19:0,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/detail/int_float_pair.hpp:26,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/exponential_distribution.hpp:27,
from C:/Users/swang/R/win-library/3.6/BH/include/boost/random/gamma_distribution.hpp:25,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat/prob/dirichlet_rng.hpp:5,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/prim/mat.hpp:276,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math/rev/mat.hpp:12,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/stan/math.hpp:4,
from C:/Users/swang/R/win-library/3.6/StanHeaders/include/src/stan/
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file61cc246763b7.cpp 2> file61cc246763b7.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

We need that call with the verbose = TRUE argument to see the underlying error message.