Issues with building a package with Rstan

I have experienced the following problem during the process of building my package. I tried to search for a solution but was unsuccessful. In order to make the error reproducible, I present a minimal package that could generate the same error. The package I built is called ‘mypkg’. First I use use_rcpp and use_rcpp_armadillo in package ‘usethis’ to build the package with the following Rcpp function

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec getEigenValues(arma::mat M) {
  return arma::eig_sym(M);
}

So far the package can be successfully documented and built. Then I continue to add features of Rstan by using rstantools::use_rstan , and follow the steps on Step by step guide for creating a package that depends on RStan, and create lm.stan and lm_stan.R as instructed. Then I built the package again but it throws out an error. Here are the results of executing .Last.error.trace

1. devtools:::document()
 2. withr::with_envvar(r_env_vars(), roxygen2::roxygenise(pkg$path,  ...
 3. base:::force(code)
 4. roxygen2::roxygenise(pkg$path, roclets, load_code = load_code)
 5. roxygen2:::load_code(base_path)
 6. pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)
 7. pkgbuild::compile_dll(path, quiet = quiet)
 8. pkgbuild:::install_min(path, dest = install_dir, components = "libs",  ...
 9. pkgbuild:::rcmd_build_tools("INSTALL", c(path, paste("--library=",  ...
 10. pkgbuild:::with_build_tools(callr::rcmd_safe(..., env = env,  ...
 11. withr::with_path(rtools_path(), code)
 12. base:::force(code)
 13. callr::rcmd_safe(..., env = env, spinner = FALSE, show = FALSE,  ...
 14. callr:::run_r(options)
 15. base:::with(options, with_envvar(env, do.call(processx::run,  ...
 16. base:::with.default(options, with_envvar(env, do.call(processx::run,  ...
 17. base:::eval(substitute(expr), data, enclos = parent.frame())
 18. base:::eval(substitute(expr), data, enclos = parent.frame())
 19. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 20. base:::force(code)
 21. base:::do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 22. (function (command = NULL, args = character(), error_on_status = TRUE,  ...
 23. throw(new_process_error(res, call = sys.call(), echo = echo,  ...
x System command 'Rcmd.exe' failed, exit status: 1, stdout + stderr (last 10 lines):
E> In file included from  C:/Users/Jasper/Documents/R/win-library/3.6/rstan/include/rstan/rstaninc.hpp:3:0,
E>                  from stanExports_lm.h:20,
E>                  from stanExports_lm.cc:5:
E> C:/Users/Jasper/Documents/R/win-library/3.6/rstan/include/rstan/stan_fit.hpp:11:28: fatal error: stan/version.hpp: No such file or directory 
E>  #include <stan/version.hpp> 
E>                            ^ 
E> compilation terminated.
E> make: *** [C:/PROGRA~1/R/R-36~1.2/etc/x64/Makeconf:213: stanExports_lm.o] Error 1 
E> ERROR: compilation failed for package 'mypkg' 
E> * removing 'C:/Users/Jasper/AppData/Local/Temp/RtmpwXHySZ/devtools_install_1d18454066b0/mypkg'

Here is my makevars file

CXX_STD = CXX11
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS)
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

Could anyone suggest what the problem is? However, when I follow the steps of the guide to create a package just with Rstan, it could be successfully built, but not when I also want to use RcppArmadillo.

Thank you very much for your kind help!!

The rstantools configure script winds up making changes to your Makevars file.

My rstantools-generated Makevars looks like this:

# Generated by rstantools.  Do not edit by hand.

STANHEADERS_SRC = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "message()" -e "cat(system.file('include', 'src', package = 'StanHeaders', mustWork = TRUE))" -e "message()" | grep "StanHeaders")

PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error

CXX_STD = CXX14

all: $(SHLIB)
                @if test -e "/usr/bin/install_name_tool" && test -e "/usr/local/clang4/lib/libc++.1.dylib" && test -e "/usr/lib/libc++.1.dylib"; then /usr/bin/install_name_tool -change /usr/local/clang4/lib/libc++.1.dylib /usr/lib/libc++.1.dylib $(SHLIB); fi

clean:
                rm -rf *.so *.o

.phony: all clean

My guess is, you need to include or merge some of those variables together, because it’s likely not finding the includes.