Dealing with R 4.0

I think we need both 4.0 and a 3.x instructions but 4.0 is recommended.

Stephen, I followed you html manual and still can’t run stan in R4.0.

I have the error message whte trying your test code:

sm <- stan_model(model_code = stan_code)
Error in file(con, “r”) : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file1bc42dc1b37.cpp 2> file1bc42dc1b37.cpp.err.txt’ had status 1
2: In file(con, “r”) :
cannot open file ‘file1bc42dc1b37.cpp.err.txt’: No such file or directory
Error in sink(type = “output”) : invalid connection

Do you know what is the problem?

Need more info here.

I think the R 4.0 instructions have worked for people so far, so I assume there’s something unique to your setup.

Questions:

  • What is in your ~/.Renviron, ~/.Rprofile, and ~/.R/Makevars ?
  • Where is your model file located? Is it in a networked directory (e.g., Box, dropbox, NFS, Windows/Samba share)?

About the files:

~/.Renviron
PATH=“${RTOOLS40_HOME}\usr\bin;${PATH}”

~/.Rprofile (this file is empty)

~/.R/Makevars (this folder/file file does not exist).

My model is located inside the current R script, just as your Test code:

# Load rstan
library(rstan)

# Set seed
set.seed(13)

# Make data
stan_data <- list(y = rnorm(500, 0, 1),
                  N = 500)

# Define model
stan_code <- "
/*
  This model is just for testing.
*/
functions {
    real times_two(real x) { // Custom function test.
        return(x*2);
    }
}
data {
    int N; // Data length
    vector[N] y; // Data
}
transformed data {
    real a = 2.0; // Constant Test
}
parameters {
    real<lower = 0> sigma; // Constraint test; residual SD
    real mu; // Mean
}
transformed parameters {
    real mu_over_sigma = mu / sigma; // Transformed param test.
}
model {
    mu ~ normal(0, 1); // ~ syntax test.
    target += student_t_lpdf(sigma | 5, 0, 1); // Increment test.

    y ~ normal(mu, sigma); // Likelihood
}
generated quantities {
    real b = normal_rng(0, 1); // RNG test
    real c = times_two(a*b); // Custom Function test.
}
"

# Compile model
sm <- stan_model(model_code = stan_code)

At this stage, I have the error:

 sm <- stan_model(model_code = stan_code)
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file4374548ee21.cpp 2> file4374548ee21.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file4374548ee21.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Some possible useful information:

  • It is my first time trying to install stan, I had never used it neither had older stan versions installed in my computer.

  • My OS is Windows 10.

  • I use the most recent versions of R (4.0.1) and RStudio (1.3.959).

For me something weird happens, which I haven’t been able to figure out why. If I use library(rstan) I get a similar error to @rcpedroso. However, I do not get the error, and everything works fine, if I use rstan:: statements without first loading the library. For example:

stan_code <- structure("data {\n  int<lower=0> J;          // number of schools\n  real y[J];               // estimated treatment effect (school j)\n  real<lower=0> sigma[J];  // std err of effect estimate (school j)\n}\nparameters {\n  real mu;\n  real theta[J];\n  real<lower=0> tau;\n}\nmodel {\n  theta ~ normal(mu, tau); \n  y ~ normal(theta,sigma);\n}", 
                       model_name2 = "eight_schools")

sm <- rstan::stan_model(model_code = stan_code)

Works fine. However,

library(rstan)
stan_code <- structure("data {\n  int<lower=0> J;          // number of schools\n  real y[J];               // estimated treatment effect (school j)\n  real<lower=0> sigma[J];  // std err of effect estimate (school j)\n}\nparameters {\n  real mu;\n  real theta[J];\n  real<lower=0> tau;\n}\nmodel {\n  theta ~ normal(mu, tau); \n  y ~ normal(theta,sigma);\n}", 
                       model_name2 = "eight_schools")

sm <- rstan::stan_model(model_code = stan_code)

gives the following error:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.0/bin/x64/R CMD SHLIB file323c106d3c60.cpp 2> file323c106d3c60.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file323c106d3c60.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

