问题描述
我正在编写一些代码,在运行时,它中止了.r 版本是 3.5.1.我认为我的 rcpp 代码有问题,但我找不到.它只是显示 R 会话中止.
################我认为这部分没有任何问题.#include Rcpp::LogicalVector logical_index(Rcpp::IntegerVector idx, R_xlen_t n) {布尔反转 = 假;Rcpp::LogicalVector 结果(n, false);for (R_xlen_t i = 0; i
#################这部分可能有问题.
库(Rcpp)图书馆(gRbase)vec=0:13a=combnPrim(vec,4)cpp函数("NumericVector r_test(NumericMatrix a, Function comp, Function fct,Functiontempf,函数 combnPrim,NumericVector Fv,NumericVector vec,函数子集){数字向量 if1(4);数字向量 if2(2);数字向量 if3(4);数字向量 if4(4);数字向量序列(14);数字矩阵 b(2,45);数字矩阵 c(4,70);for (int i1=0; i1y)}r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)
#
combnPrim 用于查找向量的所有组合,并输出一个矩阵,Subset 与 vector[-i] 的用法相同.单独来看,cppFunction中的所有函数都可以很好的发挥作用,但是这样放就出问题了.
由于这段代码非常复杂,我不会去寻找确切的、特定的错误.但是,我可以确认@RalfStubner 是正确的,您的问题是您正在尝试访问不存在的向量元素.
如果将 logical_index()
更改为
LogicalVector logical_index(IntegerVector idx, R_xlen_t n) {布尔反转 = 假;逻辑向量结果(n,假);for (R_xlen_t i = 0; i
这样元素访问是通过 ()
而不是 []
完成的,以确保边界检查, 而不是只是中止,我明白以下内容:
r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)
r_test(a, comp, fct, tempf, combnPrim, Fv, vec, Subset) 中的错误:
评估错误:索引越界:[索引=10;范围=8]..
这意味着您正在尝试访问不存在的向量元素.我还可以告诉您发生这种情况的基本原因:在 logical_vector()
中,当 idx
的元素大于 时n
,这会发生.因此,在 r_test()
中的某个时刻,您正在使用 idx
调用 Subset()
,其中 idx
的元素大于 x.size()
.
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.
Since this code is pretty convoluted, I'm not going to hunt for the exact, specific bug. However, I can confirm that @RalfStubner was right that your problem is that you're trying to access an element of a vector that does not exist.
If you change logical_index()
to
LogicalVector logical_index(IntegerVector idx, R_xlen_t n) {
bool invert = false;
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;
}
so that element access is done via ()
rather than []
to ensure bounds checking, rather than just aborting, I see the following:
r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)
This means you're trying to access an element of a vector that does not exist. I can also tell you the basic reason why this is happening: In logical_vector()
, when an element of idx
is greater than n
, this will occur. So, at some point in r_test()
, you are calling Subset()
with idx
that has an element greater than x.size()
.
这篇关于r 在使用 rcpp 时中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!