How to enable the use of chunks in rstan packages?

As a minimum working example, I am using the project https://github.com/dmenne/teststan (version downloaded this morning). I managed to create the package just fine from that in RStudio for Windows 10. But the moment I tried to move some of the Stan code into chunks (stored in inst/chunks), I get a

Warning in file(con, “r”) :
cannot open file ‘likeeval.stan’: No such file or directory
Error in file(con, “r”) : cannot open the connection
Error : unable to load R code in package ‘teststan’
ERROR: lazy loading failed for package ‘teststan’
error. What does one need to do to enable the use of chunks?

It looked to me like e.g. for rstanarm, only filenames are given in the Stan code and no further directory information. I searched around for anything that looked like it should have something to do with what directories are used and my best guess was that it could be some of the differences in the make_cpp.R versus rstanarm. However, substituting one for the other did not solve this.

Full log:
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source --preclean teststan-master teststan-master

  • installing to library ‘C:/programs/R/R-3.4.0/library’
  • installing source package ‘teststan’ …
    $test1
    NULL

Wrote C++ file “src/test1Module.cc”
Writing NAMESPACE
Writing simple_stan.Rd
C:/Rtools/mingw_64/bin/g++ -std=gnu++11 -I"C:/programs/R/R-34~1.0/include" -DNDEBUG -I""C:/programs/R/R-34~1.0/bin/x64/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"" -I""C:/programs/R/R-34~1.0/bin/x64/Rscript" --vanilla -e "cat(system.file('include', 'boost_not_in_BH', package = 'rstan'))"" -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_NO_CXX11_RVALUE_REFERENCES -I"C:/programs/R/R-3.4.0/library/StanHeaders/include" -I"C:/programs/R/R-3.4.0/library/rstan/include" -I"C:/programs/R/R-3.4.0/library/BH/include" -I"C:/programs/R/R-3.4.0/library/Rcpp/include" -I"C:/programs/R/R-3.4.0/library/RcppEigen/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -O2 -Wall -mtune=core2 -c Modules.cpp -o Modules.o
Loading required package: Rcpp
Loading required package: rstan
Loading required package: ggplot2
Loading required package: StanHeaders
rstan (Version 2.15.1, packaged: 2017-04-19 05:03:57 UTC, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
** libs
In file included from C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/base.hpp:28:0,
from C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array.hpp:21,
from C:/programs/R/R-3.4.0/library/BH/include/boost/numeric/odeint/util/multi_array_adaption.hpp:29,
from C:/programs/R/R-3.4.0/library/BH/include/boost/numeric/odeint.hpp:61,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/prim/arr/functor/integrate_ode_rk45.hpp:13,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/prim/arr.hpp:39,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/prim/mat.hpp:298,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/mat.hpp:11,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/model/log_prob_grad.hpp:4,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/model/test_gradients.hpp:6,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/services/diagnose/diagnose.hpp:10,
from C:/programs/R/R-3.4.0/library/rstan/include/rstan/stan_fit.hpp:34,
from C:/programs/R/R-3.4.0/library/rstan/include/rstan/rstaninc.hpp:3,
from include/models.hpp:5,
from Modules.cpp:3:
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp: In static member function ‘static void boost::multi_array_concepts::detail::idgen_helper::call(Array&, const IdxGen&, Call_Type)’:
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp:42:43: warning: typedef ‘index_range’ locally defined but not used [-Wunused-local-typedefs]
typedef typename Array::index_range index_range;
^
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp:43:37: warning: typedef ‘index’ locally defined but not used [-Wunused-local-typedefs]
typedef typename Array::index index;
^
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp: In static member function ‘static void boost::multi_array_concepts::detail::idgen_helper<0ull>::call(Array&, const IdxGen&, Call_Type)’:
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp:53:43: warning: typedef ‘index_range’ locally defined but not used [-Wunused-local-typedefs]
typedef typename Array::index_range index_range;
^
C:/programs/R/R-3.4.0/library/BH/include/boost/multi_array/concept_checks.hpp:54:37: warning: typedef ‘index’ locally defined but not used [-Wunused-local-typedefs]
typedef typename Array::index index;
^
In file included from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core.hpp:42:0,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/mat.hpp:4,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/model/log_prob_grad.hpp:4,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/model/test_gradients.hpp:6,
from C:/programs/R/R-3.4.0/library/StanHeaders/include/src/stan/services/diagnose/diagnose.hpp:10,
from C:/programs/R/R-3.4.0/library/rstan/include/rstan/stan_fit.hpp:34,
from C:/programs/R/R-3.4.0/library/rstan/include/rstan/rstaninc.hpp:3,
from include/models.hpp:5,
from Modules.cpp:3:
C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp: At global scope:
C:/programs/R/R-3.4.0/library/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: ‘void stan::math::set_zero_all_adjoints()’ defined but not used [-Wunused-function]
static void set_zero_all_adjoints() {
^
C:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o teststan.dll tmp.def Modules.o -Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/programs/R/R-34~1.0/bin/x64 -lR
installing to C:/programs/R/R-3.4.0/library/teststan/libs/x64
** R
** exec
** inst
** preparing package for lazy loading
Warning in file(con, “r”) :
cannot open file ‘likeeval.stan’: No such file or directory
Error in file(con, “r”) : cannot open the connection
Error : unable to load R code in package ‘teststan’
ERROR: lazy loading failed for package ‘teststan’

  • removing ‘C:/programs/R/R-3.4.0/library/teststan’
  • restoring previous ‘C:/programs/R/R-3.4.0/library/teststan’

Exited with status 1.

It tools/make_cpp.R, there should be this statement

   cppcode <- rstan::stanc_builder(f, allow_undefined = TRUE,
                 isystem = file.path("inst", "chunks"))$cppcode

that searches in the chunks subdirectory.

Yes, that’s there (and in the tools sub-directory). In what I first used, it’s

cppcode ← rstan::stanc_builder(f,
isystem = file.path(“inst”, “chunks”))$cppcode
I had noticed the difference to rstanarm, where the “allow_undefined = TRUE” is added, which is why I tried substituting the file from rstanarm, so that that was also in. As mentioned, that did not help.

The only other place where chunks is referenced is in stanmodels.R

Thanks, that helped me find the problem! The issue was that in teststan the inst/chunks folders contained a file common_functions**.txt** instead of common_functions**.stan**. And the code looks for common_functions.stan to determine, in which directory the chunks are.