Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve database: add first author name et al to Reference column #348

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)"
)
})
4 changes: 3 additions & 1 deletion vignettes/database.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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: ",
"<a href=\"https://doi.org/", x$citation$doi, "\">",
x$citation$doi, "</a>"
)
Expand Down
Loading