Dealing with R 4.0

Hi,
I’m using R 3.6.0 and it is hard for me to uninstall R and re-install a newer version. I tried to install StanHeaders 2.19.0 and rstan 2.19.3 because my last laptop has this combination and no issues. But I can’t find rstan 2.19.3 available.

Try

remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan")

I’m currently having this same issue, were you ever able to resolve this?

I’m new to stan and also experiencing this issue, with the caveat that using the rstan:: workaround functions okay if I’m just using stan, but it doesn’t work when I try to use it with Richard McElreath’s rethinking package (I assume because it loads rstan).

I tried

remotes::install_github("stan-dev/rstan", ref = "develop", subdir = "rstan/rstan")

but it failed with status 128. I’ve tried just about everything else in this thread, plus uninstalling everything R-related and doing a completely new install of R 4.0.2, RStudio 1.3.1056, and Rtools40, and then following the current instructions on the rstan github’s getting started page. I’m still getting this error message:

Error in file(con, "r") : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  '-E' not found
2: In system(cmd, intern = !verbose) :
  running command 'C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file1d6c52b484b.cpp 2> file1d6c52b484b.cpp.err.txt' had status 1
3: In file(con, "r") :
  cannot open file 'file1d6c52b484b.cpp.err.txt': No such file or directory
Error in sink(type = "output") : invalid connection

Both with and without Makevars.win present. Not sure what else I should try, but any help would be greatly appreciated!

Edit: Here’s my version info:
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 (build 18363)
RStudio version 1.3.1056
Rtools40
rstan_2.21.2
StanHeaders_2.21.0-5

You could try a

install.packages("ftp://cran.r-project.org/incoming/pretest/StanHeaders_2.21.0-6.tar.gz", repos = NULL, type = "source")

But if that doesn’t work, the error message from

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

near the line of output that says “Compilation argument” is the most useful to us for figuring out which solution to recommend to which problem.

That did the trick—the code now works! Thank you! I do still get a warning message, in case that matters:

Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
'-E' not found

Is that something that can be safely ignored for now?

Edit: On second thought, I’m not sure whether this error message is from rstan or rethinking. I’ll leave it here just in case, though.

Hi @bgoodri and others,

I’ve been a heavy user of Stan for years without problems, but since upgrading to R 4.0 and Rtools 40 last week I’ve been have this problem and have not been able to run any model… I’ve read through this and other threads and have basically tried all the suggestions without success.

For instance the rstan::stan(...) workaround does not work for me, neither does any of the changes suggested to the makewars.win file. As far as I can tell the error message is always the same:

Compilation argument:
C:/PROGRA~1/R/R-40~1.2/bin/x64/R CMD SHLIB file1b10bff1325.cpp 2> file1b10bff1325.cpp.err.txt
Error in file(con, “r”) : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found
2: In file(con, “r”) :
cannot open file ‘file1b10bff1325.cpp.err.txt’: No such file or directory

My version info is much like @michael1, with the exception that I have updated StanHeaders to v. 2.21.0-6 as suggested.

Version info:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
RStudio: 1.3.1056
Rtools40
rstan_2.21.2
StanHeaders_2.21.0-6

I’m getting the same error when trying using the inline package:

fx <- inline::cxxfunction( signature(x = “integer”, y = “numeric” ) , ‘return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;’ )
Error in file(con, “r”) : cannot open the connection
In addition: Warning message:
In file(con, “r”) :
cannot open file ‘file1b104434265c.cpp.err.txt’: No such file or directory

Any suggestions would be much appreciated! I’m basically stuck.

Hi,

sorry to hear you are having problems. Lets see if we can debug this one.

Can you try to compile again and when it fails, run tempdir() in the R session. Then go in the folder that will be printed(open it in the File Explorer on Windows), sort the files by date and copy the latest file (it should have just been created) that ends on .warn or something like that?

Here it is. Note, that it doesn’t end with .warn but .cpp:

