Installation issue

I installed rstan and tried to some examples, but nothing work. I also installed the latest version of R, Rtools, and RStudio with the hope that it can solve the issue but it does not. What I basically ran is as follows:

remove.packages(c("StanHeaders", "rstan"))
file.remove(".RData")
.rs.restartR()

pkgbuild::has_build_tools(debug = TRUE)

install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)

library("rstan")
schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

school_stan <- 'data {
  int<lower=0> J;         
  real y[J];              
  real<lower=0> sigma[J]; 
}

parameters {
  real mu;                
  real<lower=0> tau;      
  vector[J] eta;          
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        
}

model {
  target += normal_lpdf(eta | 0, 1);       
  target += normal_lpdf(y | theta, sigma); 
}'

fit <- stan(model_name = school_stan, data = schools_dat)

Then, I found the message below.

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file2b7429273907.cpp 2> file2b7429273907.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file2b7429273907.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

I tried several solutions that I found from other threads (e.g., installing older version of StanHeaders), but nothing worked.

Hi and welcome. Can you provide the versions of R, Rtools, Rstudio, and rstan. And your OS and version. Thanks!

Thanks, Ara_Winter!

Below are the version information.

R: 4.0.1
RTools: 4.0
RStudio: 1.3.959
rstan: 2.19.3
OS: Windows 10

Hi Chunhu,

What version of StanHeaders do you have? This kind of error can occur with version 2.21.

One workaround is to not load the RStan library, and just use the rstan:: qualifier instead.

So rather than:

library(rstan)
stan(model, data)

Use:

rstan::stan(model, data)

If that doesn’t work for you, can you run:

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

And post the line that starts with error:?

Thanks, andrjohns!

Yes, I have StanHeaders v2.21.

Your first suggestion does not work either.

Error in file(con, "r") : cannot open the connection
Error in sink(type = "output") : invalid connection

Your second suggestion gives me the below.

Found file = ‘C:/Users/Chunhu Jeon/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: 0x00000242059ea940> 
 $ : 'srcref' int [1:8] 9 1 9 56 1 56 9 9
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000242059ea940> 
 $ : 'srcref' int [1:8] 10 1 10 45 1 45 10 10
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000242059ea940> 
 $ : 'srcref' int [1:8] 11 1 11 46 1 46 11 11
  ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000242059ea940> 

