The goal of strict to make R behave a little more strictly, making base functions more likely to throw an error rather than returning potentially ambiguous results.
library(strict)
forces you to confront potential problems now, instead of in the future. This has both pros and cons: often you can most easily fix a potential ambiguity when you're working on the code (rather than in six months time when you've forgotten how it works), but it also forces you to resolve ambiguities that might never occur with your code/data.
# install.packages("devtools")
devtools::install_github("hadley/strict")
library(strict)
affects code in the current script/session only (i.e. it doesn't affect code in others packages).
An alternative conflict resolution mechanism. Instead of warning about conflicts on package load and letting the last loaded package win, strict throws an error when you access ambiguous functions:
``` r library(strict) library(plyr) library(Hmisc)
is.discrete
is.discrete
.```
(Thanks to @krlmlr for this neat idea!)
Shims for functions with "risky" arguments, i.e. arguments that either rely on global options (like stringsAsFactors
) or have computed defaults that 90% evaluate to one thing (like drop
). strict forces you to supply values for these arguments.
``` r library(strict) mtcars[, 1]
drop
when selecting a single columndata.frame(x = "a")
stringsAsFactors
argument.```
Automatically sets options to warn when partial matching occurs.
``` r library(strict)
df <- data.frame(xyz = 1) df$x
$.data.frame
(df, x): Partial match of 'x' to 'xyz' in data```
T
and F
generate errors, forcing you to use TRUE
and FALSE
.
``` r library(strict) T
```
sapply()
throws an error suggesting that you use the type-safe vapply()
instead. apply()
throws an error if you use it with a data frame.
``` r library(strict) sapply(1:10, sum)
vapply()
instead of sapply()
.```
:
will throw an error instead of creating a decreasing sequence that terminates in 0.
``` r library(strict)
x <- numeric() 1:length(x)
seq_along()
instead?```
diag()
and sample()
throw an error if given scalar x
. This avoids an otherwise unpleasant surprise.
``` r library(strict)
sample(5:3)
sample(5:4)
lax(sample(5:5))
sample(5:5)
sample()
has surprising behaviour when x
is a scalar.sample.int()
instead.```
Once strict is loaded, you can continue to run code in a lax manner using lax()
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.