检查对象是否存在于指定路径的多层列表中

发布于 2025-02-03 00:53:36 字数 876 浏览 4 评论 0 原文

我有一个多级列表 l 具有任意数量的级别和条目。举一个简单的例子:

l <- list(a = 1,
          b = list(x = 2, y = 3),
          c = list(dog = 5, cat = list(fish = 3, mouse = 10))
         )

在我的软件包中,用户通过一个“地址”,该“地址”指定了此列表中的对象。地址作为字符串传递:

address = "$c$cat$fish"

这不包括列表本身的名称。

我编写了一个函数,该功能检查地址是否为“有效”,从某种意义上说我想检查指定地址是否存在对象。该函数的主要实质如下:

# this is the call to evaluate, as a string
expr_str <- paste0("address_value <- l", address)

# evaluate the call
eval(str2lang(expr_str))

if(is.null(address_value)){
   warning("Address is NULL or not found in list")
}

现在,这起作用(尽管可能有一种更优雅的方法可以做到这一点)。但是问题在于我在CMD检查中获得了注释,因为 address_value 没有可见的绑定,因为它是在 eval()中创建的。

我想摆脱注释,但我不确定如何在不使用 eval()的情况下检索 address 上的任何内容(或不是)。

谁能帮助解决这个问题?

I have a multi-level list l with an arbitrary number of levels and entries. To give a simple example:

l <- list(a = 1,
          b = list(x = 2, y = 3),
          c = list(dog = 5, cat = list(fish = 3, mouse = 10))
         )

In my package, the user passes an "address" which specifies an object in this list. The address is passed as a string, of the form:

address = "$c$cat$fish"

This excludes the name of the list itself.

I have written a function which checks whether the address is "valid", in the sense that I want to check whether an object exists at the specified address. The main substance of the function is as follows:

# this is the call to evaluate, as a string
expr_str <- paste0("address_value <- l", address)

# evaluate the call
eval(str2lang(expr_str))

if(is.null(address_value)){
   warning("Address is NULL or not found in list")
}

Now, this works (although there is probably a more elegant way to do it). But the problem is that I get a NOTE in the CMD check because the address_value doesn't have a visible binding, because it is created inside eval().

I want to get rid of the note but I'm not sure how to retrieve whatever is (or isn't) at address without using eval().

Can anyone help with this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒路情人 2025-02-10 00:53:36

您是否考虑过使用 purrr :: Pluck ?我认为这与您的要求相符:

l %>% pluck('c', 'cat', 'fish')
[1] 3

Have you considered using purrr::pluck? I think it matches your requirements:

l %>% pluck('c', 'cat', 'fish')
[1] 3
夜灵血窟げ 2025-02-10 00:53:36

的分配可以是 eval> eval 的 的分配,该可以避免CMD检查中未宣布的变量NAG。

check_address <- function(address) {
  
  expr_str <- paste0("l", address)
  
  # evaluate the call
  address_value <- eval(str2lang(expr_str))
  
  if(is.null(address_value)){
    warning("Address is NULL or not found in list")
  }
  
  address_value
}

这导致:

check_address("$c$cat$fish")
#> [1] 3

check_address("$c$cat$banana")
#> NULL
#> Warning message:
#> In check_address("$c$cat$banana") : Address is NULL or not found in list

The assignment to address_value can be outside of the eval, which avoids the undeclared variable nag in the CMD check.

check_address <- function(address) {
  
  expr_str <- paste0("l", address)
  
  # evaluate the call
  address_value <- eval(str2lang(expr_str))
  
  if(is.null(address_value)){
    warning("Address is NULL or not found in list")
  }
  
  address_value
}

Which results in:

check_address("$c$cat$fish")
#> [1] 3

check_address("$c$cat$banana")
#> NULL
#> Warning message:
#> In check_address("$c$cat$banana") : Address is NULL or not found in list
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文