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 a59369036..76da2e0e3 100644 --- a/vignettes/database.Rmd +++ b/vignettes/database.Rmd @@ -20,7 +20,9 @@ tbl <- lapply(db, function(x) { disease = x$disease, pathogen = x$pathogen, epi_dist = x$epi_dist, - url = paste0( + citation_info = paste0( + epiparameter:::.citet(x$citation), # nolint undesirable_operator_linter + ", DOI: ", "", x$citation$doi, "" )