When I call cmdstan_model
, it can compile and save the model, but it gives the error Error in file(con, "r") : cannot open the connection
when trying to actually make it available in R. However, if the model is already compiled, then it correctly returns a CmdStanModel.
Example
The following code fails with the error Error in file(con, "r") : cannot open the connection
:
library(cmdstanr)
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file)
When running with options("cmdstanr_verbose" = TRUE)
the final part (about linking the model) is:
--- Linking model ---
g++ -std=c++1y -pthread -D_REENTRANT -Wno-sign-compare -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -Wl,-L,"/home/adam/.local/opt/cmdstan/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/home/adam/.local/opt/cmdstan/stan/lib/stan_math/lib/tbb" /tmp/RtmpZPELHp/model-463e2ffb1b99.o src/cmdstan/main.o -Wl,-L,"/home/adam/.local/opt/cmdstan/stan/lib/stan_math/lib/tbb" -Wl,-rpath,"/home/adam/.local/opt/cmdstan/stan/lib/stan_math/lib/tbb" stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_cvodes.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_idas.a stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_kinsol.a stan/lib/stan_math/lib/tbb/libtbb.so.2 -o /tmp/RtmpZPELHp/model-463e2ffb1b99
rm /tmp/RtmpZPELHp/model-463e2ffb1b99.hpp /tmp/RtmpZPELHp/model-463e2ffb1b99.o
Error in file(con, "r") : cannot open the connection
However, if I rerun the code (so the compiled model is already saved), it works.
Setup
Operating system: Ubuntu.
> library(cmdstanr)
This is cmdstanr version 0.6.1.9000
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: /home/adam/.local/opt/cmdstan
- CmdStan version: 2.33.1
> check_cmdstan_toolchain()
The C++ toolchain required for CmdStan is setup properly!
> cmdstan_path()
[1] "/home/adam/.local/opt/cmdstan"
> cmdstan_version()
[1] "2.33.1"
> tempdir()
[1] "/tmp/Rtmp6MIhLF"
The problem appeared after I updated cmdstan to version 2.33.1 (from version 2.32, I think) and cmdstanr to the newest version from GitHub (I am not sure what version I had before).