0

I'm trying to implement named-list calling in Rcpp

in R

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

cppFunction(
  "
NumericVector split(Rcpp::List & b){
  Rcpp::NumericVector c= b['bgroups'];
  return c;
}")

split(b)

But this causes my R session to abort.

I'm trying to implement this procedure as illustrated in one of Dirk's presentations, but I'm missing something.

enter image description here

This is an extension of my question

user2723494
  • 1,168
  • 2
  • 15
  • 26

1 Answers1

8

The following works:

b<-list("bgroups"=c(1,1,1,1,0,0,0,0))
b$bgroups
#[1] 1 1 1 1 0 0 0 0

Rcpp::cppFunction(
  '
NumericVector split(Rcpp::List & b){
  Rcpp::NumericVector c = b["bgroups"];
  return c;
}')

split(b)
#[1] 1 1 1 1 0 0 0 0

In C++, ' is used to quote a character, while " is used to quote strings. My compiler warns me about this:

warning: character constant too long for its type
   Rcpp::NumericVector c= b['bgroups'];
                            ^~~~~~~~~

Generally it is a good idea to take compiler warnings seriously.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75