Problems installing rstan 2.21.2 with Rtools 4

Hello
I am trying to install rstan in RStudio using a university controlled computer that I have admin authority on. My issues has been discussed in numerous other forums but nothing is working for me. I have been following the more recent forum post Problems getting Stan to run after upgrading to R 4.0.2 - #6 by Chris_Dalton.

I have tried two forms of installing.

install.packages("rstan")
install.packages("rstan", type = "source")

However the source option gives me a series of errors and DOES NOT install the package while the standard installation procedure seems to install but still doesn’t run any models.
I have tried various versions of the Makevars.win file creation using

  cat("CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2",file = "~/.R/Makevars.win", sep = "\n", append = FALSE)

or putting the following in the Makevars.win file.

CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2
CXX = C:\rtools40\mingw_64\bin\g++

Now regardless of what I do when I run the example code:

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

I get the following consistent error

Error in compileCode(f, code, language = language, verbose = verbose) : 
                   from file4e0c312d2e87.cpp:14:C:/Users/n9401849/OneDrive - Queensland University of Technology/Documents/R/win-library/4.0/BH/include/boost/fusion/include/make_fused.hpp:11:10: fatal error: C:/Users/n9401849/OneDrive - Queensland University of Technology/Documents/R/win-library/4.0/BH/include/boost/fusion/functional/generation/make_fused.hpp: Invalid argument #include <boost/fusion/functional/generation/make_fused.hpp>          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~compilation terminated.make: *** [C:/PROGRA~1/R/R-40~1.5/etc/x64/Makeconf:229: file4e0c312d2e87.o] Error 1
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.5/library/base”,
  “C:/Program Files/R/R-4.0.5/library/base”
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Any help here would be fantastic. Thank you in advance. Note that throughout these tests I have paused OneDrive syncing as I’m aware this can cause issues.

  • Operating System: Windows 10 Enterprise
  • RStan Version: 2.21.2

A bit more information below

> tools::Rcmd(c('config', 'CXX11'))
C:/rtools40/mingw64/bin/g++

> devtools::session_info("rstan")
- Session info -----------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.5 (2021-03-31)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_Australia.1252      
 ctype    English_Australia.1252      
 tz       Australia/Brisbane          
 date     2021-05-14  

> Sys.getenv("PATH")
[1] "C:\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.0.5\\bin\\x64;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath_target_338484;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH;C:\\Users\\n9401849\\AppData\\Local\\Microsoft\\WindowsApps"
> Sys.getenv("BINPREF")
[1] ""
> readLines("~/.R/Makevars.win")
[1] "CXX14FLAGS += -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"
[2] "CXX = C:\\rtools40\\mingw_64\\bin\\g++ "                              
Warning message:
In readLines("~/.R/Makevars.win") :
  incomplete final line found on '~/.R/Makevars.win'
> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
> readLines("~/.Rprofile")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/n9401849/OneDrive - Queensland University of Technology/Documents/.Rprofile': No such file or directory
1 Like

While I can’t really be of much help (because I normally don’t use R or Windows with Stan), since this has been without a reply for a few days I’ll try to help. I imagine you may have tried installing it outside of a OneDrive folder altogether, does that give you the same result?

Alternatively, have you considered using Windows Subsystem for Linux and run everything within a Linux environment? That’s the only way I ever ran Stan on Windows, issues with Windows seem to come up every now and then in different shapes and sizes…

Hi Jamie,

Sorry about the delay in getting to this one. When you say:

However the source option gives me a series of errors and DOES NOT install the package while the standard installation procedure seems to install but still doesn’t run any models.

That’s going to be pretty key here, since the install will likely fail for the same reason that the models can’t compile. Can you post the full log of errors from trying to install from source? There will likely be a pretty long result, so it might be easier to write the log to a file and upload that here:

sink(file="ErrorLog.txt")

install.packages(c("StanHeaders","rstan"),type="source")

sink(file=NULL)

Thankyou for your response caesoma - At this stage I have not tried installing outside a OneDrive folder. I plan to try this if andrjohns cannot determine the source of the error. In terms of Linux, I will be using Stan in some applications but not ALL and therefore I feel that the steep learning curve of using Linux may not be worth my time at this stage.

