Accessing Named List Elements in Rcpp

泪湿孤枕 提交于 2019-12-12 17:01:32

问题


I would like to access a named list element by name in Rcpp

In R

> b = list(bgroups=c(1,1,1,1,1,0,0,0,0,0))
> b$bgroups
[1] 1 1 1 1 1 0 0 0 0 0

Then when trying to access this in Rcpp I have tried:

cppFunction(
  "
void f(List & b){
 std::vector<int> c(10) = as<std::vector<int>> b['bgroups'];
}
  "
)

...

NumericVector groupings = b['bgroups'];

...

NumericVector groupings(10) = b(4);

But to no avail.

I've reviewed Dirk's many helpful answers, but have not been able to make the connection https://github.com/eddelbuettel/rcppexamples/blob/master/src/ListExample.cpp

How to handle list in R to Rcpp


回答1:


Not sure if I understand your question completely but have you tired below in your CppFunction.

NumericVector Bgroups = as<NumericVector>(b["bgroups"]);



回答2:


You already mention the source code for ListExample.cpp, if you also look at the source for the R complement RcppListExample.R it really should become clear.

Your question is also not complete and reproducible so it makes it harder to help you.



来源:https://stackoverflow.com/questions/51258206/accessing-named-list-elements-in-rcpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!