我正在编写一个包含C和Rcpp的R包。目标是从R和Rcpp中调用C函数,最终在Rcpp中执行大部分分析,并且仅返回R以执行最少的任务。我的包编译并从R调用我的函数工作正常。
#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)
但是,在Rcpp中尝试相同的操作会产生SIGSEV错误。我认为问题是我如何将参数传递给函数,但我无法弄清楚正确的方法。
#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);
}
这是包含GitHub存储库的链接。 https://github.com/GilChrist19/MinimalExample
答案 0 :(得分:2)
你从C ++到C的电话是错误的。您不能在任意数据结构前面写(SEXP)&
,并希望它成为SEXP
。
使用这样的行将C ++中的内容转换为C函数期望SEXP
在每个参数上使用Rcpp::wrap()
:
//This is where I crash, giving a SIGSEV error
fwriteMain(wrap(testMat), wrap(patchName), wrap(comma),
wrap(eol), wrap(dot), wrap(buffMem));
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)$
有关完整示例,请参阅https://github.com/GilChrist19/MinimalExample/tree/master/MinEx。