<div dir="ltr">Thank you Romain and Wush.<div><br></div><div>Where from can I get the latest version of Rcpp with these new features?</div><div>And how may I learn how to use them?</div><div><br></div><div>Many thanks,</div>

<div>Tal</div><div><br></div><div><br></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 Sun, Jul 21, 2013 at 12:48 PM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div dir="auto"><div>Hi Tal, <br><br>I'm travelling right now, i'll give some additional pointers later. </div><div><br></div><div>This is a nice non trivial example, and i hope that the code îve put in recently (with is<>) can help you so that you dont have to use TYPEOF for dispatching. </div>

<div><br></div><div>Our exchange at useR is part of my motivation for adding this. </div><div><br></div><div>Romain</div><div><br>Le 20 juil. 2013 à 18:44, Tal Galili <<a href="mailto:tal.galili@gmail.com" target="_blank">tal.galili@gmail.com</a>> a écrit :<br>

<br></div><div><div class="h5"><blockquote type="cite"><div><div dir="ltr">Hello dear Rcpp users,<div><br></div><div>First - I'd like to say that I took Hadley and Romain's workshop at the useR conference this year, and I am very excited about trying out Rcpp - this project looks AMAZING.</div>



<div><br></div><div>Second - this is my first post, and I apologize if my question is too basic. To my defense, I tried looking through this mailing list / googled before asking, and read through Hadley's github chapter on Rcpp.</div>



<div><br></div><div>The questions:<br><div><br></div><div>I am looking to understand better how List objects can be navigated, their elements accessed, and manipulated - using Rcpp.</div><div><br></div><div>For example, here is an R list object:</div>



<div><div><br></div><div>x <- list(a = 1, b = 2, c = list(ca = 3, cb = 4, 5), 6)</div><div>attr(x[[1]], "type") = "fun"</div><div>attr(x[[3]][[1]], "type") = "fun"</div><div>x</div>



</div><div><br></div><div>I would like to create two types of functions:</div><div>1) A function that will go through "x" and will <b><u>RETURN</u></b> all of the elements within it that are of type "fun".</div>



<div>In R I would do it like this:</div><div><br></div><div><div>return_fun <- function(x) {</div><div>   fun_nubmers <- numeric()</div><div>   for(i in seq_along(x)) {      </div><div>      if(class(x[[i]]) == "list") {</div>



<div>         fun_nubmers <- c(fun_nubmers, return_fun(x[[i]]))</div><div>      } else {</div><div>         if(!is.null(attr(x[[i]], "type"))) {</div><div>            if(attr(x[[i]], "type") == "fun") fun_nubmers <- c(fun_nubmers, x[[i]])   </div>



<div>         }         </div><div>      }</div><div>   }</div><div>   return fun_nubmers</div><div>}</div><div>return_fun(x) # output: 1 3</div></div><div><br></div><div>But in Rcpp there are many parts to this R function that I don't know how to do. I don't know how to access the attributes of a sub-element within a List, I don't know how to check if that attribute is null or not, etc. So any suggestions on either reading material (or better yet - an example of how this function might be created in Rcpp would be great.</div>



<div><br></div><div><br></div><div><div>2) A function that will go through "x" and will <b><u>CHANGE</u></b> all of the elements within it that are of type "fun". For example, adding 1 to them.</div><div>



In R I would do it like this:</div></div><div><br></div><div><div>add1_fun <- function(x) {</div><div>   for(i in seq_along(x)) {      </div><div>      if(class(x[[i]]) == "list") {</div><div>         x[[i]] <- add1_fun(x[[i]])</div>



<div>      } else {</div><div>         if(!is.null(attr(x[[i]], "type"))) {</div><div>            if(attr(x[[i]], "type") == "fun") x[[i]] <- x[[i]] + 1</div><div>         }         </div>



<div>      }</div><div>   }</div><div>   x</div><div>}</div><div>add1_fun(x) </div><div>return_fun(x) # output: 1 3 </div><div>return_fun(add1_fun(x) ) # output: 2 4 </div></div><div><br></div><div><br></div><div>3) Is it possible to work with some "global" variable from within a recursive Rcpp function?</div>



<div>For example:</div><div><br></div><div><div><br></div><div>count_till_5 <- function() {</div><div>   if(!exists("global_var")) global_var = 0 </div><div><br></div><div>   if(global_var <5) {</div><div>



      global_var <<- global_var+1</div><div>      count_till_5()</div><div>   }   </div><div>}</div><div>count_till_5()</div><div>global_var</div><div><br></div></div><div>Is there a way to create something like this with Rcpp?</div>



<div><br></div><div><br></div><div>Thanks in advance for any help.</div><div><br></div><div>With regards,</div><div>Tal</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>


<br>
</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><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>
</div></div></div>
</div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Rcpp-devel mailing list</span><br><span><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a></span><br>

<span><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></span></div></blockquote></div></blockquote>

</div><br></div>