13 Avoid magical defaults
13.1 What’s the problem?
If a function behaves differently when the default value is suppled explicitly, we say it has a magical default. Magical defaults are best avoided because they make it harder to interpret the function specification.
13.2 What are some examples?
data.frame(), the default argument for
NULL, but if you supply it directly you get a different result:
fun_call(data.frame) #> base::data.frame(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, #> fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors()) x <- setNames(nm = letters[1:3]) data.frame(x) #> x #> a a #> b b #> c c data.frame(x, row.names = NULL) #> x #> 1 a #> 2 b #> 3 c
hist(), the default value of
range(breaks), and the default value for
c("Sturges", "Sturges")which doesn’t work when supplied explicitly:
fun_call(hist.default) #> graphics::hist.default(x, breaks = "Sturges", freq = NULL, probability = !freq, #> include.lowest = TRUE, right = TRUE, density = NULL, angle = 45, #> col = "lightgray", border = NULL, main = paste("Histogram of", #> xname), xlim = range(breaks), ylim = NULL, xlab = xname, #> ylab, axes = TRUE, plot = TRUE, labels = FALSE, nclass = NULL, #> warn.unused = TRUE, ...) hist(1:10, xlim = c("Sturges", "Sturges")) #> Error in plot.window(xlim, ylim, "", ...): invalid 'xlim' value
Vectorize(), the default argument for
arg.names, but this variable is defined inside of
Vectorize(), so if you supply it explicitly you get an error.
rbeta(), the default value of
ncpis 0, but if you explicitly supply it the function uses a different algorithm:
table(), the default value of
list.names()is only defined inside of
progress = show_progress(), but until version 1.3.1,
show_progress()was not exported from the package. That means if you attempted to run it yourself, you’d see an error message:
template_dirhas the default value of
asciifyis not exported. That means there’s no way to interactively explore this default value.
13.3 What are the exceptions?
It’s ok to use this behaviour when you want the default value of one argument to be the same as another. For example, take
rlang::set_names(), which allows you to create a named vector from two inputs:
The default value for the names is the vector itself. This provides a convenient shortcut for naming a vector with itself:
You can see this same technique in
all.y default to the same value as
all, and in
labels defaults to the same value as
If you use this technique, make sure that you never use the value of an argument that comes later in the argument list. For example, in
overwrite defaults to the same value as
recursive, but the
recursive argument is defined after
This makes the defaults arguments harder to understand because you can’t just read from left-to-right.
13.4 What causes the problem?
There are three primary causes:
Overuse of lazy evaluation of default values, which are evaluated in the environment of the function, as described in Advanced R. Here’s a simple example:
xtakes the value
yfrom its default, it’s evaluated inside the function, yielding
yis supplied explicitly, it is evaluated in the caller environment, yielding
missing()so that the default value is never consulted:
In packages, it’s easy to use a non-exported function without thinking about it. This function is available to you, the package author, but not the user of the package, which makes it harder for them to understand how a package works.
13.5 How do I remediate the problem?
This problem is generally easy to avoid for new functions:
Don’t use default values that depend on variables defined inside the function.
Don’t use unexported functions.
If you have a made a mistake in an older function you can remediate it by using a
NULL default, as described in Chapter 14. If the problem is caused by an unexported function, you can also choose to document and export it.
This modification should not break existing code, because expands the function interface: all previous code will continue to work, and the function will also work if the argument is passed
NULL input (which probably didn’t previously).
For functions like
NULL is already a permissible value, you’ll need to use a sentinel object, as described in Section 14.4.3.