The problem seems to be with the rstan package. For instance, brms does not give me any errors. The example:

library(brms)
bprior1 <- prior(student_t(5,0,10), class = b) +
  prior(cauchy(0,2), class = sd)
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
            data = epilepsy, family = poisson(), prior = bprior1)

Works fine. But loading rstan first gives me an error.

Session Info:

> devtools::session_info()
- Session info ---------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.0 (2020-04-24)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_Netherlands.1252    
 ctype    English_Netherlands.1252    
 tz       Europe/Berlin               
 date     2020-06-12                  

- Packages -------------------------------------------------------------------------------------------------------------------
 ! package        * version  date       lib source        
   abind            1.4-5    2016-07-21 [1] CRAN (R 4.0.0)
   assertthat       0.2.1    2019-03-21 [1] CRAN (R 4.0.0)
   backports        1.1.7    2020-05-13 [1] CRAN (R 4.0.0)
   base64enc        0.1-3    2015-07-28 [1] CRAN (R 4.0.0)
   bayesplot        1.7.2    2020-05-28 [1] CRAN (R 4.0.0)
   bridgesampling   1.0-0    2020-02-26 [1] CRAN (R 4.0.0)
   brms           * 2.13.0   2020-05-27 [1] CRAN (R 4.0.0)
   Brobdingnag      1.2-6    2018-08-13 [1] CRAN (R 4.0.0)
   callr            3.4.3    2020-03-28 [1] CRAN (R 4.0.0)
   cli              2.0.2    2020-02-28 [1] CRAN (R 4.0.0)
   coda             0.19-3   2019-07-05 [1] CRAN (R 4.0.0)
   colorspace       1.4-1    2019-03-18 [1] CRAN (R 4.0.0)
   colourpicker     1.0      2017-09-27 [1] CRAN (R 4.0.0)
   crayon           1.3.4    2017-09-16 [1] CRAN (R 4.0.0)
   crosstalk        1.1.0.1  2020-03-13 [1] CRAN (R 4.0.0)
   desc             1.2.0    2018-05-01 [1] CRAN (R 4.0.0)
   devtools         2.3.0    2020-04-10 [1] CRAN (R 4.0.0)
   digest           0.6.25   2020-02-23 [1] CRAN (R 4.0.0)
   dplyr            1.0.0    2020-05-29 [1] CRAN (R 4.0.0)
   DT               0.13     2020-03-23 [1] CRAN (R 4.0.0)
   dygraphs         1.1.1.6  2018-07-11 [1] CRAN (R 4.0.0)
   ellipsis         0.3.1    2020-05-15 [1] CRAN (R 4.0.0)
   fansi            0.4.1    2020-01-08 [1] CRAN (R 4.0.0)
   fastmap          1.0.1    2019-10-08 [1] CRAN (R 4.0.0)
   fs               1.4.1    2020-04-04 [1] CRAN (R 4.0.0)
   generics         0.0.2    2018-11-29 [1] CRAN (R 4.0.0)
   ggplot2        * 3.3.1    2020-05-28 [1] CRAN (R 4.0.0)
   ggridges         0.5.2    2020-01-12 [1] CRAN (R 4.0.0)
   glue             1.4.1    2020-05-13 [1] CRAN (R 4.0.0)
   gridExtra        2.3      2017-09-09 [1] CRAN (R 4.0.0)
   gtable           0.3.0    2019-03-25 [1] CRAN (R 4.0.0)
   gtools           3.8.2    2020-03-31 [1] CRAN (R 4.0.0)
   htmltools        0.4.0    2019-10-04 [1] CRAN (R 4.0.0)
   htmlwidgets      1.5.1    2019-10-08 [1] CRAN (R 4.0.0)
   httpuv           1.5.3.1  2020-05-26 [1] CRAN (R 4.0.0)
   igraph           1.2.5    2020-03-19 [1] CRAN (R 4.0.0)
   inline           0.3.15   2018-05-18 [1] CRAN (R 4.0.0)
   later            1.1.0.1  2020-06-05 [1] CRAN (R 4.0.0)
 D lattice          0.20-41  2020-04-02 [1] CRAN (R 4.0.0)
   lifecycle        0.2.0    2020-03-06 [1] CRAN (R 4.0.0)
   loo              2.2.0    2019-12-19 [1] CRAN (R 4.0.0)
   magrittr         1.5      2014-11-22 [1] CRAN (R 4.0.0)
   markdown         1.1      2019-08-07 [1] CRAN (R 4.0.0)
 D Matrix           1.2-18   2019-11-27 [1] CRAN (R 4.0.0)
   matrixStats      0.56.0   2020-03-13 [1] CRAN (R 4.0.0)
   memoise          1.1.0    2017-04-21 [1] CRAN (R 4.0.0)
   mime             0.9      2020-02-04 [1] CRAN (R 4.0.0)
   miniUI           0.1.1.1  2018-05-18 [1] CRAN (R 4.0.0)
   munsell          0.5.0    2018-06-12 [1] CRAN (R 4.0.0)
   mvtnorm          1.1-0    2020-02-24 [1] CRAN (R 4.0.0)
 D nlme             3.1-147  2020-04-13 [1] CRAN (R 4.0.0)
   pillar           1.4.4    2020-05-05 [1] CRAN (R 4.0.0)
   pkgbuild         1.0.8    2020-05-07 [1] CRAN (R 4.0.0)
   pkgconfig        2.0.3    2019-09-22 [1] CRAN (R 4.0.0)
   pkgload          1.1.0    2020-05-29 [1] CRAN (R 4.0.0)
   plyr             1.8.6    2020-03-03 [1] CRAN (R 4.0.0)
   prettyunits      1.1.1    2020-01-24 [1] CRAN (R 4.0.0)
   processx         3.4.2    2020-02-09 [1] CRAN (R 4.0.0)
   promises         1.1.0    2019-10-04 [1] CRAN (R 4.0.0)
   ps               1.3.3    2020-05-08 [1] CRAN (R 4.0.0)
   purrr            0.3.4    2020-04-17 [1] CRAN (R 4.0.0)
   R6               2.4.1    2019-11-12 [1] CRAN (R 4.0.0)
   Rcpp           * 1.0.4.6  2020-04-09 [1] CRAN (R 4.0.0)
   remotes          2.1.1    2020-02-15 [1] CRAN (R 4.0.0)
   reshape2         1.4.4    2020-04-09 [1] CRAN (R 4.0.0)
   rlang            0.4.6    2020-05-02 [1] CRAN (R 4.0.0)
   rprojroot        1.3-2    2018-01-03 [1] CRAN (R 4.0.0)
   rsconnect        0.8.16   2019-12-13 [1] CRAN (R 4.0.0)
   rstan          * 2.19.3   2020-02-11 [1] CRAN (R 4.0.0)
   rstantools       2.1.0    2020-06-01 [1] CRAN (R 4.0.0)
   rstudioapi       0.11     2020-02-07 [1] CRAN (R 4.0.0)
   scales           1.1.1    2020-05-11 [1] CRAN (R 4.0.0)
   sessioninfo      1.1.1    2018-11-05 [1] CRAN (R 4.0.0)
   shiny            1.4.0.2  2020-03-13 [1] CRAN (R 4.0.0)
   shinyjs          1.1      2020-01-13 [1] CRAN (R 4.0.0)
   shinystan        3.0.0    2020-06-08 [1] local         
   shinythemes      1.1.2    2018-11-06 [1] CRAN (R 4.0.0)
   StanHeaders    * 2.21.0-3 2020-05-28 [1] CRAN (R 4.0.0)
   stringi          1.4.6    2020-02-17 [1] CRAN (R 4.0.0)
   stringr          1.4.0    2019-02-10 [1] CRAN (R 4.0.0)
   testthat         2.3.2    2020-03-02 [1] CRAN (R 4.0.0)
   threejs          0.3.3    2020-01-21 [1] CRAN (R 4.0.0)
   tibble           3.0.1    2020-04-20 [1] CRAN (R 4.0.0)
   tidyselect       1.1.0    2020-05-11 [1] CRAN (R 4.0.0)
   usethis          1.6.1    2020-04-29 [1] CRAN (R 4.0.0)
   vctrs            0.3.0    2020-05-11 [1] CRAN (R 4.0.0)
   withr            2.2.0    2020-04-20 [1] CRAN (R 4.0.0)
   xtable           1.8-4    2019-04-21 [1] CRAN (R 4.0.0)
   xts              0.12-0   2020-01-19 [1] CRAN (R 4.0.0)
   zoo              1.8-8    2020-05-02 [1] CRAN (R 4.0.0)

