r - 在 Rcpp 的嵌套列表中提取元素
问题描述
我正在尝试从列表中提取一个元素并使用 Rcpp 对其值做一些事情。但我不能做任务。
这基本上是我想要在 R 代码中实现的目标:
mylist = list(steps = list(`1` = list(a = 7, b = "abc"),
`2` = list(a = 3),
`3` = list(a = 5, b = NULL)))
# This is the desired behavior that I can program in R
step_name = "1"
b = ifelse(is.null(mylist$steps[[step_name]][["b"]]),
"", mylist$steps[[step_name]][["b"]])
# Do something with the value of b
以下代码无法对 进行分配b
。的值a
按应有的方式提取。我不知道我在这里错过了什么。
library(Rcpp)
cppFunction('int foo(Rcpp::List x, std::string step_name) {
Rcpp::List steps = x("steps");
Rcpp::List step = steps(step_name);
int a = step("a");
//// Not declaring b causes "not declared in this scope" error
//// so I initialize it with a random value.
std::string b = "random";
if (step.containsElementNamed("b")){
Rcout << "b is in List!" << "\\n";
if (!Rf_isNull(step("b"))) {
Rcout << "b is not NULL!" << "\\n";
if (TYPEOF(step("b")) == STRSXP)
Rcout << "b is character!" << "\\n";
std::string b = step("b");
} else {
Rcout << "b is NULL!" << "\\n";
std::string b = "efg";
}
} else {
Rcout << "b is not in List!" << "\\n";
std::string b = "xyz";
}
Rcout << "The Value of b is " << b << ".\\n";
if (b == "abc") {
//// Do something with the value of b
}
return a;
}')
foo(mylist, "1")
## b is in List!
## b is not NULL!
## b is character!
## The Value of b is random.
## [1] 7
foo(mylist, "2")
## b is not in List!
## The Value of b is random.
## [1] 3
foo(mylist, "3")
## b is in List!
## b is NULL!
## The Value of b is random.
## [1] 5
解决方案
它似乎主要是范围问题和变量类型问题。我相信std::string b
声明中的if then
声明是本地的。那里的任何改变都不会持久。
然后,您评论中的错误试图将 LHS 分配给std::string
RHS Rcpp:Vector
。虽然我确信有更好的方法来转换和/或简化,但下面的解决方案只是声明Rcpp::CharacterVector b
.
library(Rcpp)
cppFunction('int foo(Rcpp::List x, std::string step_name) {
Rcpp::List steps = x("steps");
Rcpp::List step = steps(step_name);
int a = step("a");
//// Not declaring b causes "not declared in this scope" error
//// so I initialize it with a random value.
CharacterVector b; #############
if (step.containsElementNamed("b")){
Rcout << "b is in List!" << "\\n";
if (!Rf_isNull(step("b"))) {
Rcout << "b is not NULL!" << "\\n";
if (TYPEOF(step("b")) == STRSXP)
Rcout << "b is character!" << "\\n";
b = step("b");
} else {
Rcout << "b is NULL!" << "\\n";
}
} else {
Rcout << "b is not in List!" << "\\n";
}
Rcout << "The size of b is " << b.size() << ".\\n"; #########
if (b.size() > 0 && b[0] == "abc") { ################
Rcout << "Do something with b.\\n";
//// Do something with the value of b
}
return a;
}')
mylist = list(steps = list(`1` = list(a = 7, b = "abc"),
`2` = list(a = 3),
`3` = list(a = 5, b = NULL)))
foo(mylist, "1")
# b is in List!
# b is not NULL!
# b is character!
# The size of b is 1.
# Do something with b.
# [1] 7
foo(mylist, "2")
# b is not in List!
# The size of b is 0.
# [1] 3
foo(mylist, "3")
# b is in List!
# b is NULL!
# The size of b is 0.
# [1] 5
推荐阅读
- python - 如何在python中编写谷歌表格
- c++ - Getline 代码无法正常工作或我的代码无法正确编译
- python - 使用python对嵌套JSON的键进行展平和映射
- php - 从 url 清除永久链接删除 php 扩展
- javascript - 无法在库中使用嵌套的延迟加载模块构建 Angular 8 应用程序
- swift - 将函数作为参数传递 swift
- python-3.x - 如何在 Python 中使用 XSD 验证 XML
- sql - 如果结果为空,则显示一列我在哪里输入的内容
- c# - 使用 2.1 版的 Service Fabric .net 核心远程处理
- react-native - 如何同时处理视图和按钮的点击?