【发布时间】:2014-01-21 05:10:38
【问题描述】:
让我们首先生成一些列表,里面有列表:
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)
现在我们的 myRList 已经准备好了,我想用 C++ 编写一个函数,如下所示:
1) 输入:1) myRList,2) 从 1 到 3 的 id
2) 输出:函数应该分别打印输入id对应的标量、向量和矩阵。
3) 编写这个函数可能有很多方法。我特别想读入列表并将其分配给相应的 arma 对象,然后打印它。原因是我写了这个简化的例子来向你们学习如何在 Rcpp 中提取另一个列表中的列表元素并将其分配给 arma 对象。
这是我的 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
}
再一次,我 100% 同意没有必要将它们分配给 arma 对象然后打印。在我自己的代码中,我对 arma 对象做了很多代数,这就是我强调这项作业以学习如何修复我的代码的原因/
非常感谢您的帮助。另外,我花了 3 个小时浏览了网络,但没有找到任何答案。
【问题讨论】:
-
一种解决方案是在每个子列表上调用函数,或者不调用 myRList 上的函数,我们可以为函数提供三个参数 myRList$scalarList、myRList$vectorList 和 myRList$ MatrixList 和每个单独工作
-
搜索
[rcpp] List有 183 个结果。你先搜索了吗? -
@DirkEddelbuettel,早上好。是的,我做到了。正如您在书中所解释的那样,从列表中检索元素有很多有用的答案。我没有找到关于处理包含子列表的列表的任何问题/答案。
-
我很确定我们也在某个地方回答了这个问题。 在概念上确实是一样的:你从一个列表中获取一个
SEXP。这恰好是一个列表,您可以从中获取SEXP... -
你说得对——这是this question的副本。