[1] C:/Program Files/R/R-4.0.0/library

 D -- DLL MD5 mismatch, broken installation.

I have a similar error and behavior, and if I add the verbose flag to the compilation:

library(rstan)
stan_code <- structure("data {\n  int<lower=0> J;          // number of schools\n  real y[J];               // estimated treatment effect (school j)\n  real<lower=0> sigma[J];  // std err of effect estimate (school j)\n}\nparameters {\n  real mu;\n  real theta[J];\n  real<lower=0> tau;\n}\nmodel {\n  theta ~ normal(mu, tau); \n  y ~ normal(theta,sigma);\n}", 
                       model_name2 = "eight_schools")

sm <- rstan::stan_model(model_code = stan_code,verbose=TRUE)

I get the error:

g++.exe: error: Johnson/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory

I believe you’ll get the different behaviour when not loading RStan first if you have spaces in the path to your home directory.

This is because the compilation arguments to include Eigen.hpp header slightly differ depending on whether RStan is loaded first.

If I load RStan, the compilation argument includes:

-include C:/Users/Andrew Johnson/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp

Because there’s no quotations around the call, it interprets it as two separate commands (split at the space between ‘Andrew’ and ‘Johnson’).

If I don’t load RStan first, the compilation argument includes:

-include stan/math/prim/mat/fun/Eigen.hpp

Where the relative addressing bypasses the issues with the space

1 Like

Flagging so @bgoodri and @jonah know about the issue

Ugh, that’s not good, thanks for the heads up. @bgoodri is this something that needs fixing in RStan or is this really coming from attaching the StanHeaders package?

With the verbose flag added I get a similar problem:

g++.exe: error: Files/R/R-4.0.0/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory

It seems to go wrong at the program files with the space in between.

@ducoveen, it worked for me with the rstan:: statement and without loading the library, as you did. As expected, I identify the same problem with verbose=T, that is,

g++.exe: error: Files/R/R-4.0.1/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory

I don’t know if it will be useful, but these are my session informations:

> devtools::session_info()
- Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.1 (2020-06-06)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language en                          
 collate  Portuguese_Brazil.1252      
 ctype    Portuguese_Brazil.1252      
 tz       America/Sao_Paulo           
 date     2020-06-13                  

- Packages ---------------------------------------------------------------------------------------------
 ! package        * version  date       lib source        
   abind            1.4-5    2016-07-21 [1] CRAN (R 4.0.0)
   assertthat       0.2.1    2019-03-21 [1] CRAN (R 4.0.0)
   backports        1.1.7    2020-05-13 [1] CRAN (R 4.0.0)
   base64enc        0.1-3    2015-07-28 [1] CRAN (R 4.0.0)
   bayesplot        1.7.2    2020-05-28 [1] CRAN (R 4.0.0)
   bridgesampling   1.0-0    2020-02-26 [1] CRAN (R 4.0.0)
   brms           * 2.13.0   2020-05-27 [1] CRAN (R 4.0.0)
   Brobdingnag      1.2-6    2018-08-13 [1] CRAN (R 4.0.0)
   callr            3.4.3    2020-03-28 [1] CRAN (R 4.0.0)
   cli              2.0.2    2020-02-28 [1] CRAN (R 4.0.0)
   coda             0.19-3   2019-07-05 [1] CRAN (R 4.0.0)
   codetools        0.2-16   2018-12-24 [1] CRAN (R 4.0.1)
   colorspace       1.4-1    2019-03-18 [1] CRAN (R 4.0.0)
   colourpicker     1.0      2017-09-27 [1] CRAN (R 4.0.0)
   crayon           1.3.4    2017-09-16 [1] CRAN (R 4.0.0)
   crosstalk        1.1.0.1  2020-03-13 [1] CRAN (R 4.0.0)
   desc             1.2.0    2018-05-01 [1] CRAN (R 4.0.0)
   devtools         2.3.0    2020-04-10 [1] CRAN (R 4.0.0)
   digest           0.6.25   2020-02-23 [1] CRAN (R 4.0.0)
   dplyr            1.0.0    2020-05-29 [1] CRAN (R 4.0.0)
   DT               0.13     2020-03-23 [1] CRAN (R 4.0.0)
   dygraphs         1.1.1.6  2018-07-11 [1] CRAN (R 4.0.0)
   ellipsis         0.3.1    2020-05-15 [1] CRAN (R 4.0.0)
   fansi            0.4.1    2020-01-08 [1] CRAN (R 4.0.0)
   fastmap          1.0.1    2019-10-08 [1] CRAN (R 4.0.0)
   fs               1.4.1    2020-04-04 [1] CRAN (R 4.0.0)
   generics         0.0.2    2018-11-29 [1] CRAN (R 4.0.0)
   ggplot2        * 3.3.1    2020-05-28 [1] CRAN (R 4.0.0)
   ggridges         0.5.2    2020-01-12 [1] CRAN (R 4.0.0)
   glue             1.4.1    2020-05-13 [1] CRAN (R 4.0.0)
   gridExtra        2.3      2017-09-09 [1] CRAN (R 4.0.0)
   gtable           0.3.0    2019-03-25 [1] CRAN (R 4.0.0)
   gtools           3.8.2    2020-03-31 [1] CRAN (R 4.0.0)
   htmltools        0.4.0    2019-10-04 [1] CRAN (R 4.0.0)
   htmlwidgets      1.5.1    2019-10-08 [1] CRAN (R 4.0.0)
   httpuv           1.5.4    2020-06-06 [1] CRAN (R 4.0.0)
   igraph           1.2.5    2020-03-19 [1] CRAN (R 4.0.0)
   inline           0.3.15   2018-05-18 [1] CRAN (R 4.0.0)
   later            1.1.0.1  2020-06-05 [1] CRAN (R 4.0.0)
   lattice          0.20-41  2020-04-02 [1] CRAN (R 4.0.1)
   lifecycle        0.2.0    2020-03-06 [1] CRAN (R 4.0.0)
   loo              2.2.0    2019-12-19 [1] CRAN (R 4.0.0)
   magrittr         1.5      2014-11-22 [1] CRAN (R 4.0.0)
   markdown         1.1      2019-08-07 [1] CRAN (R 4.0.0)
   Matrix           1.2-18   2019-11-27 [1] CRAN (R 4.0.1)
   matrixStats      0.56.0   2020-03-13 [1] CRAN (R 4.0.0)
   memoise          1.1.0    2017-04-21 [1] CRAN (R 4.0.0)
   mime             0.9      2020-02-04 [1] CRAN (R 4.0.0)
   miniUI           0.1.1.1  2018-05-18 [1] CRAN (R 4.0.0)
   munsell          0.5.0    2018-06-12 [1] CRAN (R 4.0.0)
   mvtnorm          1.1-1    2020-06-09 [1] CRAN (R 4.0.0)
   nlme             3.1-148  2020-05-24 [1] CRAN (R 4.0.1)
   pillar           1.4.4    2020-05-05 [1] CRAN (R 4.0.0)
   pkgbuild         1.0.8    2020-05-07 [1] CRAN (R 4.0.0)
   pkgconfig        2.0.3    2019-09-22 [1] CRAN (R 4.0.0)
   pkgload          1.1.0    2020-05-29 [1] CRAN (R 4.0.0)
   plyr             1.8.6    2020-03-03 [1] CRAN (R 4.0.0)
   prettyunits      1.1.1    2020-01-24 [1] CRAN (R 4.0.0)
   processx         3.4.2    2020-02-09 [1] CRAN (R 4.0.0)
   promises         1.1.1    2020-06-09 [1] CRAN (R 4.0.0)
   ps               1.3.3    2020-05-08 [1] CRAN (R 4.0.0)
   purrr            0.3.4    2020-04-17 [1] CRAN (R 4.0.0)
   R6               2.4.1    2019-11-12 [1] CRAN (R 4.0.0)
   Rcpp           * 1.0.4.6  2020-04-09 [1] CRAN (R 4.0.0)
 D RcppParallel     5.0.1    2020-05-06 [1] CRAN (R 4.0.0)
   remotes          2.1.1    2020-02-15 [1] CRAN (R 4.0.0)
   reshape2         1.4.4    2020-04-09 [1] CRAN (R 4.0.0)
   rlang            0.4.6    2020-05-02 [1] CRAN (R 4.0.0)
   rprojroot        1.3-2    2018-01-03 [1] CRAN (R 4.0.0)
   rsconnect        0.8.16   2019-12-13 [1] CRAN (R 4.0.0)
   rstan          * 2.19.3   2020-02-11 [1] CRAN (R 4.0.1)
   rstantools       2.0.0    2019-09-15 [1] CRAN (R 4.0.0)
   rstudioapi       0.11     2020-02-07 [1] CRAN (R 4.0.0)
   scales           1.1.1    2020-05-11 [1] CRAN (R 4.0.0)
   sessioninfo      1.1.1    2018-11-05 [1] CRAN (R 4.0.0)
   shiny            1.4.0.2  2020-03-13 [1] CRAN (R 4.0.0)
   shinyjs          1.1      2020-01-13 [1] CRAN (R 4.0.0)
   shinystan        2.5.0    2018-05-01 [1] CRAN (R 4.0.0)
   shinythemes      1.1.2    2018-11-06 [1] CRAN (R 4.0.0)
   StanHeaders    * 2.21.0-5 2020-06-09 [1] CRAN (R 4.0.0)
   stringi          1.4.6    2020-02-17 [1] CRAN (R 4.0.0)
   stringr          1.4.0    2019-02-10 [1] CRAN (R 4.0.0)
   testthat         2.3.2    2020-03-02 [1] CRAN (R 4.0.0)
   threejs          0.3.3    2020-01-21 [1] CRAN (R 4.0.0)
   tibble           3.0.1    2020-04-20 [1] CRAN (R 4.0.0)
   tidyselect       1.1.0    2020-05-11 [1] CRAN (R 4.0.0)
   usethis          1.6.1    2020-04-29 [1] CRAN (R 4.0.0)
   vctrs            0.3.1    2020-06-05 [1] CRAN (R 4.0.0)
   withr            2.2.0    2020-04-20 [1] CRAN (R 4.0.0)
   xtable           1.8-4    2019-04-21 [1] CRAN (R 4.0.0)
   xts              0.12-0   2020-01-19 [1] CRAN (R 4.0.0)
   zoo              1.8-8    2020-05-02 [1] CRAN (R 4.0.0)

