Rstan connection not establishing with rtools

I created a modelString and running stan_model but getting errors. The stan_model function was working well till yesterday. Due to some error I reinstalled Rtools multiple times. And now the error is persistent.
My modelString:
modelString<-"
data {
int<lower=1> Ntotal;
real y[Ntotal];
int<lower=2> NxLvl;
int<lower=1, upper=NxLvl> x[Ntotal];
real<lower=0> agammaShRa[2];
}
transformed data {
real meanY;
real sdY;
meanY = mean(y);
sdY = sd(y);
}
parameters {
real a0;
real<lower=0> aSigma;
vector[NxLvl] a;
real<lower=0> ySigma;
}
model {
a0 ~ normal(meanY, 5sdY);
aSigma ~ gamma(agammaShRa[1], agammaShRa[2]);
a ~ normal(0, aSigma);
ySigma ~ uniform(sdY/100, sdY
10);
for ( i in 1:Ntotal ) {
y[i] ~ normal(a0 + a[x[i]], ySigma);
}
}
generated quantities {
// Convert a0,a[] to sum-to-zero b0,b[] :
real b0;
vector[NxLvl] b;
b0 = a0 + mean(a);
b = a - mean(a);
}"

stanDso <- stan_model(model_code = modelString)

Errors
Error in file(con, “r”) : cannot open the connection
9.
file(con, “r”)
8.
readLines(errfile)
7.
compileCode(f, code, language = language, verbose = verbose)
6.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, …, verbose = verbose)
5.
force(code)
4.
withr::with_path(rtools_path(), code)
3.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, …, verbose = verbose), required = rstan_options(“required”) && !identical(Sys.getenv(“WINDOWS”), “TRUE”) && !identical(Sys.getenv(“R_PACKAGE_SOURCE”), …
2.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap("", model_name, “”);", sep = “”), includes = inc, plugin = “rstan”, save_dso = save_dso | auto_write, module_name = paste(“stan_fit4”, model_cppname, “_mod”, sep = “”), verbose = verbose)
1.
stan_model(model_code = modelString)

Error in sink(type = “output”) : invalid connection
3.
sink(type = “output”)
2.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap("", model_name, “”);", sep = “”), includes = inc, plugin = “rstan”, save_dso = save_dso | auto_write, module_name = paste(“stan_fit4”, model_cppname, “_mod”, sep = “”), verbose = verbose)
1.
stan_model(model_code = modelString)

What version of R and RTools are you running?

Can you run the test model:

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

And post the line that starts with error:

Thank you Andrew. I am using R 4.0.1, RStudio 1.3.959 and Rtools40.

I ran the example(stan_model, run.dontrun = TRUE, verbose=TRUE) and got the below error…

Found file = ‘C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/rstan/help/stan_model’
‘envir’ chosen:<environment: R_GlobalEnv>
encoding = “UTF-8” chosen
→ parsed 4 expressions; now eval(.)ing them:
has srcrefs:
List of 4
: 'srcref' int [1:8] 8 1 8 84 1 84 8 8 ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x0000025abb510ca8> : ‘srcref’ int [1:8] 9 1 9 56 1 56 9 9
…- attr(, “srcfile”)=Classes ‘srcfilecopy’, ‘srcfile’ <environment: 0x0000025abb510ca8>
: 'srcref' int [1:8] 10 1 10 45 1 45 10 10 ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x0000025abb510ca8> : ‘srcref’ int [1:8] 11 1 11 46 1 46 11 11
…- attr(
, “srcfile”)=Classes ‘srcfilecopy’, ‘srcfile’ <environment: 0x0000025abb510ca8>

eval(expression_nr. 1 )
=================

stn_md> stancode ← ‘data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}’
curr.fun: symbol ←
… after ‘expression(stancode ← ‘data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}’)’

eval(expression_nr. 2 )
=================

stn_md> mod ← stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model ‘73fc79f8b1915e8208c736914c86d1a1’.
COMPILING THE C++ CODE FOR MODEL ‘73fc79f8b1915e8208c736914c86d1a1’ NOW.
OS: x86_64, mingw32; rstan: 2.19.3; Rcpp: 1.0.4.6; inline: 0.3.15