// includes from the plugin
// [[Rcpp::plugins(cpp14)]]

// user includes
#include <Rcpp.h>
#include <rstan/io/rlist_ref_var_context.hpp>
#include <rstan/io/r_ostream.hpp>
#include <rstan/stan_args.hpp>
#include <boost/integer/integer_log2.hpp>
// Code generated by Stan version 2.21.0

#include <stan/model/model_header.hpp>

namespace model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1_namespace {

using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using stan::io::dump;
using stan::math::lgamma;
using stan::model::prob_grad;
using namespace stan::math;

static int current_statement_begin__;

stan::io::program_reader prog_reader__() {
stan::io::program_reader reader;
reader.add_event(0, 0, “start”, “model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1”);
reader.add_event(3, 1, “end”, “model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1”);
return reader;
}

class model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1
: public stan::model::model_base_crtp<model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1> {
private:
double y_mean;
public:
model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1(rstan::io::rlist_ref_var_context& context__,
std::ostream* pstream__ = 0)
: model_base_crtp(0) {
ctor_body(context__, 0, pstream__);
}

model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1(stan::io::var_context& context__,
    unsigned int random_seed__,
    std::ostream* pstream__ = 0)
    : model_base_crtp(0) {
    ctor_body(context__, random_seed__, pstream__);
}

void ctor_body(stan::io::var_context& context__,
               unsigned int random_seed__,
               std::ostream* pstream__) {
    typedef double local_scalar_t__;

    boost::ecuyer1988 base_rng__ =
      stan::services::util::create_rng(random_seed__, 0);
    (void) base_rng__;  // suppress unused var warning

    current_statement_begin__ = -1;

    static const char* function__ = "model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1_namespace::model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1";
    (void) function__;  // dummy to suppress unused var warning
    size_t pos__;
    (void) pos__;  // dummy to suppress unused var warning
    std::vector<int> vals_i__;
    std::vector<double> vals_r__;
    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning

    try {
        // initialize data block variables from context__
        current_statement_begin__ = 1;
        context__.validate_dims("data initialization", "y_mean", "double", context__.to_vec());
        y_mean = double(0);
        vals_r__ = context__.vals_r("y_mean");
        pos__ = 0;
        y_mean = vals_r__[pos__++];


        // initialize transformed data variables
        // execute transformed data statements

        // validate transformed data

        // validate, set parameter ranges
        num_params_r__ = 0U;
        param_ranges_i__.clear();
        current_statement_begin__ = 1;
        num_params_r__ += 1;
    } catch (const std::exception& e) {
        stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
        // Next line prevents compiler griping about no return
        throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
    }
}

~model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1() { }


void transform_inits(const stan::io::var_context& context__,
                     std::vector<int>& params_i__,
                     std::vector<double>& params_r__,
                     std::ostream* pstream__) const {
    typedef double local_scalar_t__;
    stan::io::writer<double> writer__(params_r__, params_i__);
    size_t pos__;
    (void) pos__; // dummy call to supress warning
    std::vector<double> vals_r__;
    std::vector<int> vals_i__;

    current_statement_begin__ = 1;
    if (!(context__.contains_r("y")))
        stan::lang::rethrow_located(std::runtime_error(std::string("Variable y missing")), current_statement_begin__, prog_reader__());
    vals_r__ = context__.vals_r("y");
    pos__ = 0U;
    context__.validate_dims("parameter initialization", "y", "double", context__.to_vec());
    double y(0);
    y = vals_r__[pos__++];
    try {
        writer__.scalar_unconstrain(y);
    } catch (const std::exception& e) {
        stan::lang::rethrow_located(std::runtime_error(std::string("Error transforming variable y: ") + e.what()), current_statement_begin__, prog_reader__());
    }

    params_r__ = writer__.data_r();
    params_i__ = writer__.data_i();
}

void transform_inits(const stan::io::var_context& context,
                     Eigen::Matrix<double, Eigen::Dynamic, 1>& params_r,
                     std::ostream* pstream__) const {
  std::vector<double> params_r_vec;
  std::vector<int> params_i_vec;
  transform_inits(context, params_i_vec, params_r_vec, pstream__);
  params_r.resize(params_r_vec.size());
  for (int i = 0; i < params_r.size(); ++i)
    params_r(i) = params_r_vec[i];
}


template <bool propto__, bool jacobian__, typename T__>
T__ log_prob(std::vector<T__>& params_r__,
             std::vector<int>& params_i__,
             std::ostream* pstream__ = 0) const {

    typedef T__ local_scalar_t__;

    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // dummy to suppress unused var warning

    T__ lp__(0.0);
    stan::math::accumulator<T__> lp_accum__;
    try {
        stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);

        // model parameters
        current_statement_begin__ = 1;
        local_scalar_t__ y;
        (void) y;  // dummy to suppress unused var warning
        if (jacobian__)
            y = in__.scalar_constrain(lp__);
        else
            y = in__.scalar_constrain();

        // model body

        current_statement_begin__ = 1;
        lp_accum__.add(normal_log<propto__>(y, y_mean, 1));

    } catch (const std::exception& e) {
        stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
        // Next line prevents compiler griping about no return
        throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
    }

    lp_accum__.add(lp__);
    return lp_accum__.sum();

} // log_prob()

template <bool propto, bool jacobian, typename T_>
T_ log_prob(Eigen::Matrix<T_,Eigen::Dynamic,1>& params_r,
           std::ostream* pstream = 0) const {
  std::vector<T_> vec_params_r;
  vec_params_r.reserve(params_r.size());
  for (int i = 0; i < params_r.size(); ++i)
    vec_params_r.push_back(params_r(i));
  std::vector<int> vec_params_i;
  return log_prob<propto,jacobian,T_>(vec_params_r, vec_params_i, pstream);
}


void get_param_names(std::vector<std::string>& names__) const {
    names__.resize(0);
    names__.push_back("y");
}


void get_dims(std::vector<std::vector<size_t> >& dimss__) const {
    dimss__.resize(0);
    std::vector<size_t> dims__;
    dims__.resize(0);
    dimss__.push_back(dims__);
}

template <typename RNG>
void write_array(RNG& base_rng__,
                 std::vector<double>& params_r__,
                 std::vector<int>& params_i__,
                 std::vector<double>& vars__,
                 bool include_tparams__ = true,
                 bool include_gqs__ = true,
                 std::ostream* pstream__ = 0) const {
    typedef double local_scalar_t__;

    vars__.resize(0);
    stan::io::reader<local_scalar_t__> in__(params_r__, params_i__);
    static const char* function__ = "model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1_namespace::write_array";
    (void) function__;  // dummy to suppress unused var warning

    // read-transform, write parameters
    double y = in__.scalar_constrain();
    vars__.push_back(y);

    double lp__ = 0.0;
    (void) lp__;  // dummy to suppress unused var warning
    stan::math::accumulator<double> lp_accum__;

    local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
    (void) DUMMY_VAR__;  // suppress unused var warning

    if (!include_tparams__ && !include_gqs__) return;

    try {
        if (!include_gqs__ && !include_tparams__) return;
        if (!include_gqs__) return;
    } catch (const std::exception& e) {
        stan::lang::rethrow_located(e, current_statement_begin__, prog_reader__());
        // Next line prevents compiler griping about no return
        throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
    }
}

template <typename RNG>
void write_array(RNG& base_rng,
                 Eigen::Matrix<double,Eigen::Dynamic,1>& params_r,
                 Eigen::Matrix<double,Eigen::Dynamic,1>& vars,
                 bool include_tparams = true,
                 bool include_gqs = true,
                 std::ostream* pstream = 0) const {
  std::vector<double> params_r_vec(params_r.size());
  for (int i = 0; i < params_r.size(); ++i)
    params_r_vec[i] = params_r(i);
  std::vector<double> vars_vec;
  std::vector<int> params_i_vec;
  write_array(base_rng, params_r_vec, params_i_vec, vars_vec, include_tparams, include_gqs, pstream);
  vars.resize(vars_vec.size());
  for (int i = 0; i < vars.size(); ++i)
    vars(i) = vars_vec[i];
}

std::string model_name() const {
    return "model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1";
}


void constrained_param_names(std::vector<std::string>& param_names__,
                             bool include_tparams__ = true,
                             bool include_gqs__ = true) const {
    std::stringstream param_name_stream__;
    param_name_stream__.str(std::string());
    param_name_stream__ << "y";
    param_names__.push_back(param_name_stream__.str());

    if (!include_gqs__ && !include_tparams__) return;

    if (include_tparams__) {
    }

    if (!include_gqs__) return;
}


void unconstrained_param_names(std::vector<std::string>& param_names__,
                               bool include_tparams__ = true,
                               bool include_gqs__ = true) const {
    std::stringstream param_name_stream__;
    param_name_stream__.str(std::string());
    param_name_stream__ << "y";
    param_names__.push_back(param_name_stream__.str());

    if (!include_gqs__ && !include_tparams__) return;

    if (include_tparams__) {
    }

    if (!include_gqs__) return;
}

}; // model

} // namespace