Hi andrjohns, thankyou for your response here. I have tried using the sink file code (result is ErrorLog.txt attached) you suggested. Unfortunately this does not have the entire result from installing from source. I have copied as much of the result from the console as I can and included this in another file (attached) called error log.txt. Let me know what you think. My concern is how often is says “failed ***************” throughout the log.
Thanks again.
Jamie
error log.txt (205.3 KB)
ErrorLog.txt (91 Bytes)

Hm so there are two failures here:

C:/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -ltbb
C:/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -ltbbmalloc
collect2.exe: error: ld returned 1 exit status

and then the original:

functional/generation/make_fused.hpp: Invalid argument
 #include <boost/fusion/functional/generation/make_fused.hpp>

I’m currently also getting those first errors when installing from source (they’re related to the RcppParallel package, so I’ll need to dig into that a bit more.

In the interim, can you try the experimental preview of RStan 2.26:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

That’s avoiding a few of these issues at the moment

Since your message I have setup packages to be installed outside of OneDrive. Your way of installing has worked, but when I run example(stan_model, run.dontrun = TRUE, verbose=TRUE). I get the following new error message.

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.5/library/base”,
  “C:/Program Files/R/R-4.0.5/library/base”
2: In readLines(file.path(makevar_files)) :
  incomplete final line found on 'C:/Users/n9401849/OneDrive - Queensland University of Technology/Documents/.R/Makevars.win'

However, I do get the fit and fit2 objects - so I suppose this has worked?

I have then tried to run an arbitrary example that works perfectly on my personal computer. Code below.

# Create the random data-------------------------------------------------------
  rm(list=ls())
  y=rnorm(100, 5, 1)
  y[67] = 10
  my_data=list(N=100, y=y) #the data must be in list format

# Model------------------------------------------------------------------------
  model <- "
  data {
      int<lower=0> N; //#cannot be less than 0
      vector[N] y;
    }
    parameters {
      real mu;
      real<lower=0> sigma; //#cannot be less than 0
    }
    model {
      mu~normal(0,100);
      sigma ~ cauchy(0,1);
      y ~ normal(mu, sigma);
  }
  generated quantities {
    vector[N] log_lik;
    vector[N] ypred;
    int<lower=0, upper=1> sd_gt = sd(ypred)>sd(y);
    for (n in 1:N){
    log_lik[n] = normal_lpdf(y[n] | mu, sigma); //# this is th log likelihood
                                                //# will be smaller when y[n] is further from the 
                                                //#estimated mu and sigma
    ypred[n] = normal_rng(mu, sigma);
    }
  }"

# Fit the model with 2 chains--------------------------------------------------
  fit <- stan(model_code = model, data = my_data, 
              chains = 2, 
              iter = 1000, 
              warmup =200) #, cores=8)

However, this returns a new error.

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  parser failed badly

Any ideas?

That’s interesting, the parser is failing because of how you’ve written your comments:

//#cannot be less than 0

If you remove the # then it all works

@hsbadr there’s an interesting edge case with the parser here, it fails if comments have a # prefix:

For example:

  //#comment

hey andrjohns.

This works! You are an absolute savour. Thank you so much.

The model runs but I still get this error message. Should I be worried about this?

Warning message:
In readLines(file.path(makevar_files)) :
  incomplete final line found on 'C:/Users/n9401849/OneDrive - Queensland University of Technology/Documents/.R/Makevars.win'

No that’s harmless. You can make it go away by appending a blank line to your Makevars:

cat("",file = "~/.R/Makevars.win", sep = "\n", append = TRUE)

Great, this has removed the error message . Thankyou andrjohns. Your swift replies are greatly appreciated!

1 Like

@andrjohns I think this is a stanc3 issue (@rok_cesnovar ?). in rstan:::stanc_process we process the headers:

The current function passes the comments to stanc3, but we could remove them from all code blocks, if needed.

1 Like

Yeah, this was as issue that should be fixed with 2.27. The issue was that deprecation warnings were just printed to stderr as opposed to returned.

2 Likes