setting environment variables:
PKG_LIBS = -L"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders
PKG_CPPFLAGS = -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/Rcpp/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/BH/include" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG -D_REENTRANT -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp -std=c++1y
Program source :

1 :
2 : // includes from the plugin
3 : // [[Rcpp::plugins(cpp14)]]
4 :
5 : // user includes
6 : #define STAN__SERVICES__COMMAND_HPP#include <boost/integer/integer_log2.hpp>
7 : #include <rstan/rstaninc.hpp>
8 : // Code generated by Stan version 2.21.0
9 :
10 : #include <stan/model/model_header.hpp>
11 :
12 : namespace model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace {
13 :
14 : using std::istream;
15 : using std::string;
16 : using std::stringstream;
17 : using std::vector;
18 : using stan::io::dump;
19 : using stan::math::lgamma;
20 : using stan::model::prob_grad;
21 : using namespace stan::math;
22 :
23 : static int current_statement_begin__;
24 :
25 : stan::io::program_reader prog_reader__() {
26 : stan::io::program_reader reader;
27 : reader.add_event(0, 0, “start”, “model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1”);
28 : reader.add_event(3, 1, “end”, “model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1”);
29 : return reader;
30 : }
31 :
32 : class model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1
33 : : public stan::model::model_base_crtp<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1> {
34 : private:
35 : double y_mean;
36 : public:
37 : model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1(stan::io::var_context& context__,
38 : std::ostream* pstream__ = 0)
39 : : model_base_crtp(0) {
40 : ctor_body(context__, 0, pstream__);
41 : }
42 :
43 : model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1(stan::io::var_context& context__,
44 : unsigned int random_seed__,
45 : std::ostream* pstream__ = 0)
46 : : model_base_crtp(0) {
47 : ctor_body(context__, random_seed__, pstream__);
48 : }
49 :
50 : void ctor_body(stan::io::var_context& context__,
51 : unsigned int random_seed__,
52 : std::ostream* pstream__) {
53 : typedef double local_scalar_t__;
54 :
55 : boost::ecuyer1988 base_rng__ =
56 : stan::services::util::create_rng(random_seed__, 0);
57 : (void) base_rng__; // suppress unused var warning
58 :
59 : current_statement_begin__ = -1;
60 :
61 : static const char* function__ = “model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1”;
62 : (void) function__; // dummy to suppress unused var warning
63 : size_t pos__;
64 : (void) pos__; // dummy to suppress unused var warning
65 : std::vector vals_i__;
66 : std::vector vals_r__;
67 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
68 : (void) DUMMY_VAR__; // suppress unused var warning
69 :
70 : try {
71 : // initialize data block variables from context__
72 : current_statement_begin__ = 1;
73 : context__.validate_dims(“data initialization”, “y_mean”, “double”, context__.to_vec());
74 : y_mean = double(0);
75 : vals_r__ = context__.vals_r(“y_mean”);
76 : pos__ = 0;
77 : y_mean = vals_r__[pos__++];
78 :
79 :
80 : // initialize transformed data variables
81 : // execute transformed data statements
82 :
83 : // validate transformed data
84 :
85 : // validate, set parameter ranges
86 : num_params_r__ = 0U;
87 : param_ranges_i__.clear();
88 : current_statement_begin__ = 1;
89 : num_params_r__ += 1;
90 : } catch (const std::exception& e) {
91 : stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
92 : // Next line prevents compiler griping about no return
93 : throw std::runtime_error(“*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
94 : }
95 : }
96 :
97 : ~model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1() { }
98 :
99 :
100 : void transform_inits(const stan::io::var_context& context__,
101 : std::vector& params_i__,
102 : std::vector& params_r__,
103 : std::ostream
pstream__) const {
104 : typedef double local_scalar_t__;
105 : stan::io::writer writer__(params_r__, params_i__);
106 : size_t pos__;
107 : (void) pos__; // dummy call to supress warning
108 : std::vector vals_r__;
109 : std::vector vals_i__;
110 :
111 : current_statement_begin__ = 1;
112 : if (!(context__.contains_r(“y”)))
113 : stan::lang::rethrow_located(std::runtime_error(std::string(“Variable y missing”)), current_statement_begin__, prog_reader__());
114 : vals_r__ = context__.vals_r(“y”);
115 : pos__ = 0U;
116 : context__.validate_dims(“parameter initialization”, “y”, “double”, context__.to_vec());
117 : double y(0);
118 : y = vals_r__[pos__++];
119 : try {
120 : writer__.scalar_unconstrain(y);
121 : } catch (const std::exception& e) {
122 : stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable y: ") + e.what()), current_statement_begin__, prog_reader__());
123 : }
124 :
125 : params_r__ = writer__.data_r();
126 : params_i__ = writer__.data_i();
127 : }
128 :
129 : void transform_inits(const stan::io::var_context& context,
130 : Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
131 : std::ostream
pstream__) const {
132 : std::vector params_r_vec;
133 : std::vector params_i_vec;
134 : transform_inits(context, params_i_vec, params_r_vec, pstream__);
135 : params_r.resize(params_r_vec.size());
136 : for (int i = 0; i < params_r.size(); ++i)
137 : params_r(i) = params_r_vec[i];
138 : }
139 :
140 :
141 : template <bool propto__, bool jacobian__, typename T__>
142 : T__ log_prob(std::vector<T__>& params_r__,
143 : std::vector& params_i__,
144 : std::ostream
pstream__ = 0) const {
145 :
146 : typedef T__ local_scalar_t__;
147 :
148 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
149 : (void) DUMMY_VAR__; // dummy to suppress unused var warning
150 :
151 : T__ lp__(0.0);
152 : stan::math::accumulator<T__> lp_accum__;
153 : try {
154 : stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
155 :
156 : // model parameters
157 : current_statement_begin__ = 1;
158 : local_scalar_t__ y;
159 : (void) y; // dummy to suppress unused var warning
160 : if (jacobian__)
161 : y = in__.scalar_constrain(lp__);
162 : else
163 : y = in__.scalar_constrain();
164 :
165 : // model body
166 :
167 : current_statement_begin__ = 1;
168 : lp_accum__.add(normal_log<propto__>(y, y_mean, 1));
169 :
170 : } catch (const std::exception& e) {
171 : stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
172 : // Next line prevents compiler griping about no return
173 : throw std::runtime_error(”*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
174 : }
175 :
176 : lp_accum__.add(lp__);
177 : return lp_accum__.sum();
178 :
179 : } // log_prob()
180 :
181 : template <bool propto, bool jacobian, typename T_>
182 : T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
183 : std::ostream
pstream = 0) const {
184 : std::vector<T_> vec_params_r;
185 : vec_params_r.reserve(params_r.size());
186 : for (int i = 0; i < params_r.size(); ++i)
187 : vec_params_r.push_back(params_r(i));
188 : std::vector vec_params_i;
189 : return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
190 : }
191 :
192 :
193 : void get_param_names(std::vectorstd::string& names__) const {
194 : names__.resize(0);
195 : names__.push_back(“y”);
196 : }
197 :
198 :
199 : void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
200 : dimss__.resize(0);
201 : std::vector<size_t> dims__;
202 : dims__.resize(0);
203 : dimss__.push_back(dims__);
204 : }
205 :
206 : template
207 : void write_array(RNG& base_rng__,
208 : std::vector& params_r__,
209 : std::vector& params_i__,
210 : std::vector& vars__,
211 : bool include_tparams__ = true,
212 : bool include_gqs__ = true,
213 : std::ostream
pstream__ = 0) const {
214 : typedef double local_scalar_t__;
215 :
216 : vars__.resize(0);
217 : stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
218 : static const char
function__ = “model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::write_array”;
219 : (void) function__; // dummy to suppress unused var warning
220 :
221 : // read-transform, write parameters
222 : double y = in__.scalar_constrain();
223 : vars__.push_back(y);
224 :
225 : double lp__ = 0.0;
226 : (void) lp__; // dummy to suppress unused var warning
227 : stan::math::accumulator lp_accum__;
228 :
229 : local_scalar_t__ DUMMY_VAR__(std::numeric_limits::quiet_NaN());
230 : (void) DUMMY_VAR__; // suppress unused var warning
231 :
232 : if (!include_tparams__ && !include_gqs__) return;
233 :
234 : try {
235 : if (!include_gqs__ && !include_tparams__) return;
236 : if (!include_gqs__) return;
237 : } catch (const std::exception& e) {
238 : stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
239 : // Next line prevents compiler griping about no return
240 : throw std::runtime_error(“*** IF YOU SEE THIS, PLEASE REPORT A BUG ");
241 : }
242 : }
243 :
244 : template
245 : void write_array(RNG& base_rng,
246 : Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
247 : Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
248 : bool include_tparams = true,
249 : bool include_gqs = true,
250 : std::ostream
pstream = 0) const {
251 : std::vector params_r_vec(params_r.size());
252 : for (int i = 0; i < params_r.size(); ++i)
253 : params_r_vec[i] = params_r(i);
254 : std::vector vars_vec;
255 : std::vector params_i_vec;
256 : write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
257 : vars.resize(vars_vec.size());
258 : for (int i = 0; i < vars.size(); ++i)
259 : vars(i) = vars_vec[i];
260 : }
261 :
262 : std::string model_name() const {
263 : return “model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1”;
264 : }
265 :
266 :
267 : void constrained_param_names(std::vectorstd::string& param_names__,
268 : bool include_tparams__ = true,
269 : bool include_gqs__ = true) const {
270 : std::stringstream param_name_stream__;
271 : param_name_stream__.str(std::string());
272 : param_name_stream__ << “y”;
273 : param_names__.push_back(param_name_stream__.str());
274 :
275 : if (!include_gqs__ && !include_tparams__) return;
276 :
277 : if (include_tparams__) {
278 : }
279 :
280 : if (!include_gqs__) return;
281 : }
282 :
283 :
284 : void unconstrained_param_names(std::vectorstd::string& param_names__,
285 : bool include_tparams__ = true,
286 : bool include_gqs__ = true) const {
287 : std::stringstream param_name_stream__;
288 : param_name_stream__.str(std::string());
289 : param_name_stream__ << “y”;
290 : param_names__.push_back(param_name_stream__.str());
291 :
292 : if (!include_gqs__ && !include_tparams__) return;
293 :
294 : if (include_tparams__) {
295 : }
296 :
297 : if (!include_gqs__) return;
298 : }
299 :
300 : }; // model
301 :
302 : } // namespace
303 :
304 : typedef model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1 stan_model;
305 :
306 : #ifndef USING_R
307 :
308 : stan::model::model_base& new_model(
309 : stan::io::var_context& data_context,
310 : unsigned int seed,
311 : std::ostream
msg_stream) {
312 : stan_model
m = new stan_model(data_context, seed, msg_stream);
313 : return m;
314 : }
315 :
316 : #endif
317 :
318 : /
*
319 : * Define Rcpp Module to expose stan_fit’s functions to R.
320 : */
321 : RCPP_MODULE(stan_fit4model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_mod){
322 : Rcpp::class_<rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1,
323 : boost::random::ecuyer1988> >(“stan_fit4model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1”)
324 : // .constructorRcpp::List()
325 : .constructor<SEXP, SEXP, SEXP>()
326 : // .constructor<SEXP, SEXP>()
327 : .method(“call_sampler”,
328 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::call_sampler)
329 : .method(“param_names”,
330 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names)
331 : .method(“param_names_oi”,
332 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names_oi)
333 : .method(“param_fnames_oi”,
334 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_fnames_oi)
335 : .method(“param_dims”,
336 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims)
337 : .method(“param_dims_oi”,
338 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims_oi)
339 : .method(“update_param_oi”,
340 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::update_param_oi)
341 : .method(“param_oi_tidx”,
342 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_oi_tidx)
343 : .method(“grad_log_prob”,
344 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::grad_log_prob)
345 : .method(“log_prob”,
346 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::log_prob)
347 : .method(“unconstrain_pars”,
348 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrain_pars)
349 : .method(“constrain_pars”,
350 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrain_pars)
351 : .method(“num_pars_unconstrained”,
352 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::num_pars_unconstrained)
353 : .method(“unconstrained_param_names”,
354 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrained_param_names)
355 : .method(“constrained_param_names”,
356 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrained_param_names)
357 : .method(“standalone_gqs”,
358 : &rstan::stan_fit<model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1_namespace::model9ddc23367098_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::standalone_gqs)
359 : ;
360 : }
361 :
362 : // declarations
363 : extern “C” {
364 : SEXP file9ddc4e4d213b( ) ;
365 : }
366 :
367 : // definition
368 :
369 : SEXP file9ddc4e4d213b( ){
370 : return Rcpp::wrap(“73fc79f8b1915e8208c736914c86d1a1”);
371 : }
372 :
373 :
Compilation argument:
C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file9ddc4e4d213b.cpp 2> file9ddc4e4d213b.cpp.err.txt
C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.1/include” -DNDEBUG -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/Rcpp/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/BH/include" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/ranja/OneDrive/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG -D_REENTRANT -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -include stan/math/prim/mat/fun/Eigen.hpp -std=c++1y -march=core2 -O3 -march=native -mtune=native -c file9ddc4e4d213b.cpp -o file9ddc4e4d213b.o
sh: C:/Rtools/mingw_64/bin/g++: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.1/etc/x64/Makeconf:229: file9ddc4e4d213b.o] Error 127
Error in file(con, “r”) : cannot open the connection
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.1/library/base”,
“C:/Program Files/R/R-4.0.1/library/base”
2: In file(con, “r”) :
cannot open file ‘file9ddc4e4d213b.cpp.err.txt’: No such file or directory

