3

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 ??
}
2
  • 2
    This is essentially (maybe not quite) a duplicate of stackoverflow.com/q/21225662/1968 Commented Aug 1, 2016 at 16:55
  • Close enough to a dupe for me. Known feature, well documented, no point in cluttering SO with repeats. Commented Aug 1, 2016 at 20:24

1 Answer 1

10

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(....);

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.