From 62fca2a4f3ec4f669141e4da2c465d558ea58f60 Mon Sep 17 00:00:00 2001 From: Cam Race <52536248+cjrace@users.noreply.github.com> Date: Sat, 10 Feb 2024 12:59:36 +0000 Subject: [PATCH] add lintr details to contributing (#58) * add lintr details to contributing * fix linting issues --- .github/CONTRIBUTING.md | 16 ++++++++++++++-- NAMESPACE | 4 ++-- R/{formatAY.R => format_ay.R} | 13 +++++++------ R/{roundFiveUp.R => round_five_up.R} | 6 +++--- man/{formatAY.Rd => format_ay.Rd} | 14 +++++++------- man/{roundFiveUp.Rd => round_five_up.Rd} | 12 ++++++------ tests/testthat/test-formatAY.R | 13 ------------- tests/testthat/test-format_ay.R | 13 +++++++++++++ ...test-roundFiveUp.R => test-round_five_up.R} | 18 +++++++++--------- 9 files changed, 61 insertions(+), 48 deletions(-) rename R/{formatAY.R => format_ay.R} (65%) rename R/{roundFiveUp.R => round_five_up.R} (91%) rename man/{formatAY.Rd => format_ay.Rd} (73%) rename man/{roundFiveUp.Rd => round_five_up.Rd} (80%) delete mode 100644 tests/testthat/test-formatAY.R create mode 100644 tests/testthat/test-format_ay.R rename tests/testthat/{test-roundFiveUp.R => test-round_five_up.R} (56%) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 92dd901..4a627d2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -74,9 +74,21 @@ Once you've incremented the version number, it'll offer to perform a commit on y ### Code style -New code should follow the tidyverse [style guide](https://style.tidyverse.org). +New code should follow the tidyverse [style guide](https://style.tidyverse.org). We use [lintr](https://lintr.r-lib.org/articles/lintr.html) to scan styling on pull requests, this will automatically run and add comments for any code that is failing the standards we'd expect. Where these happen, please proactively resolve these as we are unlikely to approve pull requests that have styling issues. -You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles. +You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply most of the styling using: + +``` r +styler::style_pkg() +``` + +To check for any further styling issues locally, use: + +``` r +lintr::lint_package() +``` + +[styler](https://CRAN.R-project.org/package=styler) will not fix all linting issues, so we recommend using that first, then using [lintr](https://lintr.r-lib.org/articles/lintr.html) to check for places you may need to manually fix styling issues such as line length or not using snake_case. We use [testthat](https://cran.r-project.org/package=testthat) for unit tests, we expect all new functions to have some level of test coverage. diff --git a/NAMESPACE b/NAMESPACE index 19cab5d..c00d9fd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,5 @@ # Generated by roxygen2: do not edit by hand -export(formatAY) -export(roundFiveUp) +export(format_ay) +export(round_five_up) importFrom(lifecycle,deprecated) diff --git a/R/formatAY.R b/R/format_ay.R similarity index 65% rename from R/formatAY.R rename to R/format_ay.R index 34c3645..5b684be 100644 --- a/R/formatAY.R +++ b/R/format_ay.R @@ -1,7 +1,7 @@ #' Format academic year #' #' This function formats academic year variables for reporting purposes. It -#' will convert an academic year input from 201516 format to 2015/16 format. \cr\cr +#' will convert an academic year input from 201516 format to 2015/16 format. #' #' It accepts both numerical and character arguments. #' @@ -9,10 +9,11 @@ #' @return Character vector of formatted academic year #' @export #' @examples -#' formatAY(201617) -#' formatAY("201617") -formatAY <- function(year) { - if (!grepl("^[0-9]{6,6}$", year)) stop("year parameter must be a six digit number e.g. 201617") - +#' format_ay(201617) +#' format_ay("201617") +format_ay <- function(year) { + if (!grepl("^[0-9]{6,6}$", year)) { + stop("year parameter must be a six digit number e.g. 201617") + } sub("(.{4})(.*)", "\\1/\\2", year) } diff --git a/R/roundFiveUp.R b/R/round_five_up.R similarity index 91% rename from R/roundFiveUp.R rename to R/round_five_up.R index 990e0a1..0e02073 100644 --- a/R/roundFiveUp.R +++ b/R/round_five_up.R @@ -18,9 +18,9 @@ #' @export #' #' @examples -#' roundFiveUp(2495, -1) -#' roundFiveUp(2495.85, 1) -roundFiveUp <- function(value, dp) { +#' round_five_up(2495, -1) +#' round_five_up(2495.85, 1) +round_five_up <- function(value, dp) { if (!is.numeric(value) && !is.numeric(dp)) stop("both inputs must be numeric") if (!is.numeric(value)) stop("the value to be rounded must be numeric") if (!is.numeric(dp)) stop("the decimal places value must be numeric") diff --git a/man/formatAY.Rd b/man/format_ay.Rd similarity index 73% rename from man/formatAY.Rd rename to man/format_ay.Rd index 67423ab..6c8bc4f 100644 --- a/man/formatAY.Rd +++ b/man/format_ay.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/formatAY.R -\name{formatAY} -\alias{formatAY} +% Please edit documentation in R/format_ay.R +\name{format_ay} +\alias{format_ay} \title{Format academic year} \usage{ -formatAY(year) +format_ay(year) } \arguments{ \item{year}{Academic year} @@ -14,12 +14,12 @@ Character vector of formatted academic year } \description{ This function formats academic year variables for reporting purposes. It -will convert an academic year input from 201516 format to 2015/16 format. \cr\cr +will convert an academic year input from 201516 format to 2015/16 format. } \details{ It accepts both numerical and character arguments. } \examples{ -formatAY(201617) -formatAY("201617") +format_ay(201617) +format_ay("201617") } diff --git a/man/roundFiveUp.Rd b/man/round_five_up.Rd similarity index 80% rename from man/roundFiveUp.Rd rename to man/round_five_up.Rd index 6237de7..e32006d 100644 --- a/man/roundFiveUp.Rd +++ b/man/round_five_up.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/roundFiveUp.R -\name{roundFiveUp} -\alias{roundFiveUp} +% Please edit documentation in R/round_five_up.R +\name{round_five_up} +\alias{round_five_up} \title{Round five up} \usage{ -roundFiveUp(value, dp) +round_five_up(value, dp) } \arguments{ \item{value}{number to be rounded} @@ -28,6 +28,6 @@ You can use a negative value for the decimal places. For example: and so on. } \examples{ -roundFiveUp(2495, -1) -roundFiveUp(2495.85, 1) +round_five_up(2495, -1) +round_five_up(2495.85, 1) } diff --git a/tests/testthat/test-formatAY.R b/tests/testthat/test-formatAY.R deleted file mode 100644 index 82542a2..0000000 --- a/tests/testthat/test-formatAY.R +++ /dev/null @@ -1,13 +0,0 @@ -test_that("Rejects non 6-digit numbers", { - expect_error(formatAY(19858)) - expect_error(formatAY(1985)) - expect_error(formatAY(1985867)) - expect_error(formatAY("1999c")) - expect_error(formatAY("abcdef")) - expect_error(formatAY("1985-98")) -}) - -test_that("Converts correctly", { - expect_equal(formatAY(199920), "1999/20") - expect_equal(formatAY("199920"), "1999/20") -}) diff --git a/tests/testthat/test-format_ay.R b/tests/testthat/test-format_ay.R new file mode 100644 index 0000000..4c54a8d --- /dev/null +++ b/tests/testthat/test-format_ay.R @@ -0,0 +1,13 @@ +test_that("Rejects non 6-digit numbers", { + expect_error(format_ay(19858)) + expect_error(format_ay(1985)) + expect_error(format_ay(1985867)) + expect_error(format_ay("1999c")) + expect_error(format_ay("abcdef")) + expect_error(format_ay("1985-98")) +}) + +test_that("Converts correctly", { + expect_equal(format_ay(199920), "1999/20") + expect_equal(format_ay("199920"), "1999/20") +}) diff --git a/tests/testthat/test-roundFiveUp.R b/tests/testthat/test-round_five_up.R similarity index 56% rename from tests/testthat/test-roundFiveUp.R rename to tests/testthat/test-round_five_up.R index 52323e4..01f8389 100644 --- a/tests/testthat/test-roundFiveUp.R +++ b/tests/testthat/test-round_five_up.R @@ -1,32 +1,32 @@ test_that("Rounds fives up", { - expect_equal(roundFiveUp(285, -1), 290) - expect_equal(roundFiveUp(2.85, 1), 2.9) + expect_equal(round_five_up(285, -1), 290) + expect_equal(round_five_up(2.85, 1), 2.9) }) test_that("Rounds other numbers", { - expect_equal(roundFiveUp(283, -1), 280) - expect_equal(roundFiveUp(2.87, 1), 2.9) + expect_equal(round_five_up(283, -1), 280) + expect_equal(round_five_up(2.87, 1), 2.9) }) test_that("Input validation", { expect_error( - roundFiveUp("ten", "10"), + round_five_up("ten", "10"), "both inputs must be numeric" ) expect_error( - roundFiveUp(12, "ten"), + round_five_up(12, "ten"), "the decimal places value must be numeric" ) expect_error( - roundFiveUp(12, "10"), + round_five_up(12, "10"), "the decimal places value must be numeric" ) expect_error( - roundFiveUp("twelve", 10), + round_five_up("twelve", 10), "the value to be rounded must be numeric" ) expect_error( - roundFiveUp("12", 10), + round_five_up("12", 10), "the value to be rounded must be numeric" ) })