Error in sink(type = "output") : invalid connection

Hello,

I’m new to running stan code and seem to be having issues having my model run in R.

Versions:

R 4.6.0

RTools 4.5

rstan 2.32.7

StanHeaders 2.39.1

Rcpp 1.1.2

I’ve tried running a simple model to test whether R can compile the code correctly and I keep on encountering the error:

Error in sink(type = "output") : invalid connection

I’m using this model to check whether it works:

stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
mod <- stan_model(model_code = stancode, verbose = TRUE)

If anyone could point me towards what I can do to debug the problem, that would be really helpful as I’m needing to start running some larger models soon.

Thanks in advance,
Kieran

Welcome to the Stan forum. RStan redirects output while it compiles your model, and if the C++ compilation fails then the cleanup step will error like this and hide the real problem unfortunately. Since you set verbose = TRUE, I think the actual compiler error should be printed somewhere further up in the output. Could you post the first error lines from there if you can find them? That should tell us what’s actually going wrong here.

Also, can you run pkgbuild::has_build_tools(debug = TRUE) and post the output? That should help us confirm that R is actually able to find and use Rtools45.

Hello Jonah and Kieran, I seem to have the same problem, and maybe instead of opening a new issue, it’s better to join here. My version of R is 4.5.3, Rtools 4.5, rstan 2.32.7, StanHeaders 2.39.1, Rcpp 1.1.2. The full error message is

make cmd is
  make -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/etc/x64/Makeconf" -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/share/make/winshlib.mk" CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB="file97fc48bb606a.dll" WIN=64 TCLBIN= OBJECTS="file97fc48bb606a.o"

make would use
g++  -std=gnu++17 -I"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/include" -DNDEBUG   -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/Rcpp/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/unsupported"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/BH/include" -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -DTBB_INTERFACE_NEW -IC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign    -c file97fc48bb606a.cpp -o file97fc48bb606a.o
if test "zfile97fc48bb606a.o" != "z"; then \
  if test -e "file97fc48bb606a-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file97fc48bb606a.dll file97fc48bb606a-win.def file97fc48bb606a.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file97fc48bb606a.dll file97fc48bb606a-win.def file97fc48bb606a.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file97fc48bb606a.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo g++  -shared -s -static-libgcc -o file97fc48bb606a.dll tmp.def file97fc48bb606a.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file97fc48bb606a.dll tmp.def file97fc48bb606a.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

Error in sink(type = "output") : invalid connection


Running

pkgbuild::has_build_tools(debug = TRUE)

as you propose results in:

Trying to compile a simple C file
Running "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
T_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall -std=gnu2x  -mfpmath=sse -msse2 -mstackrealign   -c foo.c -o foo.osupported"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/BH/include" -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -DTBB_INTERFACE_NEW -IC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOS
cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
In file included from <command-line>:
C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:3:10: fatal error: stdexcept: No such file or directory
    3 | #include <stdexcept>
      |          ^~~~~~~~~~~
compilation terminated.
make: *** [C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/etc/x64/Makeconf:295: foo.o] Error 1
[1] FALSE

I also tried to run

example(stan_model, package = "rstan", run.dontrun = TRUE)

which gives a very long output, among which

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

and



Compilation ERROR, function(s)/method(s) not created!

Error in compileCode(f, code, language = language, verbose = verbose) : 
  bine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> >]'
   22 |     return 0.5 * z.p.transpose() * z.inv_e_metric_ * z.p;