typedef model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1_namespace::model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1 stan_model;

#ifndef USING_R

stan::model::model_base& new_model(
stan::io::var_context& data_context,
unsigned int seed,
std::ostream* msg_stream) {
stan_model* m = new stan_model(data_context, seed, msg_stream);
return *m;
}

#endif

#include <rstan_next/stan_fit.hpp>

struct stan_model_holder {
stan_model_holder(rstan::io::rlist_ref_var_context rcontext,
unsigned int random_seed)
: rcontext_(rcontext), random_seed_(random_seed)
{
}

//stan::math::ChainableStack ad_stack;
rstan::io::rlist_ref_var_context rcontext_;
unsigned int random_seed_;
};

Rcpp::XPtrstan::model::model_base model_ptr(stan_model_holder* smh) {
Rcpp::XPtrstan::model::model_base model_instance(new stan_model(smh->rcontext_, smh->random_seed_), true);
return model_instance;
}

Rcpp::XPtrrstan::stan_fit_base fit_ptr(stan_model_holder* smh) {
return Rcpp::XPtrrstan::stan_fit_base(new rstan::stan_fit(model_ptr(smh), smh->random_seed_), true);
}

std::string model_name(stan_model_holder* smh) {
return model_ptr(smh).get()->model_name();
}

RCPP_MODULE(stan_fit4model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1_mod){
Rcpp::class_<stan_model_holder>(“stan_fit4model1b10644c40b2_73fc79f8b1915e8208c736914c86d1a1”)
.constructor<rstan::io::rlist_ref_var_context, unsigned int>()
.method(“model_ptr”, &model_ptr)
.method(“fit_ptr”, &fit_ptr)
.method(“model_name”, &model_name)
;
}

