从 rcpp 代码中的包调用函数

calling function from package in rcpp code

我在 R 中有一个包 X。该包有一个函数 foo()。我想在 cpp 文件中调用函数 foo()(使用 Rcpp)。可能吗?

#include <Rcpp.h>

void function01() {

    // call foo() from package X ??
}

这有点重复。虽然,大多数情况不涉及从用户定义的包调用。

因此,要使用的模具是:

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

}