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