Skip to content

Commit

Permalink
Update linter (#170)
Browse files Browse the repository at this point in the history
* Update lint config

* Fix object_usage_liner problems

- local variable 'classic' assigned but may not be used

excel_themes.R

Replace tidyverse package with specific packages

- no visible global funtion definition for `set_names`
- no visible global funtion definition for `flatten_chr`
- no visible global funtion definition for `str_subset`

libreoffice_palettes.R

Replace tidyverse package with specific packages

- no visible global funtion definition for `tibble`
- local variable 'name' assigned but mau not be used

tests/testthat/helper-utils.R

Add testthat library explicitly

* Ignore examples with dontrun

They cause unexpected symbol lint errors.

* Update docs

* Fix line length lint issues

- Set line length to 120
- Ignore or fix remaining cases

* Fix infix_spaces_linter warnings

Put spaces around all infix operators

* Fix indentation issues

Style the examples while ignoring those
examples with `\dontrun`.

* Remove end-of-file-fixer pre-commit

It was screwing up .lintr file which requires
a newline, and not adding much benefit otherwise.

* Remove indentation_linter

Indentation formatting is handled by styler.

* Fix misc linter

* Fix warning

* Add lint

* Add lint github action

* Try to fix unfound symbol warning

* Misc updates

* Updating

* Fixing
  • Loading branch information
jrnold authored Feb 9, 2024
1 parent 61fccbe commit ff59831
Show file tree
Hide file tree
Showing 74 changed files with 492 additions and 343 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
^CONTRIBUTING.*$
^\.pre-commit-config\.yaml$
^CRAN-SUBMISSION$
^scripts$
32 changes: 32 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: lint

jobs:
lint:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::lintr, local::.
needs: lint

- name: Lint
run: lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
21 changes: 19 additions & 2 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
linters: with_defaults(commented_code_linter = NULL, snake_case_linter = NULL, camel_case_linter = NULL, line_length_linter(80L), multiple_dots_linter = NULL)
exclusions: list("./inst/examples/ex-palette_pander.R", "./inst/examples/ex-theme_economist.R")
linters: linters_with_defaults(
line_length_linter(120),
commented_code_linter = NULL,
indentation_linter = NULL
)
encoding: "UTF-8"
exclusions: list(
"./inst/exampels/ex-few_shape_pal.R",
"./inst/examples/ex-calc_shape_pal.R",
"./inst/examples/ex-cleveland_shape_pal.R",
"./inst/examples/ex-few_shape_pal.R",
"./inst/examples/ex-palette_pander.R",
"./inst/examples/ex-scale_shape_stata.R",
"./inst/examples/ex-scale_shape_tableau.R",
"./inst/examples/ex-tableau_shape_pal.R",
"./inst/examples/ex-tableau_shape_pal.R",
"./inst/examples/ex-theme_calc.R",
"./inst/examples/ex-theme_economist.R"
)
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ repos:
rev: 'v4.5.0'
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: 'no-commit-to-branch'
args: ['--branch', 'main']
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ data:

.PHONY: style
style:
$(RSCRIPT) -e 'styler::style_pkg(".", style = styler::tidyverse_style, strict = TRUE)'
$(RSCRIPT) scripts/style.R

.PHONY: lint
lint:
$(RSCRIPT) -e 'devtools::lint()'
6 changes: 3 additions & 3 deletions R/banking.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## 45 degrees in radians
FORTY_FIVE <- base::pi / 4
FORTY_FIVE <- base::pi / 4 # nolint: object_name_linter

calc_slopes <- function(x, y, cull = FALSE) {
dx <- abs(diff(x))
Expand Down Expand Up @@ -102,9 +102,9 @@ calc_slopes <- function(x, y, cull = FALSE) {
bank_slopes <- function(x, y, cull = FALSE, weight = NULL,
method = c("ms", "as"), ...) {
method <- match.arg(method)
FUN <- bank_slopes_funs[[method]]
fun <- bank_slopes_funs[[method]]
# Heer produces functions with the target alpha = w/h = x/y
xyrat <- FUN(calc_slopes(x, y, cull = cull), ...)
xyrat <- fun(calc_slopes(x, y, cull = cull), ...)
# but coord_fixed ratio is the aspect ratio y/x
1 / xyrat
}
Expand Down
2 changes: 2 additions & 0 deletions R/canva.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# nolint start
#' 150 Color Palettes from Canva
#'
#' 150 four-color palettes by the
Expand All @@ -20,6 +21,7 @@
#' }
#' @example inst/examples/ex-canva_pal.R
"canva_palettes"
# nolint end

#' Canva.com color palettes
#'
Expand Down
2 changes: 2 additions & 0 deletions R/few.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# nolint start
#' Color Palettes Few "Show Me the Numbers"
#'
#' Qualitative color palettes from Stephen Few (2012)
Expand All @@ -23,6 +24,7 @@
#' @param palette One of \Sexpr[results=rd]{names(ggthemes:::rd_optlist(ggthemes::ggthemes_data$few$colors))}
#' @family colour few
#' @example inst/examples/ex-few_pal.R
# nolint end
few_pal <- function(palette = "Medium") {
palette <- ggthemes::ggthemes_data$few$colors[[palette]]
if (is.null(palette)) {
Expand Down
10 changes: 6 additions & 4 deletions R/geom-rangeframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ geom_rangeframe <- function(mapping = NULL,
position = "identity",
...,
sides = "bl",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
na.rm = FALSE, # nolint: object_name_linter
show.legend = NA, # nolint: object_name_linter
inherit.aes = TRUE # nolint: object_name_linter
) {
layer(
data = data,
mapping = mapping,
Expand All @@ -56,7 +57,8 @@ geom_rangeframe <- function(mapping = NULL,
#' @export
#' @importFrom ggplot2 Geom
#' @importFrom scales alpha
GeomRangeFrame <- ggplot2::ggproto("GeomRangeFrame", ggplot2::Geom,
GeomRangeFrame <- ggplot2::ggproto("GeomRangeFrame", # nolint: object_name_linter
ggplot2::Geom,
optional_aes = c("x", "y"),
draw_panel = function(data, panel_scales, coord, sides = "bl") {
rugs <- list()
Expand Down
34 changes: 18 additions & 16 deletions R/geom-tufteboxplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ geom_tufteboxplot <-
data = NULL,
stat = "fivenumber",
position = "dodge",
outlier.colour = "black",
outlier.shape = 19,
outlier.size = 1.5,
outlier.stroke = 0.5,
outlier.colour = "black", # nolint: object_name_linter
outlier.shape = 19, # nolint: object_name_linter
outlier.size = 1.5, # nolint: object_name_linter
outlier.stroke = 0.5, # nolint: object_name_linter
voffset = 0.01,
hoffset = 0.005,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
median.type = "point",
whisker.type = "line",
na.rm = FALSE, # nolint: object_name_linter
show.legend = NA, # nolint: object_name_linter
inherit.aes = TRUE, # nolint: object_name_linter
median.type = "point", # nolint: object_name_linter
whisker.type = "line", # nolint: object_name_linter
...) {
layer(
data = data,
Expand Down Expand Up @@ -111,7 +111,7 @@ geom_tufteboxplot <-
#' @importFrom ggplot2 draw_key_pointrange ggproto_parent GeomBoxplot GeomSegment GeomPoint
#' @importFrom scales alpha
#' @importFrom grid grobTree
GeomTufteboxplot <-
GeomTufteboxplot <- # nolint: object_name_linter
ggplot2::ggproto("GeomTufteboxplot",
ggplot2::GeomBoxplot,
setup_data = function(self, data, params) {
Expand All @@ -123,15 +123,17 @@ GeomTufteboxplot <-
data
},
draw_group = function(data, panel_scales, coord, fatten = 2,
outlier.colour = "black", outlier.shape = 19,
outlier.size = 1.5, outlier.stroke = 0.5,
outlier.colour = "black", # nolint: object_name_linter
outlier.shape = 19, # nolint: object_name_linter
outlier.size = 1.5, # nolint: object_name_linter
outlier.stroke = 0.5, # nolint: object_name_linter
varwidth = FALSE,
median.type = c("point", "line"),
whisker.type = c("line", "point"),
median.type = c("point", "line"), # nolint: object_name_linter
whisker.type = c("line", "point"), # nolint: object_name_linter
hoffset = 0.01,
voffset = 0.01) {
median.type <- match.arg(median.type)
whisker.type <- match.arg(whisker.type)
median.type <- match.arg(median.type) # nolint: object_name_linter
whisker.type <- match.arg(whisker.type) # nolint: object_name_linter

common <- data.frame(
colour = data$colour,
Expand Down
9 changes: 5 additions & 4 deletions R/pander.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' A ggplot theme originated from the pander package
#'
#' The \pkg{pander} ships with a default theme when the 'unify plots' option is
#' enabled via \code{panderOptions}, which is now also available outside of \pkg{pander} internals, like \code{evals}, \code{eval.msgs} or \code{Pandoc.brew}.
#' enabled via \code{panderOptions}, which is now also available outside of \pkg{pander} internals, like \code{evals},
#' \code{eval.msgs} or \code{Pandoc.brew}.
#' @inheritParams ggplot2::theme_bw
#' @param nomargin suppress the white space around the plot (boolean)
#' @param ff font family, like \code{sans}. Deprecated: use \code{base_family} instead.
Expand All @@ -18,13 +19,13 @@
#' @param axis axis angle as defined in \code{par(les)}
#' @export
#' @example inst/examples/ex-theme_pander.R
theme_pander <- function(base_size = 12,
theme_pander <- function(base_size = 12, # nolint: cyclocomp_linter
base_family = "sans",
nomargin = TRUE,
ff = NULL,
fc = "black",
fs = NULL,
gM = TRUE,
gM = TRUE, # nolint: object_name_linter
gm = TRUE,
gc = "grey",
gl = "dashed",
Expand Down Expand Up @@ -57,7 +58,7 @@ theme_pander <- function(base_size = 12,
base_size <- pander::panderOptions("graph.fontsize")
}
if (missing(gM)) {
gM <- pander::panderOptions("graph.grid")
gM <- pander::panderOptions("graph.grid") # nolint: object_name_linter
}
if (missing(gm)) {
gm <- pander::panderOptions("graph.grid.minor")
Expand Down
34 changes: 20 additions & 14 deletions R/scales.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Much of this code is copied from the labeling package.
.simplicity <- function(q, Q, j, lmin, lmax, lstep) {
.simplicity <- function(q, Q, j, lmin, lmax, lstep) { # nolint: object_name_linter
eps <- .Machine$double.eps * 100

n <- length(Q)
Expand All @@ -10,7 +10,7 @@
1 - (i - 1) / (n - 1) - j + v
}

.simplicity.max <- function(q, Q, j) {
.simplicity_max <- function(q, Q, j) { # nolint: object_name_linter
n <- length(Q)
i <- match(q, Q)[1]
v <- 1
Expand All @@ -22,7 +22,7 @@
1 - 0.5 * ((dmax - lmax)^2 + (dmin - lmin)^2) / ((0.1 * range)^2)
}

.coverage.max <- function(dmin, dmax, span) {
.coverage_max <- function(dmin, dmax, span) {
range <- dmax - dmin
if (span > range) {
half <- (span - range) / 2
Expand All @@ -38,7 +38,7 @@
2 - max(r / rt, rt / r)
}

.density.max <- function(k, m) {
.density_max <- function(k, m) {
if (k >= m) {
2 - (k - 1) / (m - 1)
} else {
Expand All @@ -52,9 +52,11 @@

#' Pretty axis breaks inclusive of extreme values
#'
#' This function returns pretty axis breaks that always include the extreme values of the data.
#' This works by calling the extended Wilkinson algorithm (Talbot et. al, 2010), constrained to solutions interior to the data range.
#' Then, the minimum and maximum labels are moved to the minimum and maximum of the data range.
#' This function returns pretty axis breaks that always include the extreme values of the data.
#' This works by calling the extended Wilkinson algorithm (Talbot et al., 2010), constrained to solutions
#' interior to the data range.
#' Then, the minimum and maximum labels are moved to the minimum and maximum of the data
#' range.
#'
#' \code{extended_range_breaks} implements the algorithm and returns the break values.
#' \code{scales_extended_range_breaks} uses the conventions of the \pkg{scales} package, and returns a function.
Expand All @@ -65,14 +67,16 @@
#' @param Q set of nice numbers
#' @param w weights applied to the four optimization components (simplicity, coverage, density, and legibility)
#' @return For \code{extended_range_breaks}, the vector of axis label locations.
#' For \code{scales_extended_range_breaks}, a function which takes a single argument, a vector of data, and returns the vector of axis label locations.
#' For \code{scales_extended_range_breaks}, a function which takes a single argument, a vector of data, and returns
#' the vector of axis label locations.
#' @references
#' Talbot, J., Lin, S., Hanrahan, P. (2010) An Extension of Wilkinson's Algorithm for Positioning Tick Labels on Axes, InfoVis 2010.
#' Talbot, J., Lin, S., Hanrahan, P. (2010) An Extension of Wilkinson's Algorithm
#' for Positioning Tick Labels on Axes, InfoVis 2010.
#' @author Justin Talbot \email{jtalbot@@stanford.edu}, Jeffrey B. Arnold, Baptiste Auguie
#' @rdname range_breaks
#' @export
extended_range_breaks_ <- function(dmin, dmax, n = 5,
Q = c(1, 5, 2, 2.5, 4, 3),
extended_range_breaks_ <- function(dmin, dmax, n = 5, # nolint: cyclocomp_linter
Q = c(1, 5, 2, 2.5, 4, 3), # nolint: object_name_linter
w = c(0.25, 0.2, 0.5, 0.05)) {
eps <- .Machine$double.eps * 100

Expand All @@ -96,7 +100,7 @@ extended_range_breaks_ <- function(dmin, dmax, n = 5,
j <- 1
while (j < Inf) {
for (q in Q) {
sm <- .simplicity.max(q, Q, j)
sm <- .simplicity_max(q, Q, j)

if ((w[1] * sm + w[2] + w[3] + w[4]) < best$score) {
j <- Inf
Expand All @@ -105,7 +109,7 @@ extended_range_breaks_ <- function(dmin, dmax, n = 5,

k <- 2
while (k < Inf) {
dm <- .density.max(k, n)
dm <- .density_max(k, n)
if ((w[1] * sm + w[2] + w[3] * dm + w[4]) < best$score) {
break
}
Expand All @@ -116,7 +120,7 @@ extended_range_breaks_ <- function(dmin, dmax, n = 5,
while (z < Inf) {
step <- j * q * 10^z

cm <- .coverage.max(dmin, dmax, step * (k - 1))
cm <- .coverage_max(dmin, dmax, step * (k - 1))

if ((w[1] * sm + w[2] * cm + w[3] * dm + w[4]) < best$score) {
break
Expand Down Expand Up @@ -213,6 +217,7 @@ precision <- function(x) {
10^floor(log10(span))
}

# nolint start
#' Format numbers with automatic number of digits
#'
#' @param x A numeric vector to format
Expand All @@ -227,6 +232,7 @@ precision <- function(x) {
#'
#' @rdname smart_digits
#' @export
# nolint end
smart_digits <- function(x, ...) {
if (length(x) == 0) {
return(character())
Expand Down
2 changes: 2 additions & 0 deletions R/shapes.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# nolint start
#' Shape palette from Cleveland "Elements of Graphing Data" (discrete).
#'
#' Shape palettes for overlapping and non-overlapping points.
Expand Down Expand Up @@ -32,6 +33,7 @@
#'
#' @family shapes
#' @export
# nolint end
cleveland_shape_pal <- function(overlap = TRUE) {
shapes <- if (overlap[[1]]) {
ggthemes::ggthemes_data$shapes$cleveland$overlap$pch
Expand Down
2 changes: 1 addition & 1 deletion R/show.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ show_shapes <- function(shapes, labels = TRUE) {
nrow <- ceiling(n / ncol)
x <- c(shapes, rep(NA, nrow * ncol - length(shapes)))
x <- matrix(x, ncol = ncol, byrow = TRUE)
x <- x[nrow(x):1, ]
x <- x[rev(seq_len(nrow(x))), ]
plot(0, 0,
xlim = c(1, ncol(x)), ylim = c(1, nrow(x)), type = "n",
xlab = "", ylab = "", axes = FALSE
Expand Down
Loading

0 comments on commit ff59831

Please sign in to comment.