[1] C:/Program Files/R/R-4.0.1/library

 D -- DLL MD5 mismatch, broken installation.

I am a begginer about R packages, but I don’t like this last sentence: D – DLL MD5 mismatch, broken installation. Is it a problem?

Thank you (and all others) for the help, I hope to contribute in the future with some solutions and not only questions!

Which version of StanHeaders?

Sumarised from my last post:

> devtools::session_info()
- Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.1 (2020-06-06)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language en                          
 collate  Portuguese_Brazil.1252      
 ctype    Portuguese_Brazil.1252      
 tz       America/Sao_Paulo           
 date     2020-06-13    

> devtools::session_info()
- Packages ---------------------------------------------------------------------------------------------
   rstan          * 2.19.3   2020-02-11 [1] CRAN (R 4.0.1)
   StanHeaders    * 2.21.0-5 2020-06-09 [1] CRAN (R 4.0.0)

The same information is available for @ducoveen session_info in his post above, that is:

> devtools::session_info()
- Session info ---------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.0 (2020-04-24)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_Netherlands.1252    
 ctype    English_Netherlands.1252    
 tz       Europe/Berlin               
 date     2020-06-12                  

- Packages -------------------------------------------------------------------------------------------------------------------
   rstan          * 2.19.3   2020-02-11 [1] CRAN (R 4.0.0)
   StanHeaders    * 2.21.0-3 2020-05-28 [1] CRAN (R 4.0.0)

