Rstan installation troubles

Hello. I am trying to install rstan fresh on a new machine. I have followed all the steps given here (RStan Getting Started · stan-dev/rstan Wiki · GitHub) but I cannot get the test model to run (example(stan_model, package = "rstan", run.dontrun = TRUE), nor can I get any of my own models to run.

Using my own models, I get the following error:

Error in compileCode(f, code, language = language, verbose = verbose) : Error in sink(type = "output") : invalid connection

To be clear, I have tried to install rstan both normally and from source. From what I can tell, it actually installs without any issues.

Operating system: Windows 11
RStan version: 2.21.8 seems to be installed
Output of writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars"))):

Error in file(con, "r") : cannot open the connection In addition: Warning message: In file(con, "r") : cannot open file 'C:/Users/user/Documents/.R/Makevars': No such file or directory

Output of devtools::session_info("rstan"):

Many thanks!

You’ll need to use the rstan preview, which you can install via:

remove.packages(c("StanHeaders", "rstan"))

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

Thanks for your reply.

I tried what you suggested and now it doesn’t seem to recognise the package is installed at all.

library(rstan)

returns
Error in library(rstan) : there is no package called ‘rstan’

Which is really weird, because if I start typing library(r… it comes up with rstan.

As a sanity check, library(tidyverse) works no problem, so it doesn’t appear to be an issue with loading packages in general.

What output did you get from running:

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

Installing package into ‘C:/Users/user/AppData/Local/R/win-library/4.3’
(as ‘lib’ is unspecified)
also installing the dependency ‘StanHeaders’

Warning in install.packages :
unable to access index for repository https://mc-stan.org/r-packages/bin/windows/contrib/4.3:
cannot open URL ‘https://mc-stan.org/r-packages/bin/windows/contrib/4.3/PACKAGES

There are binary versions available but the source versions are later:
binary source needs_compilation
StanHeaders 2.21.0-7 2.26.22 TRUE
rstan 2.21.8 2.26.22 TRUE

installing the source packages ‘StanHeaders’, ‘rstan’

trying URL ‘https://mc-stan.org/r-packages/src/contrib/StanHeaders_2.26.22.tar.gz
Content type ‘application/gzip’ length 2758280 bytes (2.6 MB)
downloaded 2.6 MB

trying URL ‘https://mc-stan.org/r-packages/src/contrib/rstan_2.26.22.tar.gz
Content type ‘application/gzip’ length 610748 bytes (596 KB)
downloaded 596 KB

Warning in install.packages :
installation of package ‘StanHeaders’ had non-zero exit status
Warning in install.packages :
installation of package ‘rstan’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\user\AppData\Local\Temp\RtmpwJGzkf\downloaded_packages’

That means that the installation failed, are there any output or error messages that indicate why? Have you installed rtools?

I have. I installed version Rtools 43 (presumably this is the version I would want?). Just to double check, I uninstalled it and reinstalled it just now and the error is still persisting.

All the output it gives me is just what I pasted above, so maybe not that helpful…

Are you able to install any packages from source? What output do you get from:

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

So that package appeared to install fine, though here is the output:

Installing package into ‘C:/Users/user/AppData/Local/R/win-library/4.3’
(as ‘lib’ is unspecified)
trying URL ‘https://cran.rstudio.com/src/contrib/Rcpp_1.0.10.tar.gz
Content type ‘application/x-gzip’ length 2936173 bytes (2.8 MB)
downloaded 2.8 MB

Warning in install.packages :
installation of package ‘Rcpp’ had non-zero exit status

The downloaded source packages are in
‘C:\Users\user\AppData\Local\Temp\RtmpQ3SPUV\downloaded_packages’

And then running library(rcpp) appears to load the package without any problems

I am having the same issue.

Warning in install.packages :
installation of package ‘Rcpp’ had non-zero exit status

This means that it did not install successfully, and it’s just reverted to the existing binary package. This isn’t an rstan issue, it’s your R or Rtools installation

Do you have any .Renviron or .Rprofile files where you’ve changed settings?

Ahh right okay, that makes sense.

I only installed R fresh yesterday as this is a new machine, so I haven’t changed anything knowingly. Is there something I can check to make sure everything in the background is set up correctly?

Can you install the pkgbuild package and run:

pkgbuild::check_build_tools(debug = TRUE)
pkgbuild::check_compiler(debug = TRUE)

pkgbuild::check_build_tools(debug = TRUE)
Scanning R CMD config CC…
cc_path:
‘’ does not exist
Scanning path…
ls: C:\rtools43\usr\bin\ls.exe
gcc_path: C:\rtools43\X86_64~1.POS\bin\gcc.exe
Scanning registry…
Found C:/rtools42 for 4.2
Found C:/rtools43 for 4.3.5550
Your system is ready to build packages!
pkgbuild::check_compiler(debug = TRUE)
Trying to compile a simple C file
Running “C:/PROGRA~1/R/R-43~1.0/bin/x64/Rcmd.exe” SHLIB foo.c
Error: Failed to compile C code

It seems to be referring also to rtools42 which I think I installed by mistake first but then uninstalled and deleted the files. I wonder if that is causing problems?

One last check, what output do you get from:

Rcpp::cppFunction(
    'int fibonacci(const int x) {
        if (x == 0) return(0);
        if (x == 1) return(1);
        return (fibonacci(x - 1)) + fibonacci(x - 2);
    }',verbose=TRUE)

And is this a personal computer or one issued by an organisation?

Its a personal computer.

Running the code you pasted prompted the installation of a C++ compiler. I click yes and it seems to download something, but the code gives this error:

Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, : Error 65535 occurred building shared library. trying URL 'https://cran.rstudio.com/bin/windows/Rtools/rtools43/files/rtools43-5550-5548.exe' Content type 'application/x-msdownload' length 487427824 bytes (464.8 MB)

Wow, it really doesn’t want to find your toolchain. All I can think of now is to check your path and config, after that the only option might be to try completely uninstalling and re-installing both R and Rtools.

What output do you get from:

system("C:/rtools43/X86_64~1.POS/bin/gcc.exe -v")
Sys.getenv("PATH")
Sys.getenv("BINPREF")
readLines("~/.Rprofile")
readLines("~/.Renviron")

system("C:/rtools43/X86_64~1.POS/bin/gcc.exe -v"): 0

Sys.getenv("PATH"): [1] "C:\rtools43\x86_64-w64-mingw32.static.posix\bin;C:\rtools43\usr\bin;\usr\bin;C:\Program Files\R\R-4.3.0\bin\x64;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\user\AppData\Local\Microsoft\WindowsApps;C:\Program Files\RStudio\resources\app\bin\quarto\bin;C:\Program Files\RStudio\resources\app\bin\postback

Sys.getenv("BINPREF"): “”

readLines("~/.Rprofile"): Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/user/Documents/.Rprofile’: No such file or directory

I don’t mind doing a fresh install if it’s needed. I’ve never had this much trouble getting stan installed though, its weird.

Sorry, missed that one.

[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""

Ah, it looks like you do have an .Renviron file with a setting changed.

Can you delete that file in your home directory (usually your documents folder), then restart R and try running pkgbuild::check_compiler(debug = TRUE) again?