// declarations
extern “C” {
SEXP file1b106f0b71d4( ) ;
}

// definition

SEXP file1b106f0b71d4( ){
return Rcpp::wrap(“73fc79f8b1915e8208c736914c86d1a1”);
}

Was there any other files generated at this time?

And if possible, can you post the output of Sys.getenv(“PATH”)

No, that’s the only one… Its name also maches the second warning:

In file(con, “r”) :
cannot open file ‘file1b106f0b71d4.cpp.err.txt’: No such file or directory

Except for the .err.txt ending

Edit: And here’s Sys.getenv:

Sys.getenv(“PATH”)
[1] “C:\Program Files\R\R-4.0.2\bin\x64;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Java\jre1.8.0_211\bin;C:\Program Files (x86)\Java\jre1.8.0_211\bin;C:\Program Files\Microsoft SQL Server\130\Tools\Binn;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\PuTTY;C:\Program Files\Git\cmd;C:\rtools40\usr\bin;C:\rtools40\mingw64\bin;C:\Users\REVSB00E\AppData\Local\Microsoft\WindowsApps;C:\Users\REVSB00E\AppData\Local\Programs\MiKTeX\miktex\bin\x64”

Sorry, one more debug. What does

.libPaths()

return?

.libPaths()
[1] “C:/Users/REVSB00E/Documents/R/win-library/4.0” “C:/Program Files/R/R-4.0.2/library”

