r在使用rcpp时中止
我正在编写一些代码,当运行时,它中止了。r版本是3.5.1。我想我的rcpp代码有问题,但我找不到。它只是显示R会话已中止 ################我认为这部分没有什么问题。r在使用rcpp时中止,r,rcpp,R,Rcpp,我正在编写一些代码,当运行时,它中止了。r版本是3.5.1。我想我的rcpp代码有问题,但我找不到。它只是显示R会话已中止 ################我认为这部分没有什么问题。 #include <Rcpp.h> Rcpp::LogicalVector logical_index(Rcpp::IntegerVector idx, R_xlen_t n) { bool invert = false; Rcpp::LogicalVector result(n, fals
#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())];
}
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)
#包括
Rcpp::LogicalVector逻辑索引(Rcpp::IntegerVector idx,R\u xlen\u t n){
布尔反转=假;
Rcpp::LogicalVector结果(n,false);
对于(R_xlen_t i=0;i
#
################这部分可能有问题。
#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())];
}
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)
库(Rcpp)
图书馆(gRbase)
向量=0:13
a=combnPrim(vec,4)
CPP功能(“
数值向量r_检验(数值矩阵a,函数comp,函数fct,函数
tempf,函数CombPrim,数值向量Fv,数值向量vec,函数
子集){
数值向量if1(4);
数值向量if2(2);
数值向量if3(4);
数值向量if4(4);
数字矢量序列(14);
数值矩阵b(2,45);
数值矩阵c(4,70);
对于(int i1=0;i1,由于这段代码非常复杂,我不打算寻找确切的、特定的bug。但是,我可以确认@RalfStubner是正确的,您的问题是您试图访问不存在的向量元素
如果将逻辑索引()
更改为
LogicalVector逻辑索引(IntegerVector idx,R\u xlen\u t n){
布尔反转=假;
LogicalVector结果(n,false);
对于(R_xlen_t i=0;i
因此,元素访问是通过()
而不是[]
来完成的。为了确保边界检查,1而不仅仅是中止,我看到了以下内容:
r_test(a,comp, fct, tempf, combnPrim, Fv, vec, Subset)
r_测试错误(a、comp、fct、tempf、combnPrim、Fv、vec、子集):
评估错误:索引超出范围:[索引=10;范围=8]
这意味着您试图访问不存在的向量元素。我还可以告诉您发生这种情况的基本原因:在logical\u vector()
中,当idx
的元素大于n
时,就会发生这种情况。因此,在r\u test()
中的某个点,您正在调用子集()
使用元素大于x.size()的idx
1您是对的,尝试使用()
当用另一个向量对一个向量进行子集设置时,向量子集设置。然而,这在访问向量的一个元素时有效。你能试着将你的例子缩小到一个最小的可重复的例子吗?@F.Privé嗯,当向量包含6个或更少的元素时,这是可以的,但实际情况是这样的我需要24个元素。我希望有办法处理这么多的元素。有两个想法:1)从命令行运行R。通常在退出之前会产生错误。2)将通过[]
的向量访问替换为()
。后者进行边界检查。@RalfStubner我不太明白(1)是什么意思也就是说,因为我的r_测试是一个完整的函数,你的意思是把它分开,看看哪一行会导致错误吗?在这种情况下,实际上每一行都可以单独工作。对于(2),实际上没有任何[]在我的代码中……不,我不是指单独运行这些行。我的意思是不使用像RStudio这样的GUI,这很不幸地隐藏了这样的错误消息。我不知道RGui.exe
的行为是否更合理。对于(2),您的第一个代码块包含格式vector[index]的多个位置
。这很有帮助!实际上,子集函数是从internet复制的,我只检查它是否有效,但没想到它会有其他错误。谢谢!关于向量子集与单值子集的比较,这是一个很好的理解!