I wouldn’t try to use StanHeaders 2.21 with rstan 2.19 even though those are the current versions on CRAN. Try

install.packages(
"https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.19.2.tar.gz",
repos = NULL, type = "source")

I installed StanHeaders 2.19.2 as you suggested:

> packageVersion("rstan")
[1] ‘2.19.3’
> packageVersion("StanHeaders")
[1] ‘2.19.2’

The problem still happens, and now the strategy proposed by @ducoveen does not work anymore.

@ducoveen strategy: rstan:: statement and without loading the library:

> sm <- rstan::stan_model(model_code = stan_code)
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file9cc42701a2f.cpp 2> file9cc42701a2f.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file9cc42701a2f.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection 

Normal procedure:

> library(rstan)
> sm <- stan_model(model_code = stan_code)
Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file9cc55da7b9d.cpp 2> file9cc55da7b9d.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file9cc55da7b9d.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

My session informations now:

> devtools::session_info()
- Session info -----------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.1 (2020-06-06)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language en                          
 collate  Portuguese_Brazil.1252      
 ctype    Portuguese_Brazil.1252      
 tz       America/Sao_Paulo           
 date     2020-06-13                  

- Packages ---------------------------------------------------------------------------------------------
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
 backports     1.1.7   2020-05-13 [1] CRAN (R 4.0.0)
 callr         3.4.3   2020-03-28 [1] CRAN (R 4.0.0)
 cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
 colorspace    1.4-1   2019-03-18 [1] CRAN (R 4.0.0)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
 desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
 devtools      2.3.0   2020-04-10 [1] CRAN (R 4.0.0)
 digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
 dplyr         1.0.0   2020-05-29 [1] CRAN (R 4.0.0)
 ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
 fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
 fs            1.4.1   2020-04-04 [1] CRAN (R 4.0.0)
 generics      0.0.2   2018-11-29 [1] CRAN (R 4.0.0)
 ggplot2     * 3.3.1   2020-05-28 [1] CRAN (R 4.0.0)
 glue          1.4.1   2020-05-13 [1] CRAN (R 4.0.0)
 gridExtra     2.3     2017-09-09 [1] CRAN (R 4.0.0)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
 inline        0.3.15  2018-05-18 [1] CRAN (R 4.0.0)
 lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.0)
 loo           2.2.0   2019-12-19 [1] CRAN (R 4.0.0)
 magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
 matrixStats   0.56.0  2020-03-13 [1] CRAN (R 4.0.0)
 memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
 pillar        1.4.4   2020-05-05 [1] CRAN (R 4.0.0)
 pkgbuild      1.0.8   2020-05-07 [1] CRAN (R 4.0.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
 pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
 processx      3.4.2   2020-02-09 [1] CRAN (R 4.0.0)
 ps            1.3.3   2020-05-08 [1] CRAN (R 4.0.0)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
 R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
 Rcpp          1.0.4.6 2020-04-09 [1] CRAN (R 4.0.0)
 remotes       2.1.1   2020-02-15 [1] CRAN (R 4.0.0)
 rlang         0.4.6   2020-05-02 [1] CRAN (R 4.0.0)
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
 rstan       * 2.19.3  2020-02-11 [1] CRAN (R 4.0.0)
 rstudioapi    0.11    2020-02-07 [1] CRAN (R 4.0.0)
 scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.0)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
 StanHeaders * 2.19.2  2020-02-11 [1] CRAN (R 4.0.1)
 testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
 tibble        3.0.1   2020-04-20 [1] CRAN (R 4.0.0)
 tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.0)
 usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
 vctrs         0.3.1   2020-06-05 [1] CRAN (R 4.0.0)
 withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)

[1] C:/Program Files/R/R-4.0.1/library

I installed again StanHeaders and @ducoveen strategy now works:

packageVersion(“rstan”)
[1] ‘2.19.3’
packageVersion(“StanHeaders”)
[1] ‘2.21.0.5’

I’m also getting the error Error in file(con, "r") : cannot open the connection in R 4.0, Windows 10 64-bit, rstan 2.19.3. I’ve tried StanHeaders 2.21.0.5 and 2.19.2, and I get an error with both.

Can you run:

example(stan_model,run.dontrun = TRUE,verbose=TRUE)

And post the line that starts with error:?

Here are the lines with errors:

Compilation argument: C:/PROGRA~1/R/R-40~1.0/bin/x64/R CMD SHLIB file2b8039f440d1.cpp 2> file2b8039f440d1.cpp.err.txt "C:/rtools40/mingw64/bin/"g++ -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG -I"C:/Program Files/R/Packages/Rcpp/include/" -I"C:/Program Files/R/Packages/RcppEigen/include/" -I"C:/Program Files/R/Packages/RcppEigen/include/unsupported" -I"C:/Program Files/R/Packages/BH/include" -I"C:/Program Files/R/Packages/StanHeaders/include/src/" -I"C:/Program Files/R/Packages/StanHeaders/include/" -I"C:/Program Files/R/Packages/rstan/include" -DEIGEN_NO_DEBUG -D_REENTRANT -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp -std=c++1y -march=core2 -include C:/Program Files/R/Packages/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I "C:/Program Files/R/Packages/StanHeaders/include" -I "C:/Program Files/R/Packages/RcppEigen/include" -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c file2b8039f440d1.cpp -o file2b8039f440d1.o g++.exe: error: Files/R/Packages/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory make: *** [C:/PROGRA~1/R/R-40~1.0/etc/x64/Makeconf:229: file2b8039f440d1.o] Error 1

