<div dir="ltr"><div>Hello Dirk and others,</div><div><br></div><div>Dirk - </div><div>Thank you for the quick reply!</div><div><br></div><div>Some responses:</div><div><br></div><div><br></div><div><div>1) I looked at the Rcpp gallery.</div>

<div>The best I found was:</div><div><a href="http://gallery.rcpp.org/articles/setting-object-attributes/">http://gallery.rcpp.org/articles/setting-object-attributes/</a></div><div>(which already was available in Hadley's book)</div>

<div>And:</div><div><a href="http://gallery.rcpp.org/articles/modifying-a-data-frame/">http://gallery.rcpp.org/articles/modifying-a-data-frame/</a></div><div><a href="http://gallery.rcpp.org/articles/reversing-a-vector/">http://gallery.rcpp.org/articles/reversing-a-vector/</a></div>

<div><br></div><div>And also now at unitTests/cpp/DataFrame</div><div>(interesting)</div><div><br></div><div>But none have helped me with getting the attributes of an element inside a List.</div><div>That is, say that we have x as a List, how do we go about fetching its attr?</div>

<div>I tried:</div><div>x[1].attr("type")</div><div>x.attr[1]("type")</div><div>But none seemed to have worked. Any suggestions there?</div></div><div><br></div><div>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.<br>

</div><div><br></div><div><div>3) The post on Rcpp didn't get on R-bloggers since it wasn't marked with the "R" category.</div><div><a href="http://blog.revolutionanalytics.com/2013/07/deepen-your-r-experience-with-rcpp.html">http://blog.revolutionanalytics.com/2013/07/deepen-your-r-experience-with-rcpp.html</a></div>

<div>(David could still fix it if he wants to - and set the date of the post to be more recent)</div></div><div><br></div><div>4) Having an</div><div>is<List>(x) will be nice. Although I will need to see some examples to see when it is better then simply using: </div>

<div>x.inherits("list")</div><div><br></div><div>5) I couldn't find a</div><div> 'dispatch on type' example in the Rcpp gallery.</div><div>I'm probably missing something in how I'm searching there.</div>

<div><br></div><div>6) Environment.cpp in the unitTest is interesting. It is probably what I needed.</div><div><br></div><div>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.</div>

<div>(the functions themselves are not useful, but they require many of the features I imagine I will need later on).</div><div><br></div><div><br></div><div>Cheers,</div><div>Tal</div><div><br></div></div><div class="gmail_extra">

<br clear="all"><div><div dir="ltr"><br>----------------Contact Details:-------------------------------------------------------<br>Contact me: <a href="mailto:Tal.Galili@gmail.com" target="_blank">Tal.Galili@gmail.com</a> |  <br>

Read me: <a href="http://www.talgalili.com" target="_blank">www.talgalili.com</a> (Hebrew) | <a href="http://www.biostatistics.co.il" target="_blank">www.biostatistics.co.il</a> (Hebrew) | <a href="http://www.r-statistics.com" target="_blank">www.r-statistics.com</a> (English)<br>

