https://github.com/hadley/strict
Make R a little bit stricter
https://github.com/hadley/strict
Last synced: 2 months ago
JSON representation
Make R a little bit stricter
- Host: GitHub
- URL: https://github.com/hadley/strict
- Owner: hadley
- Created: 2017-05-23T21:01:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-12-04T14:22:03.000Z (over 4 years ago)
- Last Synced: 2025-02-04T00:37:10.032Z (2 months ago)
- Language: R
- Homepage:
- Size: 34.2 KB
- Stars: 236
- Watchers: 11
- Forks: 10
- Open Issues: 26
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
- jimsghstars - hadley/strict - Make R a little bit stricter (R)
README
---
output: github_document
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
set.seed(1014)
```# strict
[](https://travis-ci.org/hadley/strict)
[](https://cran.r-project.org/package=strict)
[](https://codecov.io/github/hadley/strict?branch=master)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 your 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.
## Installation
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("hadley/strict")
```## Features
`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, error = TRUE}
library(strict)
library(plyr)
library(Hmisc)
is.discrete
```
(Thanks to @[krlmlr](https://github.com/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, error = TRUE}
library(strict)
mtcars[, 1]data.frame(x = "a")
```* Automatically sets options to warn when partial matching occurs.
```{r}
library(strict)
df <- data.frame(xyz = 1)
df$x
```* `T` and `F` generate errors, forcing you to use `TRUE` and `FALSE`.
```{r, error = TRUE}
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, error = TRUE}
library(strict)
sapply(1:10, sum)
```* `:` will throw an error instead of creating a decreasing sequence that
terminates in 0.```{r, error = TRUE}
library(strict)
x <- numeric()
1:length(x)
```* `diag()` and `sample()` throw an error if given scalar `x`. This avoids
an otherwise unpleasant surprise.
```{r, error = TRUE}
library(strict)
sample(5:3)
sample(5:4)
lax(sample(5:5))
sample(5:5)
```Once strict is loaded, you can continue to run code in a lax manner using `lax()`.