args(data.frame)
#> function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
#> fix.empty.names = TRUE, stringsAsFactors = FALSE)
#> NULL
x <- setNames(nm = letters[1:2])
x
#> a b
#> "a" "b"
data.frame(x)
#> x
#> a a
#> b b
data.frame(x, row.names = NULL)
#> x
#> 1 a
#> 2 b
Avoid magical defaults
What’s the problem?
If a function behaves differently when the default value is supplied explicitly, we say it has a magical default. Magical defaults are best avoided because they make it harder to interpret the function specification.
What are some examples?
-
In
data.frame()
, the default argument forrow.names
isNULL
, but if you supply it directly you get a different result: -
In
hist()
, the default value ofxlim
isrange(breaks)
, and the default value forbreaks
is"Sturges"
.range("Sturges")
returnsc("Sturges", "Sturges")
which doesn’t work when supplied explicitly:args(hist.default) #> function (x, breaks = "Sturges", freq = NULL, probability = !freq, #> include.lowest = TRUE, right = TRUE, fuzz = 1e-07, 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, ...) #> NULL hist(1:10, xlim = c("Sturges", "Sturges")) #> Error in plot.window(xlim, ylim, "", ...): invalid 'xlim' value
-
readr::read_csv()
hasprogress = 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:show_progress() #> Error in show_progress(): could not find function "show_progress"
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:
set_names(letters[1:3])
#> a b c
#> "a" "b" "c"
You can see this same technique in merge()
, where all.x
and all.y
default to the same value as all
, and in factor()
where labels
defaults to the same value as levels
.
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 file.copy()
overwrite
defaults to the same value as recursive
, but the recursive
argument is defined after overwrite
:
args(file.copy)
#> function (from, to, overwrite = recursive, recursive = FALSE,
#> copy.mode = TRUE, copy.date = FALSE)
#> NULL
This makes the defaults arguments harder to understand because you can’t just read from left-to-right.
What causes 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. The default values of function arguments are lazily evaluated in the environment of the function when they are first used, as described in Advanced R. Here’s a simple example:
f1 <- function(x = y) { y <- 2 x } y <- 1 f1() #> [1] 2 f1(y) #> [1] 1
When
x
takes the valuey
from its default, it’s evaluated inside the function, yielding1
. Wheny
is supplied explicitly, it is evaluated in the caller environment, yielding2
. -
Don’t use
missing()
[^def-magical-1].f2 <- function(x = 1) { if (missing(x)) { 2 } else { x } } f2() #> [1] 2 f2(1) #> [1] 1
Don’t use unexported functions. 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.
How do I remediate the problem?
If you have a made a mistake in an older function you can remediate it by using a NULL
default, as described in Chapter 9). If the problem is caused by an unexported function, you can also choose to document and export it. Remediating this problem shouldn’t break existing code, because it 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).