Skip to content

Commit

Permalink
add .citet function and tests and use in database vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlambert committed Jul 15, 2024
1 parent ddb89b7 commit 2bdcd15
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
32 changes: 32 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,35 @@ calc_disc_dist_quantile <- function(prob, days, quantile) {
names(quantiles) <- as.character(quantile)
quantiles
}

#' Format short citation from `<bibentry>` object
#'
#' @description
#' Output is equivalent to the `\citet{}` function in the \pkg{natbib} LaTeX
#' package.
#'
#' @param x A `<bibentry>` 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
}
19 changes: 19 additions & 0 deletions man/dot-citet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
)
})
9 changes: 1 addition & 8 deletions vignettes/database.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",
"<a href=\"https://doi.org/", x$citation$doi, "\">",
x$citation$doi, "</a>"
Expand Down

0 comments on commit 2bdcd15

Please sign in to comment.