RCPP:更改列表列表中的列表项 [英] Rcpp: change a list item in a list of a list
问题描述
更改列表列表中的列表项.
Change a list item in a list of a list.
不幸的是,我在论坛中找不到我的问题的答案.
Unfortunately, I could not find an answer to my question in the forum.
使用rcpp,我想直接更改列表中的列表项.我有以下方法:
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
}
但是,我只能更改完整列表列表[i],而不能更改单个元素:list [[i]] or list [i][j]
.
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
我可以提取内部列表,但是在这里我仅更改新创建的列表,而不更改旧列表.对我来说,直接在R Workspace中更改列表至关重要.我当然可以更改新创建的列表,然后将其分配给旧列表.但是,我希望这里有一个更优雅的解决方案.
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
最后,我希望做到以下几点(直接在R Workspace中更改变量):
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.
非常感谢:)
推荐答案
我发现您发布的一些代码很难遵循,但这是一项相当简单的任务,无论是否要从R中的全局环境访问列表,首次尝试的编码方式,或者将列表作为参数传递;使用C ++代码
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
*/
我进了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"
更新
扩展到列表中的列表也很简单;使用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;
}
我在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"
这篇关于RCPP:更改列表列表中的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!