C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0:   required from here
   21 |   double T(dense_e_point& z) {
C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__m128d'} [-Wignored-attributes]
  654 |   return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
      |                                                                          ^~~~~~~~~
make: *** [C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/etc/x64/Makeconf:302: file97fc3f1a5565.o

seem to be the main message.

Any help would be appreciated. Thanks in advance!

Maria

Can you try installing from r-universe and seeing if that fixes it? You can try with this code:

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

Thanks for the advice! I tried it, but it still gives the same error. In case you need the details, the code I have tried to run:

stan_code <- "
data {
int n;
array[n] real y;
}

parameters {
real mu;
real<lower=0> sigma;
}

model {
for (i in 1:n)
y[i] ~ normal(mu,sigma);
mu ~ normal(1.7,0.3);
sigma ~ cauchy(0,1);
}
"
n     = 100
y     = rnorm(n,1.6,0.2)

model = stan_model(model_code = stan_code)

and the output is

make cmd is
  make -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/etc/x64/Makeconf" -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/share/make/winshlib.mk" CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB="fileb0443e9210d4.dll" WIN=64 TCLBIN= OBJECTS="fileb0443e9210d4.o"

make would use
g++  -std=gnu++17 -I"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/include" -DNDEBUG   -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/Rcpp/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/unsupported"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/BH/include" -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -DTBB_INTERFACE_NEW -IC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign    -c fileb0443e9210d4.cpp -o fileb0443e9210d4.o
if test "zfileb0443e9210d4.o" != "z"; then \
  if test -e "fileb0443e9210d4-win.def"; then \
    echo g++  -shared -s -static-libgcc -o fileb0443e9210d4.dll fileb0443e9210d4-win.def fileb0443e9210d4.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o fileb0443e9210d4.dll fileb0443e9210d4-win.def fileb0443e9210d4.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm fileb0443e9210d4.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo g++  -shared -s -static-libgcc -o fileb0443e9210d4.dll tmp.def fileb0443e9210d4.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o fileb0443e9210d4.dll tmp.def fileb0443e9210d4.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

Error in sink(type = "output") : invalid connection

Hi @maria.sueveges, can you try a few more things that will hopefully help figure out the issue?

  1. Start a completely fresh R session. Before loading rstan, run this and share the full output:
.libPaths()
ip <- installed.packages()
ip[ip[, "Package"] %in% c("rstan", "StanHeaders"), c("Package", "Version", "LibPath"), drop = FALSE]
Sys.getenv("PKG_CPPFLAGS")
tools::makevars_user()
pkgbuild::has_build_tools(debug = TRUE)

This tells us which versions of rstan and StanHeaders actually got installed, whether there are duplicate copies in different libraries, and whether the compiler works when rstan hasn’t been loaded first.

I think it’s likely the reinstall didn’t work properly, but I’m not 100% sure.

  1. After getting the output above, in the same fresh R session without rstan loaded:
remove.packages(c("rstan", "StanHeaders"))
install.packages("StanHeaders", repos = "https://stan-dev.r-universe.dev")
install.packages("rstan", repos = c("https://stan-dev.r-universe.dev", getOption("repos")))
  1. After installation restart R again and then run this to test if it worked. The important part here is verbose = TRUE. You can use the same Stan code and data you were using before, so first define stan_code before running the stan_model command below.
packageVersion("rstan"); packageVersion("StanHeaders")  # both should be 2.39.0.9000
library(rstan)
model <- stan_model(model_code = stan_code, verbose = TRUE)

With verbose=TRUE the output will be long and some warnings are normal. If it ends without an Error message, it worked and in the future you can run without verbose = TRUE. If it does end with an error, please search the output for lines containing error: (with the colon) and share those, plus a few lines before each.

Sorry for the hassle, I hope we can help you get everything running smoothly!

Hi Jonah, thanks for your reply! Here are the replies to your points:

1:

> .libPaths()
[1] "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library"
> ip <- installed.packages()
> ip[ip[, "Package"] %in% c("rstan", "StanHeaders"), c("Package", "Version", "LibPath"), drop = FALSE]
            Package       Version 
rstan       "rstan"       "2.32.7"
StanHeaders "StanHeaders" "2.39.1"
            LibPath                                                       
rstan       "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library"
StanHeaders "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library"
> Sys.getenv("PKG_CPPFLAGS")
[1] ""
> tools::makevars_user()
character(0)
> pkgbuild::has_build_tools(debug = TRUE)
Trying to compile a simple C fileRunning "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/bin/x64/Rcmd.exe" SHLIB foo.c
using C compiler: 'gcc.exe (GCC) 14.3.0'
gcc  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/include" -DNDEBUG     -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall -std=gnu2x  -mfpmath=sse -msse2 -mstackrealign   -c foo.c -o foo.o
gcc -shared -s -static-libgcc -o foo.dll tmp.def foo.o -LC:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64 -LC:/rtools45/x86_64-w64-mingw32.static.posix/lib -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/bin/x64 -lR
[1] TRUE

Successfully removed and reinstalled rstan and StanHeader (just copy-pasting what you recommended above). Then restarting R, the checks gave this:

> packageVersion("rstan"); packageVersion("StanHeaders")  # both should be 2.39.0.9000
[1] ‘2.32.7’
[1] ‘2.39.1’

so this is not what you want, apparently.

Defining my stan command and trying to set up the model, the error is still there. The environment of the output around the two error lines:


86 :           "constrained_param_names",
 487 :           &rstan::stan_fit<stan_model,
 488 :                            boost::random::ecuyer1988>::constrained_param_names)
 489 :       .method("standalone_gqs",
 490 :               &rstan::stan_fit<stan_model,
 491 :                                boost::random::ecuyer1988>::standalone_gqs);
 492 : }
 493 : 
 494 : 
 495 : // declarations
 496 : extern "C" {
 497 : SEXP file28205bfd47f9( ) ;
 498 : }
 499 : 
 500 : // definition
 501 : SEXP file28205bfd47f9() {
 502 :  return Rcpp::wrap("anon_model");
 503 : }
make cmd is
  make -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/etc/x64/Makeconf" -f "C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/share/make/winshlib.mk" CXX='$(CXX17) $(CXX17STD)' CXXFLAGS='$(CXX17FLAGS)' CXXPICFLAGS='$(CXX17PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX17LDFLAGS)' SHLIB_LD='$(SHLIB_CXX17LD)' SHLIB="file28205bfd47f9.dll" WIN=64 TCLBIN= OBJECTS="file28205bfd47f9.o"

make would use
g++  -std=gnu++17 -I"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/include" -DNDEBUG   -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/Rcpp/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/unsupported"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/BH/include" -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/"  -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -DTBB_INTERFACE_NEW -IC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/include -I"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign    -c file28205bfd47f9.cpp -o file28205bfd47f9.o
if test "zfile28205bfd47f9.o" != "z"; then \
  if test -e "file28205bfd47f9-win.def"; then \
    echo g++  -shared -s -static-libgcc -o file28205bfd47f9.dll file28205bfd47f9-win.def file28205bfd47f9.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file28205bfd47f9.dll file28205bfd47f9-win.def file28205bfd47f9.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    nm file28205bfd47f9.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo g++  -shared -s -static-libgcc -o file28205bfd47f9.dll tmp.def file28205bfd47f9.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    g++  -shared -s -static-libgcc -o file28205bfd47f9.dll tmp.def file28205bfd47f9.o  "C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/rstan/lib/x64/libStanServices.a" -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64" -ltbb -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/libs/x64 -lRcppParallel -LC:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppParallel/lib/x64 -ltbb -ltbbmalloc -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib/x64" -L"C:/rtools45/x86_64-w64-mingw32.static.posix/lib"  -L"C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

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


....

