Reputation: 499
I wrote the following code in Rcpp
//#include <Rcpp.h>
#include <RcppArmadilloExtensions/sample.h>
#include <random>
#include <iostream>
using namespace Rcpp;
// [[Rcpp::export]]
arma::vec SimulBetaBin(int K, arma::vec N){
arma::vec D;
Environment pkg = Environment::namespace_env("extraDistr");
Function f = pkg["rbbinom"];
for(int i=0; i<K; ++i){
D[i] = f(1, N[i], 1, 1);
}
return D;
}
The purpose of this function is to simulate a Beta Binomial Distribution. However, when I compile the code in R, I get the following error
error: cannot convert 'SEXP' {aka 'SEXPREC*'} to 'double' in assignment
D[i] = f( N[i], 1, 1);
^
I tried to understand what a SEXPREC*
is but I got even more confused
What R users think of as variables or objects are symbols which are bound to a value. The value can be thought of as either a SEXP (a pointer), or the structure it points to, a SEXPREC
, what do they mean by that ??
Because I think I have to understand that first in order to solve the error.
In case
Upvotes: 1
Views: 254
Reputation: 368499
You get a SEXP
back from calling a Rcpp::Function()
object, so you need to cast it. A modified version (also simplifying headers down to what you actually used and need) of your function follows, this one compiles for me.
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::vec SimulBetaBin(int K, arma::vec N) {
arma::vec D;
Rcpp::Environment pkg = Rcpp::Environment::namespace_env("extraDistr");
Rcpp::Function f = pkg["rbbinom"];
for (int i=0; i<K; ++i) {
SEXP val = f(1, N[i], 1, 1);
D[i] = Rcpp::as<double>(val);
}
return D;
}
Edit: Removed another unused header.
Upvotes: 4