r 在使用 rcpp 时中止 [英] r aborted when using rcpp

查看:63
本文介绍了r 在使用 rcpp 时中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,在运行时,它中止了.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; 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);}}}返回 if1;}")Fv=7.001327setwd("D:/STAT 602")sourceCpp("子集.cpp")fct=函数(序列,数据,事实){return(anova(lm(data[seq]~as.factor(fact)))$`F 值`[1])}tempf=function(x1,x2,x3,x4){返回(c(x1,x2,x3,x4))}补偿=函数(x,y){返回(x>y)}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 

这样元素访问是通过 () 而不是 [] 完成的,以确保边界检查,1 而不是只是中止,我明白以下内容:

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().

<小时>

1 你是对的,当你使用 () 对向量进行子集化时,你会得到一个错误,当一个向量通过另一个向量进行子集化时.但是,这在访问向量的 one 元素时有效.

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,1 rather than just aborting, I see the following:

r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)

Error in r_test(a, comp, fct, tempf, combnPrim, Fv, vec, Subset) :
Evaluation error: Index out of bounds: [index=10; extent=8]..

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().


1 You are right that you'll get an error trying to use () for vector subsetting when subsetting a vector by another vector. However, this works when accessing one element of a vector.

这篇关于r 在使用 rcpp 时中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