gpt4 book ai didi

c++ - 读取构造函数的 R 列表会引发错误 : expecting a single value

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:06 25 4
gpt4 key购买 nike

我正在使用 Rcpp/RcppArmadillo 编译一个包,我收到一个错误,我认为它来自 Rcpp as.h header 。我编写了一个构造函数,它从 R 中获取一个列表并创建适当的类。调用构造函数的函数和实际的构造函数如下所示。我正在尝试在列表和数据框中遵循此处显示的代码 (http://adv-r.had.co.nz/Rcpp.html)。

#include "varInf.h"

//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
void varInfInput(Rcpp::List model_r)
{
mm_model model = mm_model(model_r);
varInfC(model);
}

这是包含在 varInf.h 中的构造函数 mm_model.h

using namespace Rcpp ;
using namespace arma;
class mm_model
{
public:
int T ;
int J ;
IntegerVector Rj ;
int maxR ;
IntegerVector Nijr ;
int maxN ;
int K ;
IntegerVector Vj ;
int maxV ;
NumericVector alpha;
NumericVector theta;
IntegerVector Z ;
NumericVector lambda ;
NumericVector phi ;
NumericVector delta ;
NumericVector obs ;
int dist;

mm_model(List model)
{
T = (int) model[0];
J = (int) model[1];
Rj = as<IntegerVector>(model[2]);
maxR = max(Rj);
Nijr = as<IntegerVector>(model[3]);
maxN = max(Nijr);
K = (int) model[4];
Vj = as<IntegerVector>(model[5]);
maxV = max(Vj);
alpha = as<NumericVector>(model[6]);
theta = as<NumericVector>(model[7]);
Z = as<IntegerVector>(model[8]);
lambda = as<NumericVector>(model[9]);
phi = as<NumericVector>(model[10]);
delta = as<NumericVector>(model[11]);
obs = as<NumericVector>(model[12]);
dist = (int) model[13];
}
}

当我从 R 调用(自动生成的包装器)varInfInput 时,它会抛出以下错误。

Error: expecting a single value 
3 stop(structure(list(message = "expecting a single value", call = NULL,
cppstack = NULL), .Names = c("message", "call", "cppstack"
), class = c("Rcpp::not_compatible", "C++Error", "error", "condition"
)))
2 varInfInput(output) at varInfFit.R#14
1 mmVarFit(test_model)

我是新手,一直在尝试尽可能多地阅读,因此,即使是对可能有帮助的内容的引用,我们也将不胜感激。

最佳答案

我想我解决了自己的问题。当我读入 int 时,它们仍然作为 SEXP 对象被读入,我不能只是将它们类型转换为 int。当我使用

T = as<IntegerVector>(model[0])[0]; 

它似乎按预期工作

关于c++ - 读取构造函数的 R 列表会引发错误 : expecting a single value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26616250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com