Replenishment of objects with lists with the symbol cashtag ($)

Good morning everybody,

I have a very simple question.

foo <- list(bar=2)

As you all know, I can access the object barusing brackets or using the cashtag ($) symbol.

> foo$bar
[1] 2
> foo[["bar"]]
[1] 2

If I replaced barwith ba, I will get the same result with cashtag ($), but another with brackets.

> foo$ba
[1] 2
> foo[["ba"]]
NULL

Is there a way to get the result NULLinstead 2using cashtag ($) in this situation?

+4
source share
2 answers

I do not believe that you can return it to NULL if the list does not contain anything else, which also partially corresponds to the input. Therefore, if there was a bak element in your list, then foo $ ba will return null since the partial match was not unique.

> foo <- list(bar=2)
> foo$ba
[1] 2
> foo$bak <- NA
> foo$ba
NULL

, , / .

> options(warnPartialMatchDollar = TRUE)
> foo <- list(bar = 2)
> foo$ba
[1] 2
Warning message:
In foo$ba : partial match of 'ba' to 'bar'

, , . , NULL , [[ $, , , [[ .

+4

@Dason help.

options(warnPartialMatchDollar = TRUE)

foo <- list(bar=2)
tryCatch(foo$b, warning=function(x){NULL})

.

!

+2

Source: https://habr.com/ru/post/1686972/


All Articles