简体   繁体   English

Rcpp:处理包含列表的列表

[英]Rcpp: Dealing with Lists that include Lists

Let's first generate some lists with lists inside: 让我们首先生成一些包含列表的列表:

lappend <- function(lst, ...){
  lst <- c(lst, list(...))
  return(lst)
}

scalarList <- list()
vectorList <- list()
MatrixList <- list()

for (i in 1:3){
  scalarList <- lappend(scalarList,i)
  vectorList <- lappend(vectorList,0:i)
  MatrixList <- lappend(MatrixList, diag(i + 1))
}

myRList <- list(scalarList = scalarList, vectorList = vectorList, 
  MatrixList = MatrixList)

Now that our myRList is ready, I'd like to write a function in C++ as follows: 既然我们的myRList准备好了,我想用C ++编写一个函数,如下所示:

1) inputs: 1) myRList, 2) an id from 1 to 3 1)输入:1)myRList,2)1到3的id

2) output: the function should separately print scalar, vector, and matrix corresponding to the input id. 2)输出:该函数应分别打印与输入id对应的标量,向量和矩阵。

3) There might be many ways to write this function. 3)编写此函数的方法可能有很多种。 I specifically want to read-in the list an assign it to a corresponding arma object and then print it. 我特别想要读入列表,将其分配给相应的arma对象然后打印它。 The reason is I wrote this simplified example to learn from you guys on how to extract elements of a list within another list in Rcpp and assign it to an arma object. 原因是我写了这个简化的例子,向大家学习如何在Rcpp的另一个列表中提取列表的元素并将其分配给arma对象。

Here is my C++ code: 这是我的C ++代码:

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>

// [[Rcpp::export]]
double myListExtractor( Rcpp::List aList_In_R, int id){
int myScalar = Rcpp::as<double>(aList_In_R["scalarList"][id]); //? what to do ?
arma::vec myVector = Rcpp::as<arma::vec>(aList_In_R["vectorList"[id]]); //???
arma::mat myMatrix = Rcpp::as<arma::mat>(aList_In_R["MatrixList"[id]]); //???

// If I manage to do the three assignments above, then printing is easy:

Rcpp::Rcout << "myScalar = " << myScalar << std::endl;
Rcpp::Rcout << "myVector = " << myVector << std::endl; 
Rcpp::Rcout << "myMatrix = " << myMatrix << std::endl;

return 1; // we don't care about the return for now
}

Again, I 100% agree that there is no need to assign them to arma objects and then print. 同样,我100%同意不需要将它们分配给arma对象然后打印。 In my own code I do lots of algebra with the arma objects and that's the reason I'm emphasizing on this assignment to learn how to fix my code/ 在我自己的代码中,我使用arma对象做了很多代数,这就是为什么我要强调这个任务来学习如何修复我的代码/

Thank you very much in advance for your help. 非常感谢您的帮助。 Also, I spent 3 hours browsing the Net and I didn't find any answer. 此外,我花了3个小时浏览网络,我没有找到任何答案。

Because List objects don't know anything about the type of objects they contain, you must as at each level of subsetting. 由于List对象不知道它们包含,您必须对象的类型,任何as在子集的每个级别。 Eg: 例如:

int myScalar = Rcpp::as<double>( Rcpp::as<Rcpp::List>(aList_In_R["scalarList"])[id] );

This way, we can signal to the compiler that the slot we're extracting from aList_In_R is a List , and all the methods we have for List s should apply here. 这样,我们可以向编译器发出信号,我们从aList_In_R提取的插槽是List ,而我们为List的所有方法都应该适用于此处。 It's ugly, but it's an unfortunate consequence on using a statically typed language to operate on objects used in a dynamically typed language. 这很丑陋,但使用静态类型语言操作动态类型语言中使用的对象是一个不幸的结果。

There is some work on getting templated list containers into Rcpp that might help with this situation; 将模板化列表容器放入Rcpp可能有助于解决这种情况; eg instead of having a List you might have a ListOf< List > whereby the subset operator would 'know' that it should return List after subsetting, which could be a bit nicer. 例如,代替拥有List你可能有一个ListOf< List > ,子集运算符将“知道”它应该在子集化后返回List ,这可能会更好一些。 Perhaps it could go as deep as ListOf< ListOf< double > > , etc... 也许它可以像ListOf< ListOf< double > >那样深入......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM