问题
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