436: RCPP_MODULE(stan_fit4model28205ed14161__mod) {
437:   class_<rstan::stan_fit<stan_model, boost::random::ecuyer1988> >(
438:       "stan_fit4model28205ed14161_")
439: 
440:       .constructor<SEXP, SEXP, SEXP>()
441: 
442:       .method(
443:           "call_sampler",
444:           &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::call_sampler)
445:       .method(
446:           "param_names",
447:           &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_names)
448:       .method("param_names_oi",
449:               &rstan::stan_fit<stan_model,
450:                                boost::random::ecuyer1988>::param_names_oi)
451:       .method("param_fnames_oi",
452:               &rstan::stan_fit<stan_model,
453:                                boost::random::ecuyer1988>::param_fnames_oi)
454:       .method(
455:           "param_dims",
456:           &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::param_dims)
457:       .method("param_dims_oi",
458:               &rstan::stan_fit<stan_model,
459:                                boost::random::ecuyer1988>::param_dims_oi)
460:       .method("update_param_oi",
461:               &rstan::stan_fit<stan_model,
462:                                boost::random::ecuyer1988>::update_param_oi)
463:       .method("param_oi_tidx",
464:               &rstan::stan_fit<stan_model,
465:                                boost::random::ecuyer1988>::param_oi_tidx)
466:       .method("grad_log_prob",
467:               &rstan::stan_fit<stan_model,
468:                                boost::random::ecuyer1988>::grad_log_prob)
469:       .method("log_prob",
470:               &rstan::stan_fit<stan_model, boost::random::ecuyer1988>::log_prob)
471:       .method("unconstrain_pars",
472:               &rstan::stan_fit<stan_model,
473:                                boost::random::ecuyer1988>::unconstrain_pars)
474:       .method("constrain_pars",
475:               &rstan::stan_fit<stan_model,
476:                                boost::random::ecuyer1988>::constrain_pars)
477:       .method(
478:           "num_pars_unconstrained",
479:           &rstan::stan_fit<stan_model,
480:                            boost::random::ecuyer1988>::num_pars_unconstrained)
481:       .method(
482:           "unconstrained_param_names",
483:           &rstan::stan_fit<
484:               stan_model, boost::random::ecuyer1988>::unconstrained_param_names)
485:       .method(
486:           "constrained_param_names",
487:           &rstan::stan_fit<stan_model,
488:                            boost::random::ecuyer1988>::constrained_param_names)
489:       .method("standalone_gqs",
490:               &rstan::stan_fit<stan_model,
491:                                boost::random::ecuyer1988>::standalone_gqs);
492: }
493: 
494: 
495: // declarations
496: extern "C" {
497: SEXP file28205bfd47f9( ) ;
498: }
499: 
500: // definition
501: SEXP file28205bfd47f9() {
502:  return Rcpp::wrap("anon_model");
503: }

Compilation ERROR, function(s)/method(s) not created!

Error in compileCode(f, code, language = language, verbose = verbose) : 
  bine_engine<boost::random::linear_congruential_engine<unsigned int, 40014, 0, 2147483563>, boost::random::linear_congruential_engine<unsigned int, 40692, 0, 2147483399> >]'
   22 |     return 0.5 * z.p.transpose() * z.inv_e_metric_ * z.p;
