Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object exists in multi-level list at specified path

Tags:

r

r-package

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?

like image 906
Will Avatar asked Nov 19 '25 13:11

Will


1 Answers

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

l %>% pluck('c', 'cat', 'fish')
[1] 3
like image 141
Dmitry Zotikov Avatar answered Nov 21 '25 02:11

Dmitry Zotikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!