[Rcpp-devel] Boolean vector from R to C++

Douglas Bates bates at stat.wisc.edu
Thu Apr 14 17:14:59 CEST 2011


On Thu, Apr 14, 2011 at 10:00 AM, Cedric Ginestet
<c.ginestet05 at googlemail.com> wrote:
> Hi all,
>
> I am trying to pass on a boolean vector from R to C++. I have naively tried
> the following:
>
> ################################
> src <- '
>   using namespace Rcpp ;
>   using namespace std ;
>   int b = as<bool>(xb);
> return wrap(b);'
> cxxfun <- cxxfunction(sig=signature(xb="logical"),
>                       body=src,plugin="Rcpp",verbose=TRUE)
> cxxfun(b);
>
> Error in cxxfun(b) : expecting a single value
> ###############################
>
> Is there an equivalent to IntegerVector or NumericVector for Boolean
> variables or can I simply use the Rcpp::Vector template for this purpose?

In R a logical vector is an IntegerVector.  You could transform it to
a std::vector<bool> if you were particularly concerned about space but
you would lose the ability to represent missing values.  That is,
elements of an R logical vector can be TRUE, FALSE or <NA> whereas the
C++ bool type can only be true or false.


More information about the Rcpp-devel mailing list