I am writing some code, and when running, it aborted. The r version is 3.5.1. I think there is something wrong with my rcpp code, but I can't find it. It just shows R session aborted.
################I don't think there is anything wrong in this part.#include <Rcpp.h>
Rcpp::LogicalVector logical_index(Rcpp::IntegerVector idx, R_xlen_t n) {
bool invert = false;
Rcpp::LogicalVector result(n, false);
for (R_xlen_t i = 0; i < idx.size(); i++) {
if (!invert && idx[i] < 0) invert = true;
result[std::abs(idx[i])] = true;
}
if (!invert) return result;
return !result;
}
// [[Rcpp::export]]
Rcpp::NumericVector
Subset(Rcpp::NumericVector x, Rcpp::IntegerVector idx) {
return x[logical_index(idx, x.size())];
}
#
################There maybe something wrong in this part.
library(Rcpp)
library(gRbase)
vec=0:13
a=combnPrim(vec,4)
cppFunction("
NumericVector r_test(NumericMatrix a, Function comp, Function fct,Function
tempf, Function combnPrim, NumericVector Fv, NumericVector vec, Function
Subset){
NumericVector if1(4);
NumericVector if2(2);
NumericVector if3(4);
NumericVector if4(4);
NumericVector seq(14);
NumericMatrix b(2,45);
NumericMatrix c(4,70);
for (int i1=0; i1<=a.ncol()-1; i1++){
if1=a (_, i1);
b=combnPrim(Subset(vec,-(if1)),2);
for (int i2=0; i2<=b.ncol()-1; i2++){
if2=b (_, i2);
c=combnPrim(Subset(Subset(vec,-(if1)),-(if2)),4);
for (int i3=0; i3<=c.ncol()-1; i3++){
if3=c (_, i3);
if4=Subset(Subset(Subset(vec,-(if1)),-(if2)),-(if3));
seq=tempf(if1,if2,if3,if4);
}}}return if1;}")
Fv=7.001327
setwd("D:/STAT 602")
sourceCpp("Subset.cpp")
fct=function(seq,data,fact){
return(anova(lm(data[seq]~as.factor(fact)))$`F value`[1])
}
tempf=function(x1,x2,x3,x4){
return(c(x1,x2,x3,x4))
}
comp=function(x,y){
return(x>y)
}
r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)
#
combnPrim is used to find all combination of a vector, and it outputs a matrix, and Subset is the same use as vector[-i]. Individually, all function can act well in cppFunction, but when putting them like this, things go wrong.