Skip to content

Commit

Permalink
Merge pull request #45 from Pakillo/citekeys-regex
Browse files Browse the repository at this point in the history
Fix problem with dashes in citation keys #44
  • Loading branch information
Pakillo authored Jun 4, 2024
2 parents fbda504 + 69c8953 commit f121665
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: grateful
Title: Facilitate Citation of R Packages
Version: 0.2.7
Version: 0.2.8
Authors@R: c(
person("Francisco", "Rodriguez-Sanchez",
email = "[email protected]",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# grateful 0.2.8

* Fix problem with citation keys containing non-alphanumeric characters (-) (issue #44, thanks @jkylearmstrong).

# grateful 0.2.7

Expand Down
2 changes: 1 addition & 1 deletion R/cite_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ cite_packages <- function(output = c("file", "paragraph", "table", "citekeys"),


if (output == "citekeys") {
return(unlist(pkgs.df$citekeys))
return(unname(unlist(pkgs.df$citekeys)))
}

}
Expand Down
20 changes: 13 additions & 7 deletions R/get_citations.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' @return A file on the specified `out.dir` containing the package references
#' in BibTeX format. If assigned a name, `get_citations` will also return a list
#' with citation keys for each citation (without @@).
#' with citation keys for each package (without @@).
#'
#' @export
#'
Expand Down Expand Up @@ -42,9 +42,10 @@ get_citations <- function(pkgs = NULL,

pkgs.notidy <- pkgs[pkgs != "tidyverse"]
cites.bib <- lapply(pkgs.notidy, get_citation_and_citekey)
names(cites.bib) <- pkgs.notidy

if ("tidyverse" %in% pkgs) {
cites.bib[[length(cites.bib) + 1]] <- add_citekey("tidyverse", tidyverse.citation)
cites.bib$tidyverse <- add_citekey("tidyverse", tidyverse.citation)
}

if (include.RStudio == TRUE) {
Expand All @@ -55,7 +56,7 @@ get_citations <- function(pkgs = NULL,
rstudio_cit <- tryCatch(rstudioapi::versionInfo()$citation,
error = function(e) NULL)
if (!is.null(rstudio_cit)) {
cites.bib[[length(cites.bib) + 1]] <- add_citekey("rstudio", rstudio_cit)
cites.bib$rstudio <- add_citekey("rstudio", rstudio_cit)
}
}
}
Expand All @@ -65,8 +66,13 @@ get_citations <- function(pkgs = NULL,
con = file.path(out.dir, paste0(bib.file, ".bib")),
useBytes = TRUE)

# get the citekeys and format them appropriately before returning them
citekeys <- unname(grep("\\{[[:alnum:]]+,$", unlist(cites.bib), value = TRUE))
citekeys <- gsub(".*\\{([[:alnum:]]+),$", "\\1", citekeys)
invisible(citekeys)
# get the citekeys for each package
get_pkg_citekeys <- function(pkg) {
citekeys <- unname(grep("\\{[[:alnum:]_-]+,$", pkg, value = TRUE))
citekeys <- gsub(".*\\{([[:alnum:]_-]+),$", "\\1", citekeys)
return(citekeys)
}
pkg_citekeys <- lapply(cites.bib, get_pkg_citekeys)

invisible(pkg_citekeys)
}
2 changes: 1 addition & 1 deletion R/get_csl.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ get_csl <- function(name = NULL, out.dir = NULL) {
}

if (is.null(out)) {
file.remove(destfile)
if (file.exists(destfile)) file.remove(destfile)
stop("The citation style '", name, "' could not be downloaded. Please check the style name and your internet connection.")
} else {
destfile
Expand Down
9 changes: 4 additions & 5 deletions R/get_pkgs_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ get_pkgs_info <- function(pkgs = "All",
bib.file = bib.file,
include.RStudio = include.RStudio)

if (isTRUE(include.RStudio)) {
citekeys <- citekeys[names(citekeys) != "rstudio"]
}

# Group all citations from same package
pkgs.df$citekeys <- lapply(pkgs.df$pkg, function(pkg) {
pkgname_clean <- gsub("[^[:alnum:]]", "", pkg)
citekeys[grep(paste0("^", pkgname_clean, "(\\d{4}[a-z]?)?$"), citekeys, perl = TRUE)]
})
pkgs.df$citekeys <- citekeys

pkgs.df

Expand Down
2 changes: 1 addition & 1 deletion man/get_citations.Rd

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

15 changes: 9 additions & 6 deletions tests/testthat/test-cite_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ test_that("providing wrong arguments return error", {
test_that("cite_packages returns correct citekeys", {

expect_identical(cite_packages(output = "citekeys",
pkgs = c("remotes", "renv"),
pkgs = c("remotes", "renv", "knitr"),
out.dir = tempdir()),
c("remotes", "renv"))
c("knitr2024", "knitr2015", "knitr2014", "remotes", "renv"))
})


test_that("cite_packages returns correct table", {

tabla <- cite_packages(output = "table",
pkgs = c("remotes", "renv"),
pkgs = c("remotes", "renv", "knitr"),
out.dir = tempdir())
expect_true(nrow(tabla) == 2)
expect_true(nrow(tabla) == 3)
expect_identical(names(tabla), c("Package", "Version", "Citation"))
expect_identical(tabla$Package, c("remotes", "renv"))
expect_identical(tabla$Citation, list("@remotes", "@renv"))
expect_identical(tabla$Package, c("knitr", "remotes", "renv"))
expect_identical(tabla$Citation,
list(knitr = "@knitr2014; @knitr2015; @knitr2024",
remotes = "@remotes",
renv = "@renv"))

})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-get_citations.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test_that("get_citations returns error if wrong arguments provided", {

test_that("get_citations works", {
citkeys <- get_citations("grateful", out.dir = tempdir())
expect_identical(citkeys, "grateful")
expect_identical(citkeys, list(grateful = "grateful"))
})

# test_that("get_citations adds Rstudio citation if asked", {
Expand Down
10 changes: 7 additions & 3 deletions tests/testthat/test-get_pkgs_info.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
test_that("get_pkgs_info works", {
info <- get_pkgs_info(pkgs = c("renv", "remotes", "dplyr", "ggplot2"),
info <- get_pkgs_info(pkgs = c("renv", "remotes", "dplyr", "ggplot2", "knitr"),
out.dir = tempdir())
expect_identical(info$pkg, c("remotes", "renv", "tidyverse"))
expect_identical(info$citekeys, list("remotes", "renv", "tidyverse"))
expect_identical(info$pkg, c("knitr", "remotes", "renv", "tidyverse"))
expect_identical(info$citekeys,
list(knitr = c("knitr2024", "knitr2015", "knitr2014"),
remotes = "remotes",
renv = "renv",
tidyverse = "tidyverse"))
})

0 comments on commit f121665

Please sign in to comment.