>>>> 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:/Program Files/R/R-4.0.1/library/StanHeaders/libs/x64" -lStanHeaders
PKG_CPPFLAGS =   -I"C:/Program Files/R/R-4.0.1/library/Rcpp/include/"  -I"C:/Program Files/R/R-4.0.1/library/RcppEigen/include/"  -I"C:/Program Files/R/R-4.0.1/library/RcppEigen/include/unsupported"  -I"C:/Program Files/R/R-4.0.1/library/BH/include" -I"C:/Program Files/R/R-4.0.1/library/StanHeaders/include/src/"  -I"C:/Program Files/R/R-4.0.1/library/StanHeaders/include/"  -I"C:/Users/Chunhu Jeon/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 model217042acde1_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", "model217042acde1_73fc79f8b1915e8208c736914c86d1a1");
  28 :     reader.add_event(3, 1, "end", "model217042acde1_73fc79f8b1915e8208c736914c86d1a1");
  29 :     return reader;
  30 : }
  31 : 
  32 : class model217042acde1_73fc79f8b1915e8208c736914c86d1a1
  33 :   : public stan::model::model_base_crtp<model217042acde1_73fc79f8b1915e8208c736914c86d1a1> {
  34 : private:
  35 :         double y_mean;
  36 : public:
  37 :     model217042acde1_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 :     model217042acde1_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__ = "model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_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<int> vals_i__;
  66 :         std::vector<double> vals_r__;
  67 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::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 :     ~model217042acde1_73fc79f8b1915e8208c736914c86d1a1() { }
  98 : 
  99 : 
 100 :     void transform_inits(const stan::io::var_context& context__,
 101 :                          std::vector<int>& params_i__,
 102 :                          std::vector<double>& params_r__,
 103 :                          std::ostream* pstream__) const {
 104 :         typedef double local_scalar_t__;
 105 :         stan::io::writer<double> writer__(params_r__, params_i__);
 106 :         size_t pos__;
 107 :         (void) pos__; // dummy call to supress warning
 108 :         std::vector<double> vals_r__;
 109 :         std::vector<int> 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<double> params_r_vec;
 133 :       std::vector<int> 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<int>& 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<double>::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<int> 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::vector<std::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 <typename RNG>
 207 :     void write_array(RNG& base_rng__,
 208 :                      std::vector<double>& params_r__,
 209 :                      std::vector<int>& params_i__,
 210 :                      std::vector<double>& 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__ = "model217042acde1_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<double> lp_accum__;
 228 : 
 229 :         local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::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 <typename RNG>
 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<double> 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<double> vars_vec;
 255 :       std::vector<int> 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 "model217042acde1_73fc79f8b1915e8208c736914c86d1a1";
 264 :     }
 265 : 
 266 : 
 267 :     void constrained_param_names(std::vector<std::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::vector<std::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 model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_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_fit4model217042acde1_73fc79f8b1915e8208c736914c86d1a1_mod){
 322 :   Rcpp::class_<rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1,
 323 :                boost::random::ecuyer1988> >("stan_fit4model217042acde1_73fc79f8b1915e8208c736914c86d1a1")
 324 :     // .constructor<Rcpp::List>()
 325 :     .constructor<SEXP, SEXP, SEXP>()
 326 :     // .constructor<SEXP, SEXP>()
 327 :     .method("call_sampler",
 328 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::call_sampler)
 329 :     .method("param_names",
 330 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names)
 331 :     .method("param_names_oi",
 332 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_names_oi)
 333 :     .method("param_fnames_oi",
 334 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_fnames_oi)
 335 :     .method("param_dims",
 336 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims)
 337 :     .method("param_dims_oi",
 338 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_dims_oi)
 339 :     .method("update_param_oi",
 340 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::update_param_oi)
 341 :     .method("param_oi_tidx",
 342 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::param_oi_tidx)
 343 :     .method("grad_log_prob",
 344 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::grad_log_prob)
 345 :     .method("log_prob",
 346 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::log_prob)
 347 :     .method("unconstrain_pars",
 348 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrain_pars)
 349 :     .method("constrain_pars",
 350 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrain_pars)
 351 :     .method("num_pars_unconstrained",
 352 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::num_pars_unconstrained)
 353 :     .method("unconstrained_param_names",
 354 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::unconstrained_param_names)
 355 :     .method("constrained_param_names",
 356 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::constrained_param_names)
 357 :     .method("standalone_gqs",
 358 :             &rstan::stan_fit<model217042acde1_73fc79f8b1915e8208c736914c86d1a1_namespace::model217042acde1_73fc79f8b1915e8208c736914c86d1a1, boost::random::ecuyer1988>::standalone_gqs)
 359 :   ;
 360 : }
 361 : 
 362 : // declarations
 363 : extern "C" {
 364 : SEXP file21701d467d79( ) ;
 365 : }
 366 : 
 367 : // definition
 368 : 
 369 : SEXP file21701d467d79(  ){
 370 :  return Rcpp::wrap("73fc79f8b1915e8208c736914c86d1a1");
 371 : }
 372 : 
 373 : 
Compilation argument:
 C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file21701d467d79.cpp 2> file21701d467d79.cpp.err.txt 
"C:/rtools40/mingw64/bin/"g++ -O2 -march=native -mtune=native -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.1/include" -DNDEBUG   -I"C:/Program Files/R/R-4.0.1/library/Rcpp/include/"  -I"C:/Program Files/R/R-4.0.1/library/RcppEigen/include/"  -I"C:/Program Files/R/R-4.0.1/library/RcppEigen/include/unsupported"  -I"C:/Program Files/R/R-4.0.1/library/BH/include" -I"C:/Program Files/R/R-4.0.1/library/StanHeaders/include/src/"  -I"C:/Program Files/R/R-4.0.1/library/StanHeaders/include/"  -I"C:/Users/Chunhu Jeon/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   -include C:/Program Files/R/R-4.0.1/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I "C:/Program Files/R/R-4.0.1/library/StanHeaders/include" -I "C:/Program Files/R/R-4.0.1/library/RcppEigen/include"   -O3 -march=native -mtune=native -c file21701d467d79.cpp -o file21701d467d79.o
g++.exe: error: Files/R/R-4.0.1/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp: No such file or directory
make: *** [C:/PROGRA~1/R/R-40~1.1/etc/x64/Makeconf:229: file21701d467d79.o] Error 1
Error in file(con, "r") : cannot open the connection

Oh sorry, I should have been clearer. You need to restart R, and then make sure that you don’t load RStan (i.e. do not run library(rstan)). Just use rstan::stan(model, data) to run your model.

Following your suggestion, I ran the code below.

file.remove(".RData")
.rs.restartR()

school_stan <- 'data {
  int<lower=0> J;         
  real y[J];              
  real<lower=0> sigma[J]; 
}

parameters {
  real mu;                
  real<lower=0> tau;      
  vector[J] eta;          
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        
}

model {
  target += normal_lpdf(eta | 0, 1);       
  target += normal_lpdf(y | theta, sigma); 
}'

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

fit <- rstan::stan(model_name = school_stan, data = schools_dat)

Then, I got the error message below.

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.1/bin/x64/R CMD SHLIB file325853364b5b.cpp 2> file325853364b5b.cpp.err.txt' had status 1
2: In file(con, "r") :
  cannot open file 'file325853364b5b.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Two things here, the .rs.restartR() command is not sufficient, you need to physically close R/RStudio and then re-open and run the code.

Also, you’ve used stan(model_name=school_stan...)

The model_name option is used to specify the name of the model, not provide the model code. If you instead use:

fit <- rstan::stan(model_code = school_stan, data = schools_dat)

That should work

1 Like

Yes, finally worked!
Thanks so much!!

2 Likes