Now integer_rng.stan is located in testStanPackage/src/stan_files
I want to expose that function. If i was not trying to do this inside a package I would simply run rstan::expose_stan_functions('integer_rng.sta')
Alas, I have no clue how to do this inside a package. My first attempt was to create the following R function testStanPackage/R/rng_integer.R :
#' rng_integer
#'
#' @param X
#'
#' @return
#' @export
#'
#' @examples
rng_integer <- function(X = 100) {
rstan::expose_stan_functions("src/stan_files/integer_rng.stan") # I know this is wrong, but i'm not sure what to do here
rn <- integer_rng(X)
return(rn)
}
Which does do what I was hoping. First, it compiles the stan code when I call it instead of when I build the package. Second it always returns the same number:
Then I need to find the resulting .cpp file and copy it to src. I was expecting that file to be in ./cache but there is nothing there. What am I doing wrong?
#include <exporter.h>
#include <RcppEigen.h>
// Code generated by Stan version 2.18.0
#include <stan/model/standalone_functions_header.hpp>
namespace user_7952f2e16b74f9a93db69131bbf741ef {
using std::istream;
using std::string;
using std::stringstream;
using std::vector;
using namespace stan::math;
typedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;
typedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;
typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;
stan::io::program_reader prog_reader__() {
stan::io::program_reader reader;
reader.add_event(0, 0, "start", "unknown file name");
reader.add_event(8, 6, "end", "unknown file name");
return reader;
}
template <class RNG>
int
integer_rng(const int& X, RNG& base_rng__, std::ostream* pstream__) {
typedef double local_scalar_t__;
typedef int fun_return_scalar_t__;
const static bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
int current_statement_begin__ = -1;
try {
{
current_statement_begin__ = 3;
int out(0);
(void) out; // dummy to suppress unused var warning
stan::math::fill(out, std::numeric_limits<int>::min());
stan::math::assign(out,categorical_rng(rep_vector(inv(X),X), base_rng__));
current_statement_begin__ = 4;
return stan::math::promote_scalar<fun_return_scalar_t__>(out);
}
} 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 ***");
}
}
struct integer_rng_functor__ {
template <class RNG>
int
operator()(const int& X, RNG& base_rng__, std::ostream* pstream__) const {
return integer_rng(X, base_rng__, pstream__);
}
};
}
// [[Rcpp::depends(rstan)]]
// [[Rcpp::export]]
int
integer_rng(const int& X, boost::ecuyer1988& base_rng__, std::ostream* pstream__ = nullptr){
return
user_7952f2e16b74f9a93db69131bbf741ef::integer_rng<boost::ecuyer1988>(X, base_rng__, pstream__);
}
If I understood the answer from the other post, I need to make some modification to this .cpp file and save it in my src folder to expose the function. Could you show me what do I need to modify?
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
integer_rng <- function(X, base_rng__, pstream__) {
.Call(`_testStanPackage_integer_rng`, X, base_rng__, pstream__)
}
However, when I try to run the function it does not work:
> devtools::load_all(".")
Loading testStanPackage
Loading required package: Rcpp
> integer_rng(X = 100)
Error in integer_rng(X = 100) :
object '_testStanPackage_integer_rng' not found
Oh I see, rcpp won’t export a template, it has to be instantiated. Your c++ has the rng as a template parameter, did you delete the instantiation? I think Stan puts it at the bottom.
You need the template of your function (still in its namespace) and the wrapper with the fully specified use of the template (outside the namespace where rcpp can see it). Currently your template is not in the namespace where the wrapper is looking.