在Rcpp中提取嵌套列表中的元素

在Rcpp中提取嵌套列表中的元素,r,rcpp,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 t

我试图从列表中提取一个元素,并使用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
库(Rcpp)
cppFunction('int foo(Rcpp::List x,std::string step_name){
Rcpp::列出步骤=x(“步骤”);
Rcpp::列出步骤=步骤(步骤名称);
int a=步骤(“a”);
////不声明b会导致“未在此范围内声明”错误
////所以我用一个随机值初始化它。
std::string b=“随机”;
if(步骤containsElementNamed(“b”)){

Rcout它似乎主要是一个范围问题和一个变量类型问题。我相信
if-then
语句中的
std::string b
声明是局部的。那里的任何更改都不会持续

然后,您的注释中的错误试图用RHS
Rcpp:Vector
分配LHS
std::string
。虽然我确信有更好的转换和/或简化方法,但下面的解决方案只声明了
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
库(Rcpp)
cppFunction('int foo(Rcpp::List x,std::string step_name){
Rcpp::列出步骤=x(“步骤”);
Rcpp::列出步骤=步骤(步骤名称);
int a=步骤(“a”);
////不声明b会导致“未在此范围内声明”错误
////所以我用一个随机值初始化它。
特征向量b#############
if(步骤containsElementNamed(“b”)){

Rcout我在这里之前回答了嵌套列表问题,可能会尝试一些搜索。我还建议先尝试一个列表,然后在列表中尝试一个列表,然后测试空值。也就是说,如果您当前被卡住,则分解问题。我成功地将列表策略分解为
a
,但不用于
b
。我还认为通过简单的“搜索互联网…”回答/评论问题没有帮助,因为大多数SO问题都可以通过在互联网上搜索足够的时间来回答。如果您能指出重复的问题或可以回答此问题的链接,我将不胜感激,否则请不要发表评论。因为您提供的建议已经试过了(我的能力有限)如问题所示。由于您假设我没有进行充分的搜索,我也可能假设您没有阅读该问题,只是根据标题书发表评论,我试图帮助您,同时在这里处理其他两件事情。多年来,我似乎在这里回答了3500个问题,但我没有义务回答ei重复我的话。嵌套列表在Rcpp中工作得很好是为了鼓励你,你可能有一个普通的香草虫坐在这里,但鉴于你的语气,我不会花时间为你找到它。我不想不尊重你,我感谢你的时间。可能问题对于专家来说是显而易见的。我只是Ante表示我不同意在提交问题之前没有进行充分搜索的假设。这非常有帮助,特别是给我搜索“范围”和“类型”的关键字。我搜索了它们,并在and的帮助下得出了这段代码,这相当于我在提交问题之前尝试做的事情提供类型
std::string
std::string b=step.containsElementNamed(“b”)和&!Rf_isNull(step(“b”)?Rcpp::as(step(“b”):“”;