Reputation: 11
I know how to get values from a List objet, and how to create one within a C function.
But I would like to change a value in a List which is given as a parameter and that the modification would be effective when exiting the function.
Some thing like:
void myfunc(SEXP *lst) '
List mylist (lst)
// make some modification
}
I need to modify a list within a recursive loop.
Thanks in advance Daniel
Upvotes: 1
Views: 393
Reputation: 368389
There is no magic. Just assign elements from a List
object, and modify:
R> Rcpp::cppFunction("List foo(List L) { List L2 = L[0]; L2[1] = 42; return L2;} ")
R> L <- list(list(0,1,2), 2:4, 3.3)
R> foo(L)
[[1]]
[1] 0
[[2]]
[1] 42
[[3]]
[1] 2
R>
We pick the first element (and know it is a List
itself; there are predicates for testing). In that list, we set the second element. You can do the same returning the original but now modified list:
R> Rcpp::cppFunction("List bar(List L) { List L2 = L[0]; L2[1] = 42; return L;} ")
R> L <- list(list(0,1,2), 2:4, 3.3)
R> bar(L)
[[1]]
[[1]][[1]]
[1] 0
[[1]][[2]]
[1] 42
[[1]][[3]]
[1] 2
[[2]]
[1] 2 3 4
[[3]]
[1] 3.3
R>
Upvotes: 4