Rcpp: change a list item in a list of a list

2020-05-09 18:55发布

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 :)

标签: c++ r list rcpp
1条回答
时光不老,我们不散
2楼-- · 2020-05-09 19:36

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

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
void test(List x) {
    CharacterVector tmp = x[0];
    tmp[0] = "Z";
    x[0] = tmp;
}

// [[Rcpp::export]]
void test2() {
    Environment env = Environment::global_env();
    List x = env["ListR"];
    CharacterVector tmp = x[0];
    tmp[0] = "Y";
    x[0] = tmp;
}

/*** R
ListR <- list(a = LETTERS[1:3])
ListR
test(ListR)
ListR
test2()
ListR
*/

I get in R

> Rcpp::sourceCpp("modify-list.cpp")

> ListR <- list(a = LETTERS[1:3])

> ListR
$a
[1] "A" "B" "C"


> test(ListR)

> ListR
$a
[1] "Z" "B" "C"


> test2()

> ListR
$a
[1] "Y" "B" "C"

Update

This is also fairly straightforward to extend to a list within a list; using the C++ code

#include <Rcpp.h>

using namespace Rcpp;

// For when you need to modify an element of a list within a list
// [[Rcpp::export]]
void test3() {
    Environment env = Environment::global_env();
    List y = env["listR"];
    List x = y[0];
    CharacterVector tmp = x[0];
    tmp[0] = "Z";
    x[0] = tmp;
}

I get the following in R

Rcpp::sourceCpp("modify-list.cpp")
listR = list(list())
listR[[1]] = list(LETTERS[1:3])
listR
# [[1]]
# [[1]][[1]]
# [1] "A" "B" "C"
test3()
listR
# [[1]]
# [[1]][[1]]
# [1] "Z" "B" "C"
查看更多
登录 后发表回答