Error in file(con, "r") : cannot open the connection In addition: Warning messages: 1: In find.package(package, lib.loc, verbose = verbose) : package ‘base’ found more than once, using the first from “C:/PROGRA~1/R/R-40~1.0/library/base”, “C:/Program Files/R/R-4.0.0/library/base” 2: In file(con, "r") : cannot open file 'file2b8039f440d1.cpp.err.txt': No such file or directory

This is the issue mentioned above about using RStan 2.19 with StanHeaders 2.21, you can work around this by not loading the RStan package before sampling. So rather than:

library(rstan)
stan(model, data)

Use:

rstan::stan(model, data)

That’s very helpful. Using rstan::stan() worked for me. Thanks for the workaround until the issue is fixed. Much appreciated.

1 Like

Hi @andrjohns and @bgoodri,

I got the same error messages when I tried running " rstan::stan(file = “schools.stan”, data = school.dat)

Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! g++.exe: error: Files/R/R-3.6.0/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory
make: *** [C:/PROGRA~1/R/R-36~1.0/etc/x64/Makeconf:215: file49a433f81349.o] Error 1
In addition: Warning messages:
1: In file.remove(c(unprocessed, processed)) :
cannot remove file ‘C:\Users\n1103343\AppData\Local\Temp\RtmpIl4ZnM\file49a467be37f3.stan’, reason ‘No such file or directory’
2: In system(cmd, intern = !verbose) :
running command ‘C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file49a433f81349.cpp 2> file49a433f81349.cpp.err.txt’ had status 1
Error in sink(type = “output”) : invalid connection

My rstan version is 2.21.1
stanheaders version is 2.21.0-5
Rtools version is rtools35