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

Dirk Eddelbuettel edd at debian.org
Sat Jul 20 22:31:36 CEST 2013


Hi Tal,

On 20 July 2013 at 22:14, Tal Galili wrote:
| Hello Dirk and others,
| 
| Dirk - 
| Thank you for the quick reply!
| 
| Some responses:
| 
| 
| 1) I looked at the Rcpp gallery.
| The best I found was:
| http://gallery.rcpp.org/articles/setting-object-attributes/
| (which already was available in Hadley's book)

Sure. He too recycled some older posts. What goes around ...

| And:
| http://gallery.rcpp.org/articles/modifying-a-data-frame/
| http://gallery.rcpp.org/articles/reversing-a-vector/
| 
| And also now at unitTests/cpp/DataFrame
| (interesting)
| 
| But none have helped me with getting the attributes of an element inside a
| List.
| That is, say that we have x as a List, how do we go about fetching its attr?
| I tried:
| x[1].attr("type")
| x.attr[1]("type")
| But none seemed to have worked. Any suggestions there?

Sometimes it is easier / better / necessary to do this in two steps. 

First some R data:

R> foo <- list(bar=42, bing=21)
R> attr(foo[[1]], "type") <- "xyz"

Then a quick function in C++:

R> cppFunction('CharacterVector tal(List x) { IntegerVector y = x[0]; return y.attr("type"); }')
R> tal(foo)
[1] "xyz"
R> 

So once I take the element out of the list and assign it to a "standalone"
variable, things work.  [ This protects against overzealous template
expansions. ]

Similarly, I don't think you can (yet) easily do the attr() call on a sublist
in a list.

| 2) Your book is on my "to get a hold of somehow" list. Getting books to Israel
| is always trickier - I might just get it when I'll visit the US in a few
| months.

I see.
 
| 3) The post on Rcpp didn't get on R-bloggers since it wasn't marked with the
| "R" category.
| http://blog.revolutionanalytics.com/2013/07/
| deepen-your-r-experience-with-rcpp.html
| (David could still fix it if he wants to - and set the date of the post to be
| more recent)

I see. Could you email Joe (or David) ?
 
| 4) Having an
| is<List>(x) will be nice. Although I will need to see some examples to see when
| it is better then simply using: 
| x.inherits("list")
| 
| 5) I couldn't find a
|  'dispatch on type' example in the Rcpp gallery.
| I'm probably missing something in how I'm searching there.

Sorry, I was thinking of 

   http://gallery.rcpp.org/articles/rcpp-wrap-and-recurse/

which uses TYPEOF() and ...SXP to match on type.
 
| 6) Environment.cpp in the unitTest is interesting. It is probably what I
| needed.

Yes!
 
| 7) To Dirk and others - I will be happy to wait a bit to see if any of you can
| help by writing the R functions from my previous e-mail in Rcpp. That would be
| a big help for me in understanding how the relevant pieces should fit together.
| (the functions themselves are not useful, but they require many of the features
| I imagine I will need later on).

Maybe start with something simpler.  Just recursing through a list and
printing the (integer or whatever) elements is a very good exercise.

Cheers, Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list