From 6d90c34113dee87749170ecda15b1e3da368eda4 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:36:38 +0200 Subject: [PATCH] Run styler --- R/data.R | 6 ++-- R/expand_names.R | 10 +++--- R/parse_authors.R | 49 +++++++++++++-------------- R/parse_authors_r.R | 22 ++++++------ R/remove_brackets.R | 23 ++++++------- data-raw/cran_epidemiology_packages.R | 8 ++--- man/authoritative-package.Rd | 2 +- tests/testthat/test-dev-utils.R | 2 -- tests/testthat/test-expand_names.R | 12 ++----- tests/testthat/test-parse_authors.R | 6 ++-- tests/testthat/test-parse_authors_R.R | 6 ++-- tests/testthat/test-remove_brackets.R | 10 ++---- 12 files changed, 67 insertions(+), 89 deletions(-) diff --git a/R/data.R b/R/data.R index 8e18f84..743a3af 100644 --- a/R/data.R +++ b/R/data.R @@ -1,12 +1,12 @@ #' A data.frame of historical metadata from CRAN packages epidemiology. -#' +#' #' @format A data.frame with 5 variables: #' \describe{ #' \item{Package}{package name} #' \item{Version}{package version} -#' \item{Authors@R}{authors as listed in the `Authors@R` field from the +#' \item{Authors@R}{authors as listed in the `Authors@R` field from the #' `DESCRIPTION` file} -#' \item{Author}{authors as listed in the `Author` field from the +#' \item{Author}{authors as listed in the `Author` field from the #' `DESCRIPTION` file} #' \item{Maintainer}{package maintainer} #' } diff --git a/R/expand_names.R b/R/expand_names.R index b51a37a..b76791d 100644 --- a/R/expand_names.R +++ b/R/expand_names.R @@ -1,13 +1,13 @@ #' Expand names from abbreviated forms or initials -#' +#' #' @param short A character vector of potentially abbreviated names #' @param expanded A character vector of potentially expanded names -#' +#' #' @details #' When you have a list `x`of abbreviated and non-abbreviated names and you want #' to deduplicate them, this function can be used as `expand_names(x, x)`, which #' will return the most expanded version available in `x` for each name -#' +#' #' @return A character vector with the same length as `short` #' #' @export @@ -66,8 +66,8 @@ expand_names <- function(short, expanded) { } min(x, na.rm = TRUE) }) - + short[!is.na(longest_match)] <- expanded[longest_match[!is.na(longest_match)]] - + return(short) } diff --git a/R/parse_authors.R b/R/parse_authors.R index 5c035b0..78ee135 100644 --- a/R/parse_authors.R +++ b/R/parse_authors.R @@ -1,63 +1,62 @@ #' Parse the `Author` field from a DESCRIPTION file -#' +#' #' Parse the `Author` field from a DESCRIPTION file into a `person` object #' -#' @param author_string A character containing the `Author` or `Maintainer` +#' @param author_string A character containing the `Author` or `Maintainer` #' field from a `DESCRIPTION` file #' #' @return A character vector, or a list of character vectors of length equals #' to the length of `author_string` -#' +#' #' @importFrom utils as.person -#' +#' #' @examples #' # Read from a DESCRIPTION file directly #' utils_description <- system.file("DESCRIPTION", package = "utils") #' utils_authors <- read.dcf(utils_description, "Author") -#' +#' #' parse_authors(utils_authors) -#' +#' #' # Read from a database of CRAN metadata -#' cran_epidemiology_packages$Author |> -#' parse_authors() |> -#' unlist() |> -#' unique() |> +#' cran_epidemiology_packages$Author |> +#' parse_authors() |> +#' unlist() |> +#' unique() |> #' sort() -#' +#' #' @export parse_authors <- function(author_string) { # Sanitize input from pkgsearch / crandb - author_string <- author_string |> + author_string <- author_string |> stringi::stri_replace_all_fixed( "", " " ) - - authors_no_brackets <- author_string |> - remove_brackets("(") |> + + authors_no_brackets <- author_string |> + remove_brackets("(") |> remove_brackets("[") |> remove_brackets("<") - authors_person <- authors_no_brackets |> + authors_person <- authors_no_brackets |> # Extra common strings stringi::stri_replace_all_regex("\\bet\\.? al\\.?\\b", " ") |> - stringi::stri_replace_all_regex("\\b(Prof|Dr|Mr|Mrs|Ms)\\b", "") |> + stringi::stri_replace_all_regex("\\b(Prof|Dr|Mr|Mrs|Ms)\\b", "") |> # Separators stringi::stri_replace_all_regex("\\s+", " ") |> - stringi::stri_replace_all_regex("\\b(with contributions?|contributed datasets) (of|from|by)\\b:?", ", ") |> - stringi::stri_replace_all_regex("\\band\\b", ", ") |> - stringi::stri_split_regex("\\s*(,\\s*)+") |> + stringi::stri_replace_all_regex("\\b(with contributions?|contributed datasets) (of|from|by)\\b:?", ", ") |> + stringi::stri_replace_all_regex("\\band\\b", ", ") |> + stringi::stri_split_regex("\\s*(,\\s*)+") |> # Clean string boundaries - lapply(function(x) stringi::stri_replace_all_regex(x, "\\.$", "")) |> - lapply(trimws) |> + lapply(function(x) stringi::stri_replace_all_regex(x, "\\.$", "")) |> + lapply(trimws) |> # For strings terminating with "," lapply(function(x) setdiff(x, "")) - + if (length(authors_person) == 1) { authors_person <- authors_person[[1]] } - + return(authors_person) } - diff --git a/R/parse_authors_r.R b/R/parse_authors_r.R index f1e028d..6630db1 100644 --- a/R/parse_authors_r.R +++ b/R/parse_authors_r.R @@ -1,36 +1,34 @@ #' Parse the `Authors@R` field from a DESCRIPTION file -#' +#' #' Parse the `Authors@R` field from a DESCRIPTION file into a `person` object #' -#' @param authors_r_string A character containing the `Authors@R` field from a +#' @param authors_r_string A character containing the `Authors@R` field from a #' `DESCRIPTION` file #' #' @return A `person` object, or a `list` of `person` objects of length equals #' to the length of `authors_r_string` -#' +#' #' @examples #' # Read from a DESCRIPTION file directly #' pkg_description <- system.file("DESCRIPTION", package = "authoritative") #' authors_r_pkg <- read.dcf(pkg_description, "Authors@R") -#' +#' #' parse_authors_r(authors_r_pkg) -#' +#' #' # Read from a database of CRAN metadata -#' cran_epidemiology_packages |> +#' cran_epidemiology_packages |> #' subset(!is.na(`Authors@R`), `Authors@R`, drop = TRUE) |> -#' parse_authors_r() |> +#' parse_authors_r() |> #' head() -#' +#' #' @export parse_authors_r <- function(authors_r_string) { - # Sanitize input from pkgsearch / crandb - authors_r_string <- authors_r_string |> + authors_r_string <- authors_r_string |> stringi::stri_replace_all_fixed( "", " " ) lapply(str2expression(authors_r_string), eval) - -} \ No newline at end of file +} diff --git a/R/remove_brackets.R b/R/remove_brackets.R index 01adf4d..fc65ccc 100644 --- a/R/remove_brackets.R +++ b/R/remove_brackets.R @@ -1,43 +1,40 @@ #' Remove content between brackets -#' +#' #' @param string A character vector to clean #' @param bracket Type of bracket containing the content to remove. #' Must be one of "(" (default), "<", "[" -#' +#' #' @returns A character vector of the same size of `string`, where #' elements has been stripped of their `bracket`ted content. -#' +#' #' @export -#' +#' #' @examples #' remove_brackets( #' "R source code is versioned with svn (and mirrored on GitHub)" #' ) -#' +#' #' remove_brackets( #' c("r-project.org ", "python.org "), #' bracket = "<" #' ) -#' +#' #' remove_brackets( #' "Content in square bracket [is removed] in the middle of a string", #' bracket = "[" #' ) remove_brackets <- function(string, bracket = c("(", "<", "[")) { - bracket <- match.arg(bracket) - closing_bracket <- switch( - bracket, + closing_bracket <- switch(bracket, "<" = ">", "(" = ")", "[" = "]" ) - - string |> + + string |> stringi::stri_replace_all_regex( paste0("\\", bracket, "[^", closing_bracket, "]*\\", closing_bracket), "" ) - -} \ No newline at end of file +} diff --git a/data-raw/cran_epidemiology_packages.R b/data-raw/cran_epidemiology_packages.R index 9912321..241edb7 100644 --- a/data-raw/cran_epidemiology_packages.R +++ b/data-raw/cran_epidemiology_packages.R @@ -11,15 +11,15 @@ download.file( tf ) -ctv_pkgs <- ctv::read.ctv(tf) |> - purrr::pluck("packagelist", "name") +ctv_pkgs <- ctv::read.ctv(tf) |> + purrr::pluck("packagelist", "name") cran_epidemiology_packages <- ctv_pkgs |> purrr::map(pkgsearch::cran_package_history) -cran_epidemiology_packages <- cran_epidemiology_packages |> +cran_epidemiology_packages <- cran_epidemiology_packages |> dplyr::select( - Package, + Package, Version, `Authors@R`, Author, diff --git a/man/authoritative-package.Rd b/man/authoritative-package.Rd index 28ea0dc..5cbeb85 100644 --- a/man/authoritative-package.Rd +++ b/man/authoritative-package.Rd @@ -17,7 +17,7 @@ Useful links: } \author{ -\strong{Maintainer}: Hugo Gruson \email{hugo@data.org} (\href{https://orcid.org/0000-0002-4094-1476}{ORCID}) +\strong{Maintainer}: Hugo Gruson \email{hugo@data.org} (\href{https://orcid.org/0000-0002-4094-1476}{ORCID}) [copyright holder] } \keyword{internal} diff --git a/tests/testthat/test-dev-utils.R b/tests/testthat/test-dev-utils.R index 4e0a9a6..2f51375 100644 --- a/tests/testthat/test-dev-utils.R +++ b/tests/testthat/test-dev-utils.R @@ -1,5 +1,3 @@ test_that("release_bullets() returns what usethis expects", { - expect_type(release_bullets(), "character") - }) diff --git a/tests/testthat/test-expand_names.R b/tests/testthat/test-expand_names.R index 9fbc80d..3a027e5 100644 --- a/tests/testthat/test-expand_names.R +++ b/tests/testthat/test-expand_names.R @@ -1,8 +1,7 @@ test_that("expand_names() transforms matching names", { - expect_identical( expand_names( - c("W A Mozart", "Wolfgang Mozart", "Wolfgang A Mozart"), + c("W A Mozart", "Wolfgang Mozart", "Wolfgang A Mozart"), "Wolfgang Amadeus Mozart" ), rep_len("Wolfgang Amadeus Mozart", 3) @@ -10,28 +9,24 @@ test_that("expand_names() transforms matching names", { expect_identical( expand_names( - c("Wolfgang Mozart", "Johannes Bach"), + c("Wolfgang Mozart", "Johannes Bach"), c("Johannes Sebastian Bach", "Wolfgang Amadeus Mozart") ), c("Wolfgang Amadeus Mozart", "Johannes Sebastian Bach") ) - }) test_that("expand_names() favours longer forms", { - expect_identical( expand_names( - c("W A Mozart", "Wolfgang Mozart", "Wolfgang A Mozart"), + c("W A Mozart", "Wolfgang Mozart", "Wolfgang A Mozart"), c("W A Mozart", "Wolfgang Amadeus Mozart", "Wolfgang A Mozart") ), rep_len("Wolfgang Amadeus Mozart", 3) ) - }) test_that("expand_names() leaves non-matching names untouched", { - expect_identical( expand_names( c("Johannes Bach", "Wolfgang Mozart"), @@ -39,5 +34,4 @@ test_that("expand_names() leaves non-matching names untouched", { ), c("Johannes Bach", "Wolfgang Amadeus Mozart") ) - }) diff --git a/tests/testthat/test-parse_authors.R b/tests/testthat/test-parse_authors.R index 3454430..2a5b869 100644 --- a/tests/testthat/test-parse_authors.R +++ b/tests/testthat/test-parse_authors.R @@ -1,8 +1,6 @@ test_that("parse_authors() snapshot", { - cran_epidemiology_packages$Author |> - parse_authors() |> - unique() |> + parse_authors() |> + unique() |> expect_snapshot() - }) diff --git a/tests/testthat/test-parse_authors_R.R b/tests/testthat/test-parse_authors_R.R index 9681b37..96f65f7 100644 --- a/tests/testthat/test-parse_authors_R.R +++ b/tests/testthat/test-parse_authors_R.R @@ -1,11 +1,9 @@ test_that("parse_authors_r() snapshot", { - # This seems to be R version dependent? skip_on_ci() cran_epidemiology_packages$`Authors@R` |> - parse_authors_r() |> - unique() |> + parse_authors_r() |> + unique() |> expect_snapshot() - }) diff --git a/tests/testthat/test-remove_brackets.R b/tests/testthat/test-remove_brackets.R index 642d35a..5f82421 100644 --- a/tests/testthat/test-remove_brackets.R +++ b/tests/testthat/test-remove_brackets.R @@ -1,32 +1,28 @@ test_that("remove_brackets() works in simple cases", { - expect_identical( remove_brackets("Wolfgang Amadeus Mozart (1756-1791)"), "Wolfgang Amadeus Mozart " ) - + expect_identical( remove_brackets("A sourced claim [1]", bracket = "["), "A sourced claim " ) - + expect_identical( remove_brackets("A sourced claim [1] [12]", bracket = "["), "A sourced claim " ) - }) test_that("remove_brackets() works with nested brackets", { - expect_identical( remove_brackets("A sourced claim (source: [1])", bracket = "("), "A sourced claim " ) - + expect_identical( remove_brackets("A sourced claim (source: [1])", bracket = "["), "A sourced claim (source: )" ) - })