Ok, so I am guessing Rstan in installed in the former which should work fine. Does the path to tempdir() have any spaces?

You can try running R CMD SHLIB C:/full_path_to_this_temp_folder/file1b10bff1325.cpp in some powershell to see if that works.

My hunch here is that either the path to R that has spaces in and it causes problems (this does seem unlikely) or that there is a space in some other path.

Yes it is. No spaces in the RStan package folder or the tempdir() folder. Neither in the Rtools folder or any other R-related path that I can think of (except for R itself, residing in the Program Files folder…)

$ R CMD SHLIB C:/Users/REVSB00E/AppData/Local/Temp/RtmpmaxLVx/file1b10bff1325.cpp
“C:/rtools40/mingw64/bin/“g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-40~1.2/include” -DNDEBUG -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/Rcpp/include/” -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/RcppEigen/include/" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/RcppEigen/include/unsupported" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/BH/include" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/StanHeaders/include/src/" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/StanHeaders/include/" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/RcppParallel/include/" -I"C:/Users/REVSB00E/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include “C:/Users/REVSB00E/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp” -std=c++1y -O2 -Wall -mfpmath=sse -msse2 -mstackrealign -c C:/Users/REVSB00E/AppData/Local/Temp/RtmpmaxLVx/file1b10bff1325.cpp -o C:/Users/REVSB00E/AppData/Local/Temp/RtmpmaxLVx/file1b10bff1325.o
0 [main] sh (14380) C:\Program Files\Git\usr\bin\sh.exe: *** fatal error - cygheap base mismatch detected - 0x180316408/0x180348408.
This problem is probably due to using incompatible versions of the cygwin DLL.
Search for cygwin1.dll using the Windows Start->Find/Search facility
and delete all but the most recent version. The most recent version should
reside in x:\cygwin\bin, where ‘x’ is the drive on which you have
installed the cygwin distribution. Rebooting is also suggested if you
are unable to find another cygwin DLL.
make: *** [C:/PROGRA~1/R/R-40~1.2/etc/x64/Makeconf:229: C:/Users/REVSB00E/AppData/Local/Temp/RtmpmaxLVx/file1b10bff1325.o] Error 127

1 Like

Ok, this is really helpful!

The first thing you should do is edit you PATH variable so that

C:\rtools40\usr\bin
and
C:\rtools40\mingw64\bin

are listed before

C:\Program Files\Git\cmd

Maybe just put them in first place.

Are you comfortable editing PATH? Let me know if you need guidance.

There’s actually no need to edit the PATH variable. Part of the RTools4 install instructions include a command that temporarily sets the adds the rtools40 directory to the start of the PATH variable for each session:

Just need to run

writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")

and restart R.

Although there will still be issues for cmdstan use, this will fix things for use via R (both RStan and cmdstanr, I think)

1 Like

@esben just run the line @andrjohns suggested, that is far easier yes!

Thanks Andrew!

1 Like

I tried both, still the same result:

Error in file(con, “r”) : cannot open the connection
In addition: Warning messages:
1: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
‘C:/rtools40/usr/mingw_/bin/g++’ not found
2: In file(con, “r”) :
cannot open file ‘file450474a25b88.cpp.err.txt’: No such file or directory

That is unfortunate. Can you post the outputs of

Sys.which("make")

and

Sys.which("g++")