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

I installed rstan 2.21.8 and r 4.2.2

when I run the 8 schools model I get

Error in compileCode(f, code, language = language, verbose = verbose) : **
** from C:/Tools/R-4.2.2/library/StanHeaders/include/src/stan/model/model_header.hpp:4, from file5c48646a7ac5.cpp:14:C:/Tools/R-4.2.2/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:13: warning: ‘void stan::math::set_zero_all_adjoints()’ defined but not used [-Wunused-function] static void set_zero_all_adjoints() { ^make: *** [C:/Tools/R-42~1.2/etc/x64/Makeconf:260: file5c48646a7ac5.o] Error 1

Error in sink(type = “output”) : invalid connection

A stan forum link said to try: install.packages(c(“StanHeaders”,“rstan”),type=“source”)

and when I did that Stan Headers ‘2.21.0.7’ was installed but rstan installation gave this errror:

C:/Tools/R-4.2.2/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints_nested.hpp:17:13: warning: ‘void stan::math::set_zero_all_adjoints_nested()’ defined but not used [-Wunused-function]
** static void set_zero_all_adjoints_nested() {**
** ^**
make: *** [C:/Tools/R-42~1.2/etc/x64/Makeconf:260: Module.o] Error 1
ERROR: compilation failed for package ‘rstan’
*** removing ‘C:/Tools/R-4.2.2/library/rstan’**
*** restoring previous ‘C:/Tools/R-4.2.2/library/rstan’**
Warning in install.packages :
** installation of package ‘rstan’ had non-zero exit status**

Can you advise how to eliminate the error?

Thank you

See the Getting Started Guide

I literally created this account just to say this answer is awful. Seeing an official stan developer saying “lol just read the guide” is W I L D, at that point just don’t respond TBH.

anyways, for anyone still running into this in 2025 - see an actually useful answer here brms error with new R and R studio download - Stack Overflow

Some versions of rstan have compatibility issues with rtools42, so you might have to do some manual jiggering of versions.

1 Like

This was an extremely common error that had a dedicated section in the documentation being linked to (looks like it’s since been removed) that I was helping people debug multiple times a week.

It wasn’t “go read the docs”, it was “go read this section with step-by-step instructions for debugging and resolving your issue”

You have to keep in mind that a lot of the community support is volunteer-based, and these questions are getting answered after work or on a lunch break - so things aren’t going to be perfect