GilChrist19
GilChrist19

Reputation: 59

R Package: Call C Function Within Rcpp

I am writing an R package that contains C and Rcpp. The goal is to call the C function from R and within Rcpp, eventually performing most of the analysis in Rcpp and only returning to R for minimal tasks. My package compiles and calling my function from R works fine.

#generate some matrix. Numeric is fine too. Must have column names, no row names
myMat <- matrix(data = 1:100, nrow = 10, ncol = 10,
                dimnames = list(NULL, LETTERS[1:10]))

#This works. Put in full path, no expansion. It returns null to the console.
MinimalExample::WriteMat(mat = myMat, file = "Full_Path_Please/IWork.csv",
                         sep = "," ,eol = "\n", dec = ".", buffMB = 8L)

However, attempting the same thing in Rcpp produces a SIGSEV error. I think the problem is how I am passing arguments to the function, but I can't figure out the proper way.

#include <Rcpp.h>

using namespace Rcpp;

extern "C"{
  #include "fwrite.h"
}


//' @export
// [[Rcpp::export]]
void WriteMatCpp(String& fileName, NumericMatrix& testMat){

  Rcpp::Rcout<<"I did start!"<<std::endl;

  String patchName = fileName;
  int whichRow = 1;

  std::string newString = std::string(3 - toString(whichRow).length(), '0') 
                                      + toString(whichRow);
  patchName.replace_last(".csv", newString+".csv");


  //Set objects to pass to print function
  String comma = ",";
  String eol = "\n";
  String dot = ".";
  int buffMem = 8;

  //This is where I crash, giving a SIGSEV error
  fwriteMain(testMat, (SEXP)&patchName, (SEXP)&comma, (SEXP)&eol,
                (SEXP)&dot, (SEXP)&buffMem);

}

Here is a link to the GitHub repository with the package. https://github.com/GilChrist19/MinimalExample

Upvotes: 3

Views: 929

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

Your call from C++ to C is wrong. You can't just write (SEXP)& in front of an arbitrary data structure and hope for it to become a SEXP.

Fix

Use a line such as this to convert what you have in C++ to the SEXP your C function expects using Rcpp::wrap() on each argument:

  //This is where I crash, giving a SIGSEV error
  fwriteMain(wrap(testMat), wrap(patchName), wrap(comma), 
             wrap(eol), wrap(dot), wrap(buffMem));

Demo

edd@brad:/tmp/MinimalExample/MinEx(master)$ Rscript RunMe.R 
I did start!
edd@brad:/tmp/MinimalExample/MinEx(master)$ cat /tmp/IDoNotWork.csv 
A,B,C,D,E,F,G,H,I,J
1,11,21,31,41,51,61,71,81,91
2,12,22,32,42,52,62,72,82,92
3,13,23,33,43,53,63,73,83,93
4,14,24,34,44,54,64,74,84,94
5,15,25,35,45,55,65,75,85,95
6,16,26,36,46,56,66,76,86,96
7,17,27,37,47,57,67,77,87,97
8,18,28,38,48,58,68,78,88,98
9,19,29,39,49,59,69,79,89,99
10,20,30,40,50,60,70,80,90,100
edd@brad:/tmp/MinimalExample/MinEx(master)$ 

See https://github.com/GilChrist19/MinimalExample/tree/master/MinEx for a complete example.

Upvotes: 2

Related Questions