3

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

user2723494
  • 1,168
  • 2
  • 15
  • 26

2 Answers2

4

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

NumericVector Bgroups = as<NumericVector>(b["bgroups"]);
MSW Data
  • 441
  • 3
  • 8
0

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725