From b07e21a74d52f222338f1efd81364c9e4a06c0a8 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Wed, 3 Jul 2024 12:49:42 +0100 Subject: [PATCH 1/3] Add author name et al to DOI --- vignettes/database.Rmd | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/vignettes/database.Rmd b/vignettes/database.Rmd index a59369036..1a71d39b3 100644 --- a/vignettes/database.Rmd +++ b/vignettes/database.Rmd @@ -15,12 +15,21 @@ library("DT") library("epiparameter") db <- epidist_db() +# Shorten citation info to first author family name et al. (year) +make_short_citation <- function(x){ + first_author_family_name <- x$citation$author[1]$family + paper_year <- x$citation$year + paste0(first_author_family_name, " et al. (", paper_year, ")") +} + tbl <- lapply(db, function(x) { data.frame( disease = x$disease, pathogen = x$pathogen, epi_dist = x$epi_dist, - url = paste0( + citation_info = paste0( + make_short_citation(x), + ", DOI: ", "", x$citation$doi, "" ) From 8ff31365be06b52402f156dd94f895fbada98417 Mon Sep 17 00:00:00 2001 From: jamesaazam Date: Wed, 3 Jul 2024 12:54:46 +0100 Subject: [PATCH 2/3] Fix linting issues --- vignettes/database.Rmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vignettes/database.Rmd b/vignettes/database.Rmd index 1a71d39b3..7c590b307 100644 --- a/vignettes/database.Rmd +++ b/vignettes/database.Rmd @@ -16,7 +16,7 @@ library("epiparameter") db <- epidist_db() # Shorten citation info to first author family name et al. (year) -make_short_citation <- function(x){ +make_short_citation <- function(x) { first_author_family_name <- x$citation$author[1]$family paper_year <- x$citation$year paste0(first_author_family_name, " et al. (", paper_year, ")") @@ -29,7 +29,7 @@ tbl <- lapply(db, function(x) { epi_dist = x$epi_dist, citation_info = paste0( make_short_citation(x), - ", DOI: ", + ", DOI: ", "", x$citation$doi, "" ) From ec809cf91d207bf60f4f9bb34e95b7f9df6c645b Mon Sep 17 00:00:00 2001 From: Joshua Lambert Date: Mon, 15 Jul 2024 13:31:32 +0100 Subject: [PATCH 3/3] add .citet function and tests and use in database vignette --- R/utils.R | 32 +++++++++++++++++++++++++ man/dot-citet.Rd | 19 +++++++++++++++ tests/testthat/test-utils.R | 47 +++++++++++++++++++++++++++++++++++++ vignettes/database.Rmd | 9 +------ 4 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 man/dot-citet.Rd diff --git a/R/utils.R b/R/utils.R index 1b145cf7f..d3dc5f656 100644 --- a/R/utils.R +++ b/R/utils.R @@ -36,3 +36,35 @@ calc_disc_dist_quantile <- function(prob, days, quantile) { names(quantiles) <- as.character(quantile) quantiles } + +#' Format short citation from `` object +#' +#' @description +#' Output is equivalent to the `\citet{}` function in the \pkg{natbib} LaTeX +#' package. +#' +#' @param x A `` object, see [bibentry()]. +#' +#' @return A `character` string with the short citation. +#' @keywords internal +.citet <- function(x) { + stopifnot(inherits(x, "bibentry")) + num_author <- length(x$author) + # check if first author is an organisation + is_org_author <- is.null(x$author[1]$family) + # this covers single author entries + if (is_org_author) { + # organisation name stored in $given + cit <- x$author[1]$given + } else { + cit <- x$author[1]$family + } + # append second author or et al for multi-author entries + if (num_author == 2) { + cit <- paste(cit, "&", x$author[2]$family) + } else if (num_author > 2) { + cit <- paste(cit, "et al.") + } + cit <- paste0(cit, " (", x$year, ")") + cit +} diff --git a/man/dot-citet.Rd b/man/dot-citet.Rd new file mode 100644 index 000000000..abbf89184 --- /dev/null +++ b/man/dot-citet.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{.citet} +\alias{.citet} +\title{Format short citation from \verb{} object} +\usage{ +.citet(x) +} +\arguments{ +\item{x}{A \verb{} object, see \code{\link[=bibentry]{bibentry()}}.} +} +\value{ +A \code{character} string with the short citation +} +\description{ +Output is equivalent to the \verb{\\citet\{\}} function in the \pkg{natbib} LaTeX +package. +} +\keyword{internal} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index bc4c64ba6..7bb56a08e 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -56,3 +56,50 @@ test_that("calc_disc_dist_quantile fails as expected", { ) ) }) + +test_that(".citet works as expected for multi-author", { + suppressMessages( + epidist <- epidist_db( + author = "Lessler", + disease = "RSV", + single_epidist = TRUE + ) + ) + expect_identical(.citet(epidist$citation), "Lessler et al. (2009)") +}) + +test_that(".citet works as expected for single author", { + suppressMessages( + epidist <- epidist_db( + author = "Pavlin", + disease = "Marburg", + single_epidist = TRUE + ) + ) + expect_identical(.citet(epidist$citation), "Pavlin (2014)") +}) + +test_that(".citet works as expected for two authors", { + suppressMessages( + epidist <- epidist_db( + author = "Nishiura", + disease = "Influenza", + single_epidist = TRUE + ) + ) + expect_identical(.citet(epidist$citation), "Nishiura & Inaba (2011)") +}) + +test_that(".citet works as expected for organisation author", { + suppressMessages( + epidist <- epidist_db( + author = "WHO", + disease = "Ebola", + single_epidist = TRUE + ) + ) + expect_identical( + .citet(epidist$citation), + "WHO Ebola Response Team et al. (2015)" + ) +}) diff --git a/vignettes/database.Rmd b/vignettes/database.Rmd index 7c590b307..76da2e0e3 100644 --- a/vignettes/database.Rmd +++ b/vignettes/database.Rmd @@ -15,20 +15,13 @@ library("DT") library("epiparameter") db <- epidist_db() -# Shorten citation info to first author family name et al. (year) -make_short_citation <- function(x) { - first_author_family_name <- x$citation$author[1]$family - paper_year <- x$citation$year - paste0(first_author_family_name, " et al. (", paper_year, ")") -} - tbl <- lapply(db, function(x) { data.frame( disease = x$disease, pathogen = x$pathogen, epi_dist = x$epi_dist, citation_info = paste0( - make_short_citation(x), + epiparameter:::.citet(x$citation), # nolint undesirable_operator_linter ", DOI: ", "", x$citation$doi, ""