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

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


Le 16/08/13 16:19, Dirk Eddelbuettel a écrit :
>
> Hi Tal,
>
> On 16 August 2013 at 15:48, Tal Galili wrote:
> | 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?
>
> Yes, sure.
>
> | 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
> |
> |
> | Any suggestions?
>
> Am at work and a little pressed for time, so just random thoughts:
>
>    -- R has dynamic typing. An attribute could be 'anything'
>
>    -- C++ is typed. You either must agree with yourself on a type, or do the
>       casting. Converting int to string, or vice verse, is pretty common, and
>       Google will show you examples.
>
>    -- You could try to combine the approaches and just pick leaf elements and
>       stick them into a a List object as SEXP (using wrap(...)).
>
> This is a nice example of a recursize list / tree walker. We'll get it
> polished and onto the Rcpp Gallery in no time.
>
> Dirk

I'd wait for the next Rcpp to be out for putting this example on the 
gallery. because then we would be able to use the more elegant is<> 
rather than these is_list, ...

Romain

-- 
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