diff --git a/DESCRIPTION b/DESCRIPTION index 4fc90bd..2c41174 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "f.rodriguez.sanc@gmail.com", diff --git a/NEWS.md b/NEWS.md index b0bdf51..6a0fd7f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/cite_packages.R b/R/cite_packages.R index 8338c11..d4c0d69 100644 --- a/R/cite_packages.R +++ b/R/cite_packages.R @@ -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))) } } diff --git a/R/get_citations.R b/R/get_citations.R index 1bee3e2..41d845f 100644 --- a/R/get_citations.R +++ b/R/get_citations.R @@ -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 #' @@ -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) { @@ -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) } } } @@ -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) } diff --git a/R/get_csl.R b/R/get_csl.R index beab3a0..703590f 100644 --- a/R/get_csl.R +++ b/R/get_csl.R @@ -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 diff --git a/R/get_pkgs_info.R b/R/get_pkgs_info.R index 333603e..e12bd6f 100644 --- a/R/get_pkgs_info.R +++ b/R/get_pkgs_info.R @@ -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 diff --git a/man/get_citations.Rd b/man/get_citations.Rd index 6ffa85e..db78609 100644 --- a/man/get_citations.Rd +++ b/man/get_citations.Rd @@ -26,7 +26,7 @@ current version of RStudio.} \value{ A file on the specified \code{out.dir} containing the package references in BibTeX format. If assigned a name, \code{get_citations} will also return a list -with citation keys for each citation (without @). +with citation keys for each package (without @). } \description{ Get citations for packages diff --git a/tests/testthat/test-cite_packages.R b/tests/testthat/test-cite_packages.R index c4690dc..b357ff8 100644 --- a/tests/testthat/test-cite_packages.R +++ b/tests/testthat/test-cite_packages.R @@ -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")) }) diff --git a/tests/testthat/test-get_citations.R b/tests/testthat/test-get_citations.R index 586cf66..a451243 100644 --- a/tests/testthat/test-get_citations.R +++ b/tests/testthat/test-get_citations.R @@ -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", { diff --git a/tests/testthat/test-get_pkgs_info.R b/tests/testthat/test-get_pkgs_info.R index 64cb0ee..082112f 100644 --- a/tests/testthat/test-get_pkgs_info.R +++ b/tests/testthat/test-get_pkgs_info.R @@ -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")) })