I would like the following code to error because df$t does not exist. I do not want df$this_is_the_wrong_variable to be returned:
df <- data.frame(
this_is_the_wrong_variable = c(0, 1),
r = c(2, 3),
s = c(4, 5),
st = c(6, 7), # typo!
)
print(df$t) # this does not give an error!
How is this weird concept called, and how can I disable it?
Searching the ?options page for "partial" finds this option:
warnPartialMatchDollar:logical. If true, warns if partial matching is used for extraction by
$.
So setting that option (options(warnPartialMatchDollar = TRUE)) will turn it into a warning.
I don't think there's an easy to to turn only that warning into an error, but looking up a few rows in the ?options help if you set options(warn = 2), all warnings will be treated as errors.
Alternately, you could use tibbles, which don't use partial matching with $ (though it returns NULL will a warning, not an error):
library(tibble)
as_tibble(df)$t
# NULL
# Warning message:
# Unknown or uninitialised column: `t`.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With