[Rcpp-devel] Help with accessing and manipulating List objects

Romain Francois romain at r-enthusiasts.com
Fri Aug 16 19:33:48 CEST 2013


Le 16/08/13 14:48, Tal Galili a écrit :
> Hello Dirk,
> Your modifications/corrections are VERY helpful, thank you! (also, thank
> you for the general kind words and ongoing support, they are much
> appreciated)
>
> One more question/improvement - is it possible to have it return
> whatever value is inside the "label" attr?
>
> For example, running the following would result in an error:
>
> /*** R
> x <- list(a = 1, b=2, 6)
> attr(x[[2]], "leaf") = TRUE
> attr(x[[2]], "label") = 2
> extract_fun(x)
>
> ## output:
> ## Error: expecting a string
> /*
>
> Is there a way to either coerce/force "2" to become a string / or to
> return it as what it is? (a numeric/double value)?
> I thought that was what
>          label = as<std::string>(x.attr( "label" )) ;
> was doing - but apparently it does not coerce non-string into strings

String should know how to do that. I'd recommend you to use String, 
which is an Rcpp class rather than std::string.

For example a String knows what to do with an int:

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
String strings(){
     String s( 5 ) ;
     return s ;
}

 > strings()
[1] "5"

> Any suggestions?
>
> Tal


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list