Devtools::build(binary = TRUE) problem with a package using rstan

Hello

I am trying to develop a package for a model using Stan that can be distributed to users as a binary on Windows to avoid having to install Rtools. The package is here:

As well as the rstan sampler and optimizer, it has 3 Rcpp functions which are also installed.

This is a work in progress, but the package works fine if I install using remotes and compile locally using Rtools. However, if I build the package first as a binary using

devtools::build(binary = TRUE)

and install from there the rstan optimizer/sampler gives me an error:

“Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
Exception: variable does not exist; processing stage=data initialization; variable name=Len; base type=double (in ‘string’, line 220, column 2 to column 18)”

The variable “Len” is in the data list, so it’s something else that is going wrong.

When running the devtools::build(), a file “init.c” is created which references my Rcpp functions, but not the stan functions. Similarly the stan references are removed from the RcppExports.cpp file.

If I add the references back in by hand, the package checks out fine. So for init.c the corrected file is:

/* Section generated by pkgbuild, do not edit */
/* .Call calls */
extern SEXP _fishblicc_CNinInterval(SEXP, SEXP);
extern SEXP _fishblicc_Csel_dsnormal(SEXP, SEXP, SEXP, SEXP);
extern SEXP _fishblicc_CSurvival_Est(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern RcppExport SEXP _rcpp_module_boot_stan_fit4BLICC_mod(); /* MANUALLY ADDED */

static const R_CallMethodDef CallEntries[] = {
    {"_fishblicc_CNinInterval",  (DL_FUNC) &_fishblicc_CNinInterval,  2},
    {"_fishblicc_Csel_dsnormal", (DL_FUNC) &_fishblicc_Csel_dsnormal, 4},
    {"_fishblicc_CSurvival_Est", (DL_FUNC) &_fishblicc_CSurvival_Est, 6},
    {"_rcpp_module_boot_stan_fit4BLICC_mod", (DL_FUNC) &_rcpp_module_boot_stan_fit4BLICC_mod, 0},      /* MANUALLY ADDED */
    {NULL, NULL, 0}
};
/* End section generated by pkgbuild */

When I rerun the devtools::build() it, of course, overwrites this, so it’s not really a solution.

When running devtools:check() the only note I can’t get rid of is “GNU make is a SystemRequirements.” which I don’t think matters. There are no other problems.

rstantools version 2.2.0
rstan version 2.26.13
devtools version 2.4.4
R version 4.2.1
Windows version 10

DESCRIPTION file part:

Imports: 
    dplyr,
    ggplot2,
    methods,
    posterior,
    purrr,
    Rcpp (>= 0.12.0),
    rstan (>= 2.18.1),
    statmod,
    stats,
    tibble,
    tidyr
Suggests: 
    bayesplot,
    testthat (>= 3.0.0)
LinkingTo: 
    BH (>= 1.66.0),
    Rcpp (>= 0.12.0),
    RcppEigen (>= 0.3.3.3.0),
    RcppParallel (>= 5.0.1),
    rstan (>= 2.18.1),
    StanHeaders (>= 2.18.0)
Biarch: true
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
SystemRequirements: GNU make

Thanks for any help or pointers to documentation.

Paul