check_dots_named | R Documentation |
Ensures that dots ...
are either empty (if .empty_ok = TRUE
), or all named elements in dots are a valid subset of .fn
's parameter
names. In case of an invalid or .forbidden
argument, an informative message is shown and the defined .action
is taken.
check_dots_named(
...,
.fn,
.additional = NULL,
.forbidden = NULL,
.empty_ok = TRUE,
.action = c("abort", "warn", "inform")
)
... |
Dots argument to check. |
.fn |
Function the |
.additional |
Parameter names within |
.forbidden |
Parameter names within |
.empty_ok |
Set to |
.action |
Action to take when the check fails. One of |
check_dots_named()
is intended to combat the second one of the two major downsides that using ...
usually brings. In chapter 6.6 of the book
Advanced R it is phrased as follows:
Using ...
comes with two downsides:
When you use it to pass arguments to another function, you have to carefully explain to the user where those arguments go. This makes it hard to
understand what you can do with functions like lapply()
and plot()
.
A misspelled argument will not raise an error. This makes it easy for typos to go unnoticed.
rlang's check dots functions: rlang::check_dots_empty()
, rlang::check_dots_used()
and rlang::check_dots_unnamed()
# We can use `check_dots_named()` to address this second downside:
sum_safe <- function(...,
na.rm = FALSE) {
pal::check_dots_named(...,
.fn = sum)
sum(...,
na.rm = na.rm)
}
# Note how the misspelled `na_rm` (instead of `na.rm`) silently gets ignored in the original
# function,
sum(1, 2, NA, na_rm = TRUE)
# whereas our safe version properly errors:
try(
sum_safe(1, 2, NA, na_rm = TRUE)
)
# We can even build an `sapply()` function that fails "intelligently":
sapply_safe <- function(X,
FUN,
...,
simplify = TRUE,
USE.NAMES = TRUE) {
pal::check_dots_named(...,
.fn = FUN)
sapply(X = X,
FUN = FUN,
...,
simplify = TRUE,
USE.NAMES = TRUE)
}
# While the original `sapply()` silently consumes misspelled named arguments via `...`,
sapply(1:5, paste, "hour workdays", sep = "-", colaspe = " ")
# `sapply_safe()` will throw an informative error message:
try(
sapply_safe(1:5, paste, "hour workdays", sep = "-", colaspe = " ")
)
# But be aware that `check_dots_named()` might be a bit rash,
try(
sum_safe(a = 1, b = 2)
)
# while the original function actually has nothing to complain:
sum(a = 1, b = 2)
# Furthermore, it doesn't play nicely with generics that don't expose all of the argument names
# of the method that is eventually invoked (`to` and `by` in the case of `seq()` -> `seq.int()`):
try(
sapply_safe(X = c(0,50),
FUN = seq,
to = 100,
by = 5)
)
# To work around this, directly supply the proper method (`seq.int`),
sapply_safe(X = c(0,50),
FUN = seq.int,
to = 100,
by = 5)
# or just provide `to` and `by` *unnamed*:
sapply_safe(X = c(0,50),
FUN = seq,
100,
5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.