<div dir="ltr">Thanks Romain and Dirk!<div>I am currently (slowly) working on developing a package where I would need some of these skills for specific bottleneck functions. I will get to the stage of using your code probably in something between 2 to 5 weeks (I hope).</div>


<div>Once I reach it I will gladly share my progress.</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 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 Tue, Jul 23, 2013 at 3:23 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">


Hello Tal,<br>
<br>
I'm trying to express this with the tools available in Rcpp 0.10.3. (And adding some tools for later versions too, like is<>).<br>
<br>
Here is how I would do the first problem:<br>
<br>
First break down into functions. We need :<br>
- something that tells me if a given R object is a list. With Rcpp devel, we could use is<List> but for now we can write it as:<br>
<br>
bool is_list(RObject x){<br>
    return TYPEOF(x) == VECSXP ;<br>
}<br>
<br>
I've added the is<> so that you don't need to know about TYPEOF or VECSXP. But here we are essentially mimicking typeof( x ) == "list"<br>
<br>
- We need something to tell me if something is a single string:<br>
<br>
bool is_string(RObject x){<br>
    return TYPEOF(x) == STRSXP && Rf_length(x) == 1 ;<br>
}<br>
<br>
agai, with Rcpp devel, we could express this like this: is<String>( x ) instead.<br>
<br>
- We need something that tells if an object has a type attribute that is equal to fun. R lets us do that easily, but we have to pay that flexibility in C++. What I came up with is this:<br>
<br>
bool is_fun(RObject x){<br>
    // is this a numeric vector<br>
    if( TYPEOF(x) != REALSXP ) return false ;<br>
<br>
    // is the type attribute a string ?<br>
    if( !is_string( x.attr("type") ) ) return false ;<br>
<br>
    // test the string<br>
    String type = x.attr( "type" ) ;<br>
    return type == "fun" ;<br>
}<br>
<br>
- Then, we need a recursive function that goes through elements of a list. For collecting results, I'm choosing to pass a std::vector by reference. So there are two things to learn here:<br>
   - pass by reference<br>
   - std::vector<br>
you'll find many resources online to teach you these things.<br>
<br>
void process( List data, std::vector<double>& results ){<br>
    for( int i=0; i<data.size(); i++){<br>
        if( is_list( data[i] ) ){<br>
            // recurse<br>
            process( data[i], results ) ;<br>
        } else if( is_fun( data[i] ) ){<br>
            // we want to collect them. we can use the NumericVector class<br>
            // wince we know thius is a numeric vector.<br>
            // (it is tested in is_fun)<br>
            NumericVector x = data[i] ;<br>
<br>
            // loop through the values and add them to the results vector<br>
            for( int j=0; j<x.size(); j++){<br>
                results.push_back( x[j] ) ;<br>
            }<br>
        } // else do nothing<br>
    }<br>
}<br>
<br>
<br>
- Finally, we need something that you can call from R. It takes a list as input, and returns a numeric vector.<br>
<br>
// [[Rcpp::export]]<br>
NumericVector extract_fun(List x){<br>
    std::vector<double> results ;<br>
    process(x, results) ;<br>
    return wrap(results) ;<br>
}<br>
<br>
<br>
Here is the full script:<br>
<br>
#include <Rcpp.h><br>
using namespace Rcpp ;<br>
<br>
bool is_list(RObject x){<br>
    return TYPEOF(x) == VECSXP ;<br>
}<br>
<br>
bool is_string(RObject x){<br>
    return TYPEOF(x) == STRSXP && Rf_length(x) == 1 ;<br>
}<br>
<br>
bool is_fun(RObject x){<br>
    if( TYPEOF(x) != REALSXP ) return false ;<br>
    if( !is_string( x.attr("type") ) ) return false ;<br>
    String type = x.attr( "type" ) ;<br>
    return type == "fun" ;<br>
}<br>
<br>
void process( List data, std::vector<double>& results ){<br>
    for( int i=0; i<data.size(); i++){<br>
        if( is_list( data[i] ) ){<br>
            // recurse<br>
            process( data[i], results ) ;<br>
        } else if( is_fun( data[i] ) ){<br>
            // we want to collect them. we can use the NumericVector class<br>
            // wince we know thius is a numeric vector.<br>
            // (it is tested in is_fun)<br>
            NumericVector x = data[i] ;<br>
<br>
            // loop through the values and add them to the results vector<br>
            for( int j=0; j<x.size(); j++){<br>
                results.push_back( x[j] ) ;<br>
            }<br>
        } // else do nothing<br>
    }<br>
}<br>
<br>
// [[Rcpp::export]]<br>
NumericVector extract_fun(List x){<br>
    std::vector<double> results ;<br>
    process(x, results) ;<br>
    return wrap(results) ;<br>
}<br>
<br>
/*** R<div><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></div>
extract_fun(x)<br>
<br>
*/<br>
<br>
You should be able to sourceCpp it and get the expected result.<br>
<br>
<br>
<br>
I let you do 2 and 3 as an exercize with what you learn from this.<br>
Hints:<br>
- For 2). you don't need to collect results, so your process can look like this:<br>
<br>
void process( List data ){<br>
    // ...<br>
}<br>
<br>
I let you update the body to do what you wanted<br>
<br>
- For 3). There are several ways to do this in C++. you could use a reference to an int, a static variable within the function. You could even use some data that lives in an R environment, using the Environment class.<br>



