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")))