Question
In an R package, how do I return an Rcpp::List
to a C
function?
Example
I have a package hosted on my github page to illustrate the requirement.
It contains an R
function, which calls a C
function, which I would like to call the C++
function to retrieve the list.
R
#' @useDynLib crcpp c_ask_for_list
r_ask_for_list <- function() {
.Call(c_ask_for_list)
}
C
#include <Rinternals.h>
SEXP c_ask_for_list (){
SEXP l = PROTECT(allocVector(VECSXP, 1));
//l = rcpp_create_list(); // Call the C++ function to create the list
UNPROTECT(1);
return(l);
}
C++
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
extern "C" SEXP rcpp_create_list() {
Rcpp::List l(1);
l[0] = "foo";
return l;
}
If I uncomment the l = rcpp_create_list()
line in the C
function the program crashes.