[Rcpp-devel] type information about elements in Rcpp::List
Romain François
romain at r-enthusiasts.com
Mon Jun 9 21:43:32 CEST 2014
Le 9 juin 2014 à 21:40, Chaomei Lo <chaomeilo at gmail.com> a écrit :
>
> I have a R list something like this -
>
> op=list(a=200, b="test", c=4.5)
> when I pass it as an argument to Rcp Export function which the code looks like this in below.
>
> ----------------------------------------
> int n = xlist.length();
> for (int I=0; I < n; I++) {
> SEXP s = xlist[I];
> if (Rf_isInteger(s)) {
> //do something
> }
> else if (Rf_isNumerc(s)) {
> //do other thing
> }
> else if (Rf_isString(s)) {
> //do other thing
> }
> }
>
> However, both a and c go to the Rf_isNumeric if statement; but the first one is an integer,
no it’s not.
> str( 200 )
num 200
> str( 200L )
int 200
> str( as.integer(200) )
int 200
Suffix with « L » or use as.integer if you want an integer.
Alternatively, Rcpp has is<>, so you can use
if( is<int>(s) ) {
...
}
Romain
> not sure why it did not go to into the Rf_isInteger if statement.
>
> Thanks.
More information about the Rcpp-devel
mailing list