C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/StanHeaders/include/src/stan/mcmc/hmc/hamiltonians/dense_e_metric.hpp:21:0:   required from here
   21 |   double T(dense_e_point& z) {
C:/Users/RDSuevegMa/AppData/Local/Programs/R/R-4.5.3/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:654:74: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits<double>::type' {aka '__m128d'} [-Wignored-attributes]
  654 |   return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
      |                                                                          ^~~~~~~~~
make: *** [C:/Users/RDSUEV~1/AppData/Local/Programs/R/R-45~1.3/etc/x64/Makeconf:302: file28205bfd47f9.o

Hope this is sufficient, let me know if you need anything else from the output.

Thanks, this is very helpful! The good news is that your compiler setup seems to be ok. The problem is almost certainly the version mismatch between RStan and StanHeaders.

It looks like the install from r-universe didn’t go through properly but I’m really not sure why, it’s quite strange. When you ran install.packages("StanHeaders", repos = "https://stan-dev.r-universe.dev"), did you see a warning like “unable to access index for repository” or “package ‘StanHeaders’ is not available”?

If you can’t get the r-universe installation to work then you could try installing an older CRAN version of StanHeaders. In a fresh R session:

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

This builds the package from source, so it will take a few minutes and print lots of output, which is normal. Then restart R and check:

# this time should be 2.32.7 and 2.32.10
packageVersion("rstan"); packageVersion("StanHeaders")  
library(rstan)
model <- stan_model(model_code = stan_code)

If that works then one caution: if R or RStudio later asks to update the StanHeaders package, say no for now.

I’m still not sure why the r-universe installation failed, that would be the preferred solution here.

Checking back the output, yes, the StanHeaders installation first failed with
unable to access index for repository ``https://stan-dev.r-universe.dev/bin/windows/contrib/4.5:
cannot open URL '``https://stan-dev.r-universe.dev/bin/windows/contrib/4.5/PACKAGES``'

then fell back to download the default from R-cran.

The old version installation failed with an SSL error:

> install.packages(
+   "https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz",
+   repos = NULL, type = "source"
+ )
trying URL 'https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz'
Error in .rs.downloadFile(url = p, destfile = destfile, method = method,  : 
  cannot open URL 'https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz'
In addition: Warning message:
In .rs.downloadFile(url = p, destfile = destfile, method = method,  :
  URL 'https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz': status was 'SSL connect error'
tar.exe: Error opening archive: Failed to open 'https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz'
Warning messages:
1: In system(cmd, intern = TRUE) :
  running command 'tar.exe -tf "https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz"' had status 1
2: In min(n) : no non-missing arguments to min; returning Inf
3: In untar(packagePath, files = descPath, exdir = tmpdir) :
  ‘tar.exe -xf "https://cran.r-project.org/src/contrib/Archive/StanHeaders/StanHeaders_2.32.10.tar.gz" -C "C:/Users/RDSUEV~1/AppData/Local/Temp/Rtmpkvp5rL/description-a404734e155d"’ returned error code 1

Checking directly R-cran, I did not find StanHeader 2.32.10 (and rstan) in the archived list at all. I located it at GitHub (https://github.com/cran/StanHeaders/archive/refs/tags/2.32.10.tar.gz), and run the installation with that link. It ended with

...
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (StanHeaders)
tar.exe: Error opening archive: Failed to open 'https://github.com/cran/StanHeaders/archive/refs/tags/2.32.10.tar.gz'
Warning messages:
1: In system(cmd, intern = TRUE) :
  running command 'tar.exe -tf "https://github.com/cran/StanHeaders/archive/refs/tags/2.32.10.tar.gz"' had status 1
2: In min(n) : no non-missing arguments to min; returning Inf
3: In untar(packagePath, files = descPath, exdir = tmpdir) :
  ‘tar.exe -xf "https://github.com/cran/StanHeaders/archive/refs/tags/2.32.10.tar.gz" -C "C:/Users/RDSUEV~1/AppData/Local/Temp/Rtmpkvp5rL/description-a40472147974"’ returned error code 1

Ouch. Any ideas?

Strange, that URL definitely works. This seems like it must have to do with restrictions on your network, but I’m not sure.

But your other attempt may have actually succeeded:

Actually I think that might have worked. The * DONE (StanHeaders) line means StanHeaders installed successfully. The tar.exe errors after might be harmless. Let’s test it. Could you restart R and run:

packageVersion("rstan"); packageVersion("StanHeaders")  # should be 2.32.7 and 2.32.10
library(rstan)
model <- stan_model(model_code = stan_code)

What happens when you try that?

Yay, indeed it worked, thanks a lot for your help! The versions now are what they are supposed to be, and the command ran without error (although very slowly).

I guess this incompatibility of the versions occurs for everyone who updates or just installs rstan like I have done. Maybe R should not propose incompatible versions for two dependent packages. Can this be reported somehow?

Many thanks again!

Great, I’m glad it’s working!

Right now this only affects a subset of Windows users that have a something in their setup that differs from what CRAN uses to test on Windows. The two different version numbers on CRAN were intended to preserve compatibility with each other until subsequent releases that bring the version numbers in line with each other. And they are compatible on Mac and Linux and even on CRAN’s Windows machines (all the tests are passing there). But we’ve seen several Windows users with compatibility issues, so there seems to be a compatibility problem with certain setups that we didn’t anticipate. We need to submit a new update to CRAN that fixes the problem for that subset of users.

Anyway, sorry for the hassle and I’m glad it’s working now. If you can figure out the network issue that prevented you from installing from R universe you can get an even more recent version before it makes it onto CRAN. But for now you can use what’s working for you.

All is well then, I hope the problem will be soon solved for those users too. Thanks a lot again for your help!