2

I need your help because I have a problem in this code. I'm trying to translate R code to Rcpp using functions that are in the library kerdiest. Here I have a basic example of a function in R.

library(kerdiest)
hal<- function(n){
  x<- rnorm(n)
  h_AL<- ALbw(vec_data=x)
  return(h_AL)
}

Then, I made these translations for Rcpp, they send me the error **"'vec_data' was not declared in this scope*"; I don't know which one of the three is the best options, I really don't know the difference, so if you could help me with that too I will be very happy :) (I'm sorry, I'm new in Rcpp)

// [[Rcpp::export]]
NumericVector hal(int n){
  NumericVector prueba= Rcpp:: rnorm(n);
  Environment kerdiest ("package:kerdiest");
  Function F = kerdiest ["ALbw"];
  NumericVector h_AL = F(vec_data =prueba);
 // double h_AL=Rcpp::as<double> F(prueba) ;
  // return (Rcpp::wrap(h_AL));
  return h_AL;
}

// [[Rcpp::export]]
SEXP hal2(int n){
  NumericVector prueba= Rcpp:: rnorm(n);
  Environment kerdiest ("package:kerdiest");
  Function F = kerdiest ["ALbw"];
  SEXP h_AL = F(vec_data=prueba);
  // double h_AL=Rcpp::as<double> F(prueba) ;
  // return (Rcpp::wrap(h_AL));
  return h_AL;
}



// [[Rcpp::export]]
RcppExport SEXP hal3(int n){
  NumericVector prueba= rnorm(n,0,1);
  Environment kerdiest ("package:kerdiest");
  Function F = kerdiest ["ALbw"];
  RNGScope scope;
  NumericVector h_AL = F(vec_data= prueba);
  // double h_AL=Rcpp::as<double> F(prueba) ;
  // return (Rcpp::wrap(h_AL));
  return h_AL;
}

Thank you very much!

  • [this](https://stackoverflow.com/questions/38016851/call-r-functions-in-rcpp) would suggest you call the function names like `NumericVector h_AL = F(Rcpp::_["vec_data"] =prueba)` – user20650 Jun 06 '20 at 19:08
  • 1
    @NadiaLazaro Not to disappoint you but to clarify: when you can _an R function_ from C++ it is still _an R function_ so the execution speed you get is that of _an R function_. Plus possibly some minuscule overhead in calling it from C++. In short, "no free lunch theorem" still applies. This will not be fast simply because you call from C++ via Rcpp. – Dirk Eddelbuettel Jun 07 '20 at 15:09

0 Answers0