----------------------------------------------------------------------------------------------<br><br></div></div>
<br><br><div class="gmail_quote">On Sat, Jul 20, 2013 at 8:47 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br>
Hi Tal,<br>
<br>
And welcome!<br>
<div class="im"><br>
On 20 July 2013 at 19:44, Tal Galili wrote:<br>
| Hello dear Rcpp users,<br>
|<br>
| First - I'd like to say that I took Hadley and Romain's workshop at the useR<br>
| conference this year, and I am very excited about trying out Rcpp - this<br>
| project looks AMAZING.<br>
<br>
</div>Your feedback (as conveyed earlier in private email) is much appreciated.<br>
<br>
There was a second, and also very positive, response to the same workshop:<br>
Joe posted it on the REvo blog last week---somehow that never made it through<br>
R Bloggers, I think.<br>
<div class="im"><br>
| Second - this is my first post, and I apologize if my question is too basic. To<br>
| my defense, I tried looking through this mailing list / googled before asking,<br>
| and read through Hadley's github chapter on Rcpp.<br>
<br>
</div>I hear there is a book on Rcpp too :)<br>
<div><div class="h5"><br>
| The questions:<br>
|<br>
| I am looking to understand better how List objects can be navigated, their<br>
| elements accessed, and manipulated - using Rcpp.<br>
|<br>
| For example, here is an R list object:<br>
|<br>
| x <- list(a = 1, b = 2, c = list(ca = 3, cb = 4, 5), 6)<br>
| attr(x[[1]], "type") = "fun"<br>
| attr(x[[3]][[1]], "type") = "fun"<br>
| x<br>
|<br>
| I would like to create two types of functions:<br>
| 1) A function that will go through "x" and will RETURN all of the elements<br>
| within it that are of type "fun".<br>
| In R I would do it like this:<br>
|<br>
| return_fun <- function(x) {<br>
|    fun_nubmers <- numeric()<br>
|    for(i in seq_along(x)) {      <br>
|       if(class(x[[i]]) == "list") {<br>
|          fun_nubmers <- c(fun_nubmers, return_fun(x[[i]]))<br>
|       } else {<br>
|          if(!is.null(attr(x[[i]], "type"))) {<br>
|             if(attr(x[[i]], "type") == "fun") fun_nubmers <- c(fun_nubmers, x<br>
| [[i]])   <br>
|          }         <br>
|       }<br>
|    }<br>
|    return fun_nubmers<br>
| }<br>
| return_fun(x) # output: 1 3<br>
|<br>
| But in Rcpp there are many parts to this R function that I don't know how to<br>
| do. I don't know how to access the attributes of a sub-element within a List, I<br>
| don't know how to check if that attribute is null or not, etc. So any<br>
| suggestions on either reading material (or better yet - an example of how this<br>
| function might be created in Rcpp would be great.<br>
<br>
</div></div>You can access attributes, pretty much as in R.  You can access lists within<br>
lists, pretty much as in R.<br>
<br>
Two quick pointers to working examples:<br>
<br>
  -- our unit tests, especially now that they all have been rewritten via<br>
     Rcpp attributes to be R files calling sourceCpp on C++ files:<br>
<br>
     edd@max:~$ grep -l attr /usr/local/lib/R/site-library/Rcpp/unitTests/cpp/*<br>
     /usr/local/lib/R/site-library/Rcpp/unitTests/cpp/DataFrame.cpp<br>
     /usr/local/lib/R/site-library/Rcpp/unitTests/cpp/S4.cpp<br>
     edd@max:~$<br>
<br>
  -- the Rcpp Gallery which has several posts on attr<br>
<br>
Likewise, Lists within lists can be accessed as well. You need to test each<br>
element for its type. The Rcpp Gallery has a 'dispatch on type' example as I<br>
recall. These still use the C API of R --- but Romain just added beginnings<br>
of is<foo>(x) tests to Rcpp's SVN which will help with tasks like this.<br>
<div class="im"><br>
| 2) A function that will go through "x" and will CHANGE all of the elements<br>
| within it that are of type "fun". For example, adding 1 to them.<br>
| In R I would do it like this:<br>
|<br>
| add1_fun <- function(x) {<br>
|    for(i in seq_along(x)) {      <br>
|       if(class(x[[i]]) == "list") {<br>
|          x[[i]] <- add1_fun(x[[i]])<br>
|       } else {<br>
|          if(!is.null(attr(x[[i]], "type"))) {<br>
|             if(attr(x[[i]], "type") == "fun") x[[i]] <- x[[i]] + 1<br>
|          }         <br>
|       }<br>
|    }<br>
|    x<br>
| }<br>
| add1_fun(x) <br>
| return_fun(x) # output: 1 3 <br>
| return_fun(add1_fun(x) ) # output: 2 4 <br>
<br>
</div>That is similar -- you need to test and recurse as well.<br>
<div class="im"><br>
| 3) Is it possible to work with some "global" variable from within a recursive<br>
| Rcpp function?<br>
| For example:<br>
|<br>
|<br>
| count_till_5 <- function() {<br>
|    if(!exists("global_var")) global_var = 0 <br>
|<br>
|    if(global_var <5) {<br>
|       global_var <<- global_var+1<br>
|       count_till_5()<br>
|    }   <br>
| }<br>
| count_till_5()<br>
| global_var<br>
|<br>
| Is there a way to create something like this with Rcpp?<br>
<br>
</div>Yes by using the Environment class and accessing elements from a given<br>
environment -- here the global one.<br>
<br>
Again, I think the Rcpp Gallery has a Environment example. The unit tests do.<br>
<br>
These are all nice yet not entirely trivial questions. I would like try to<br>
cook something up (unless somebody beats me to it) but I have a things I need<br>
to be working on instead ...<br>
<br>
Cheers, Dirk<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>