Change a list item in a list of a list.
Unfortunately, I could not find an answer to my question in the forum.
Using rcpp, I want to directly change a list item in a list. I have the following approach:
// [[Rcpp::export]]
void test(){
Environment env = Environment::global_env();
List outerList = env["ListR"];
List innerList = polymeraseFlagList[0];
outerList((1)) ) "test"; // correctly changing inner list
CharacterVector innerStr = innerList[1]; // correctly access to inner list element
}
However, I am only able to change the complete list list [i] and not a single element: list [[i]] or list [i][j]
.
outerList[i][j] = "new inner list element"; // not working
outerList[[i]] = "new inner list"; // not working
I can extract the inner list, but here I change only the newly created list and not the old list. It is essential for me to change the list in R Workspace directly. I could of course change the newly created list and later assign it to the old one. However, I hope that there is a more elegant solution here.
I also tried to declare the list before assigning it so that I already have a nested list that I can access as usual. Unfortunately, this did not work.
List outerList = List::create(Named("lst")); // not working
In the end, I want the following to be possible (change the variable directly in the R Workspace):
// [[Rcpp::export]]
void test(){
Environment env = Environment::global_env();
List outerList = env["ListR"];
CharacterVector innerStr = outerList[i][j];
CharacterVector innerList = outerList[[i]]
innerList[i][j] = "new String";
}
It would be great if someone could help me.
Many thanks :)
I found some of your posted code hard to follow, but this is a fairly straightforward task, whether accessing the list from the global environment in R in a hard-coded way as you first tried, or having the list passed as a parameter; using the C++ code
I get in R
Update
This is also fairly straightforward to extend to a list within a list; using the C++ code
I get the following in R