Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling function from package in rcpp code [duplicate]

Tags:

r

rcpp

I have a package X in R. The package has a function foo(). I want to call the function foo() in a cpp file (using Rcpp). Is it possible?

#include <Rcpp.h>

void function01() {

    // call foo() from package X ??
}
like image 908
torm Avatar asked Dec 04 '25 21:12

torm


1 Answers

This is sort of a duplicate. Though, the majority of cases do not involve calling from a user defined package.

As a result, the mold to use is:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void function01(){

  // Obtain environment containing function
  Rcpp::Environment package_env("package:package_name_here"); 

  // Make function callable from C++
  Rcpp::Function rfunction = package_env["function_name"];    

  // Call the function and receive output (might not be list)
  Rcpp::List test_out = rfunction(....);

}
like image 196
coatless Avatar answered Dec 06 '25 11:12

coatless



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!