[Rcpp-devel] Help with accessing and manipulating List objects
Tal Galili
tal.galili at gmail.com
Sat Jul 20 18:44:54 CEST 2013
Hello dear Rcpp users,
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.
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.
The questions:
I am looking to understand better how List objects can be navigated, their
elements accessed, and manipulated - using Rcpp.
For example, here is an R list object:
x <- list(a = 1, b = 2, c = list(ca = 3, cb = 4, 5), 6)
attr(x[[1]], "type") = "fun"
attr(x[[3]][[1]], "type") = "fun"
x
I would like to create two types of functions:
1) A function that will go through "x" and will *RETURN* all of the
elements within it that are of type "fun".
In R I would do it like this:
return_fun <- function(x) {
fun_nubmers <- numeric()
for(i in seq_along(x)) {
if(class(x[[i]]) == "list") {
fun_nubmers <- c(fun_nubmers, return_fun(x[[i]]))
} else {
if(!is.null(attr(x[[i]], "type"))) {
if(attr(x[[i]], "type") == "fun") fun_nubmers <- c(fun_nubmers,
x[[i]])
}
}
}
return fun_nubmers
}
return_fun(x) # output: 1 3
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.
2) A function that will go through "x" and will *CHANGE* all of the
elements within it that are of type "fun". For example, adding 1 to them.
In R I would do it like this:
add1_fun <- function(x) {
for(i in seq_along(x)) {
if(class(x[[i]]) == "list") {
x[[i]] <- add1_fun(x[[i]])
} else {
if(!is.null(attr(x[[i]], "type"))) {
if(attr(x[[i]], "type") == "fun") x[[i]] <- x[[i]] + 1
}
}
}
x
}
add1_fun(x)
return_fun(x) # output: 1 3
return_fun(add1_fun(x) ) # output: 2 4
3) Is it possible to work with some "global" variable from within a
recursive Rcpp function?
For example:
count_till_5 <- function() {
if(!exists("global_var")) global_var = 0
if(global_var <5) {
global_var <<- global_var+1
count_till_5()
}
}
count_till_5()
global_var
Is there a way to create something like this with Rcpp?
Thanks in advance for any help.
With regards,
Tal
----------------Contact
Details:-------------------------------------------------------
Contact me: Tal.Galili at gmail.com |
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130720/c1b2dedf/attachment.html>
More information about the Rcpp-devel
mailing list