Thanks! The key error here is:

C:/Rtools/mingw_64/bin/g++: No such file or directory

This indicates that R is looking for the old version of RTools. There are some configurations that we can check to see what’s causing this.

Can you run the following commands and post the output:

Sys.getenv("BINPREF")
Sys.getenv("PATH")
readLines("~/.R/Makevars.win")
readLines("~/Rprofile.site")

Thanks !! Please find the output

Sys.getenv(“BINPREF”)
[1] “C:/Rtools/mingw_(WIN)/bin/" Sys.getenv("PATH") [1] "C:\\Program Files\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.0.1\\bin\\x64;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath_target_300653500;C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH;C:\\Users\\ranja\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\ranja\\AppData\\Local\\atom\\bin;C:\\Users\\ranja\\AppData\\Local\\GitHubDesktop\\bin" readLines("~/.R/Makevars.win") [1] "" [2] "CXX14FLAGS=-O3 -march=native -mtune=native" [3] "CXX11FLAGS=-O3 -march=corei7 -mtune=corei7" [4] "" [5] "CXX14FLAGS=-O3 -march=native -mtune=native" [6] "CXX11FLAGS=-O3 -march=corei7 -mtune=corei7" [7] "" [8] "CXX14FLAGS=-O3 -march=native -mtune=native" [9] "CXX11FLAGS=-O3 -march=corei7 -mtune=corei7" [10] "" [11] "CXX14FLAGS=-O3 -march=corei7 -mtune=corei7" [12] "CXX14 = (BINPREF)g++ -m$(WIN) -std=c++1y”
[13] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[14] “”
[15] “CXX14FLAGS=-O3 -march=corei7 -mtune=corei7”
[16] “CXX14 = (BINPREF)g++ -m(WIN) -std=c++1y”
[17] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[18] “”
[19] “CXX14FLAGS=-O3 -march=corei7 -mtune=corei7”
[20] “CXX14 = (BINPREF)g++ -m(WIN) -std=c++1y”
[21] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[22] “”
[23] “CXX14FLAGS=-O3 -march=native -mtune=native”
[24] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[25] “”
[26] “CXX14FLAGS=-O3 -march=native -mtune=native”
[27] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[28] “”
[29] “CXX14=$(BINPREF)g++ -O2 -march=native -mtune=native”
[30] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
[31] “”
[32] “CXX14FLAGS=-O3 -march=native -mtune=native”
[33] “CXX11FLAGS=-O3 -march=corei7 -mtune=corei7”
readLines(“~/Rprofile.site”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/ranja/OneDrive/Documents/Rprofile.site’: No such file or directory

The cause of the issue is here:

> Sys.getenv("BINPREF")
[1] "C:/Rtools/mingw_$(WIN)/bin/"

This is telling R to keep looking for the old version of RTools. The first step to fixing this is to delete the file that’s setting this variable:

unlink("~/.Rprofile")

You can also get rid of the majority of the Makevars.win file, since these aren’t needed anymore with RTools4:

cat("CXX14FLAGS=-O3 -march=native -mtune=native",
    file = "~/.R/Makevars.win", 
    sep = "\n", append = FALSE)

Try running those commands, then restarting R and running the example model again:

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

If that works fine, then you should be able to run your Stan models as normal again

I ran the above suggestions and then restarted R and tries my STan models. But it is not working. Then I re installed rstan and ran the below:

unlink(“~/.Rprofile”)
Sys.getenv(“BINPREF”)
[1] “”
Sys.getenv(“PATH”)
[1] “C:\Program Files\rtools40\usr\bin;C:\Program Files\R\R-4.0.1\bin\x64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath_target_300653500;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Users\ranja\AppData\Local\Microsoft\WindowsApps;C:\Users\ranja\AppData\Local\atom\bin;C:\Users\ranja\AppData\Local\GitHubDesktop\bin”
readLines(“~/.R/Makevars.win”)
[1] “CXX14FLAGS=-O3 -march=native -mtune=native”
readLines(“~/Rprofile.site”)
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘C:/Users/ranja/OneDrive/Documents/Rprofile.site’: No such file or directory

So if you run the example model:

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

What is the line that starts with error:?

Error in file(con, “r”) : cannot open the connection
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.1/library/base”,
“C:/Program Files/R/R-4.0.1/library/base”
2: In file(con, “r”) :
cannot open file ‘file14c1030bd5233.cpp.err.txt’: No such file or directory

If you restart R and run:

library(rstan)

stancode = "data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}"
fit = stan(model_code=stancode,data=list(y_mean=0))

What happens?

stancode = “data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}”
fit = stan(model_code=stancode,data=list(y_mean=0))
I am getting 2 errors.

