Reputation: 2164
I am implementing a statistical method in R using Rcpp and as per the repeated advice given on SO I've put all of this into a package. For my implementation, I am using a stochastic volatility routine available in the stochvol
package. The linking is done as described in Writing R Extensions 5.4.3 Linking to native routines in other packages and looks as follows (plus stochvol
in the LinkingTo
field):
#include <RcppArmadillo.h>
#include <R.h>
#include <R_ext/Rdynload.h>
void sv_update(const Rcpp::NumericVector &data, double *curpara_in, double *h_in,
double &h0, double *mixprob, int *r,
const bool centered_baseline, const double C0, const double cT,
const double Bsigma, const double a0, const double b0,
const double bmu, const double Bmu, const double B011inv,
const double B022inv, const bool Gammaprior, const bool truncnormal,
const double MHcontrol, const int MHsteps, const int parameterization,
const bool dontupdatemu, const double priorlatent0) {
static void(*fun)(const Rcpp::NumericVector &, double *, double *, double &, double *, int *, const bool, const double,
const double, const double, const double, const double, const double, const double, const double, const double,
const bool, const bool, const double, const int, const int, const bool, const double) = NULL;
if (fun==NULL) {
fun = (void(*)(const Rcpp::NumericVector &, double *, double *, double &, double *, int *, const bool, const double,
const double, const double, const double, const double, const double, const double, const double, const double,
const bool, const bool, const double, const int, const int, const bool, const double)) R_GetCCallable("stochvol", "update");
}
return fun(data, curpara_in, h_in, h0, mixprob, r, centered_baseline, C0, cT,
Bsigma, a0, b0, bmu, Bmu, B011inv, B022inv, Gammaprior, truncnormal,
MHcontrol, MHsteps, parameterization, dontupdatemu, priorlatent0);
}
The function has plenty of arguments so it doesn't look very appealing, but it's been working as it's supposed to for a while now.
My problem is that usually everything works fine, so when developing my code I wrote unit tests which ran smoothly. However, when I now try to run things in a small simulation I get:
function 'update' not provided by package 'stochvol'
Information online is sparse, but the suggestion in this thread is to reinstall the package. I just updated R to 3.5.0, did not copy any packages but reinstalled everything from scratch, but the issue remains.
Any ideas on what I can do to fix this?
Upvotes: 3
Views: 1302
Reputation: 368181
I think there are two issues here:
If you are using the mechanism you refer to of proper exporting of functions from one package, and re-use in another, then the setup may be incomplete (and hence unuseable) as I do not see stockvol
providing the actual registration of routines. Compare that with init.c in RApiSerialize which exports two functions.
Now, for C++ header-only functions we don't need that so stochvol
may be meant to be used header-only in which case you would not need this mechanism. Then again, the header you refer to has R_GetCCallable
. You may need to to contact Gregor and ask him what he intended / see if he has an example. To me, this does not match the setup I have used / am using in packages. [Edit 1: Or maybe it does, and I am out of practice with this. compileAttributes()
seems to create something similar.]
And one more aside: This mechanism is for C interfaces. So not sure if it is a good idea to put the RcppArmadillo header in there, and to use Rcpp::NumericVector
-- you may just need SEXP
only.
Edit 2: And of course the error message function 'update' not provided by package 'stochvol' is spot on of the stochvol
package did not actually run R_RegisterCCallable("stockvol", "update")
so that when you try R_GetCCallable("stochvol", "update");
you are indeed hitting a non-exported object. Hence the error.
Upvotes: 2