Let's say I have a List with a few elements (< 20) but each of them is a vector (IntegerVector, Numericvector or CharacterVector) of several GB. I therefor want to avoid any copy.
To remove element from my List, I write the following Rcpp code :
void list_remove_element (List x, int i) {
Rcout << "Size before : " << x.size() << endl;
x.erase(i);
Rcout << "Size after : " <<x.size() << endl;
}
Internaly, this code effectively erase the corresponding. Sadly after the return of this function no change appears in R :
> u = list(a=1:5, b=3:4, c=5:6)
> list_remove_elements (u, 1)
Size before : 3
Size after : 2
> str(u)
List of 3
$ a: int [1:5] 1 2 3 4 5
$ b: int [1:2] 3 4
$ c: int [1:2] 5 6
As far as I have understood, using a function to grow or shrink an Rcpp object results of a data copy from the original object into a new object. Is there any solution to avoid this?
EDIT :
I also tried to do the following :
void list_remove_elements (SEXP x) {
SET_VECTOR_ELT(x, 1, R_NilValue);
}
It almost works since I get :
> str(u)
List of 3
$ a: int [1:5] 1 2 3 4 5
$ b: NULL
$ c: int [1:2] 5 6
But I still have the element 'b' and not sure this is the correct way to do ...
u[2] <- NULL?u[[2]] <- NULL.A. I use my other functions like this :fct1(A); fct2(A) ;And I don't really want to do the following :fct1(A); A = fct2 (A) ;But maybe I misunderstand your answers ...