Error 1:

Error in file(con, “r”) : cannot open the connection
10.
file(con, “r”)
9.
readLines(errfile)
8.
compileCode(f, code, language = language, verbose = verbose)
7.
cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, …, verbose = verbose)
6.
force(code)
5.
withr::with_path(rtools_path(), code)
4.
pkgbuild::with_build_tools(cxxfunction(sig = sig, body = body, plugin = plugin, includes = includes, settings = settings, …, verbose = verbose), required = rstan_options(“required”) && !identical(Sys.getenv(“WINDOWS”), “TRUE”) && !identical(Sys.getenv(“R_PACKAGE_SOURCE”), …
3.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap("", model_name, “");”, sep = “”), includes = inc, plugin = “rstan”, save_dso = save_dso | auto_write, module_name = paste(“stan_fit4”, model_cppname, “_mod”, sep = “”), verbose = verbose)
2.
stan_model(file, model_name = model_name, model_code = model_code, stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, save_dso = save_dso, verbose = verbose)
1.
stan(model_code = stancode, data = list(y_mean = 0))

Error 2:
Error in sink(type = “output”) : invalid connection
4.
sink(type = “output”)
3.
cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap("", model_name, “");”, sep = “”), includes = inc, plugin = “rstan”, save_dso = save_dso | auto_write, module_name = paste(“stan_fit4”, model_cppname, “_mod”, sep = “”), verbose = verbose)
2.
stan_model(file, model_name = model_name, model_code = model_code, stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, save_dso = save_dso, verbose = verbose)
1.
stan(model_code = stancode, data = list(y_mean = 0))

I had a similar problem once, and for me moving the R-folder from “C:/Program Files/R” to “C:/R” solved the problem. Back then I thought that maybe the space in the path is the problem, because I remember somewhere in the error message i saw that the file path was not parsed correctly (was interrupted after the space)… Not sure whether this is also the case here…

I see you’re working out of a OneDrive folder, is the result any different if you pause the synchronisation and try again?

Thank you. But there was issue in the Onedrive and it got fixed.

1 Like

Thank you so much Andrew. Totally appreciate your time and effort for helping me. Onedrive was the issue and I fixed that. Now the stan models are working

Stay safe and healthy.

1 Like