<br>
Whatever you come up with, feel free to share. Those examples are nice examples of something that is relatively easy to do when you start to be Rcpp fluent, but not so easy otherwise.<br>
<br>
Romain<br>
<br>
<br>
Le 20/07/13 18:44, Tal Galili a écrit :<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Hello dear Rcpp users,<br>
<br>
First - I'd like to say that I took Hadley and Romain's workshop at the<br>
useR conference this year, and I am very excited about trying out Rcpp -<br>
this project looks AMAZING.<br>
<br>
Second - this is my first post, and I apologize if my question is too<br>
basic. To my defense, I tried looking through this mailing list /<br>
googled before asking, and read through Hadley's github chapter on Rcpp.<br>
<br>
The questions:<br>
<br>
I am looking to understand better how List objects can be navigated,<br>
their 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></div>
1) A function that will go through "x" and will *_RETURN_* all of the<div><br>
elements 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 <-<br>
c(fun_nubmers, x[[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<br>
how to do. I don't know how to access the attributes of a sub-element<br>
within a List, I don't know how to check if that attribute is null or<br>
not, etc. So any suggestions on either reading material (or better yet -<br>
an example of how this function might be created in Rcpp would be great.<br>
<br>
<br></div>
2) A function that will go through "x" and will *_CHANGE_* all of the<div><div><br>
elements 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>
<br>
3) Is it possible to work with some "global" variable from within a<br>
recursive 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>
<br>
Thanks in advance for any help.<br>
<br>
With regards,<br>
Tal<br>
</div></div></blockquote><span><font color="#888888">
<br>
-- <br>
Romain Francois<br>
Professional R Enthusiast<br>
<a href="tel:%2B33%280%29%206%2028%2091%2030%2030" value="+33628913030" target="_blank">+33(0) 6 28 91 30 30</a><br>
<br>
R Graph Gallery: <a href="http://gallery.r-enthusiasts.com" target="_blank">http://gallery.r-enthusiasts.<u></u>com</a><br>
<br>
blog:            <a href="http://blog.r-enthusiasts.com" target="_blank">http://blog.r-enthusiasts.com</a><br>
|- <a href="http://bit.ly/13SrjxO" target="_blank">http://bit.ly/13SrjxO</a> : highlight 0.4.2<br>
`- <a href="http://bit.ly/10X94UM" target="_blank">http://bit.ly/10X94UM</a> : Mobile version of the graph gallery<br>
<br>
</font></span></blockquote></div><br></div></div>