From 0943e6d4b4b1df10382a7df3fa4a947d0254e7b7 Mon Sep 17 00:00:00 2001 From: Francisco Rodriguez-Sanchez Date: Tue, 2 Jul 2024 13:36:13 +0200 Subject: [PATCH] 0.2.10 add LaTeX as output format --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ R/cite_packages.R | 7 +++++-- R/create_rmd.R | 7 +++++-- R/render_citations.R | 18 +++++++++++++++--- README.Rmd | 6 ++++-- README.md | 14 +++++++++----- man/cite_packages.Rd | 6 ++++-- tests/testthat/test-create_rmd.R | 26 ++++++++++++++++++++++++-- tests/testthat/test-render_citations.R | 10 ++++++++++ 10 files changed, 81 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9f8ec69..ad0e535 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: grateful Title: Facilitate Citation of R Packages -Version: 0.2.9 +Version: 0.2.10 Authors@R: c( person("Francisco", "Rodriguez-Sanchez", email = "f.rodriguez.sanc@gmail.com", diff --git a/NEWS.md b/NEWS.md index 1cb2435..f0fba16 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# grateful 0.2.10 + +* Add support for LaTeX output (thanks @patrick-weiss for the suggestion). Use `out.format = "tex-document"` to obtain a full LaTeX document, or `out.format = "tex-fragment"` to produce just a fragment citing packages to be copied into another existing LaTeX document. + # grateful 0.2.9 * Match package names with citation keys explicitly, rather than relying on sorting. diff --git a/R/cite_packages.R b/R/cite_packages.R index d4c0d69..1cb284f 100644 --- a/R/cite_packages.R +++ b/R/cite_packages.R @@ -64,7 +64,9 @@ #' the references. It is recommended to set `out.dir = getwd()`. #' #' @param out.format Output format when `output = "file"`: -#' either "html" (the default), "docx" (Word), "pdf", "Rmd", or "md" (markdown). +#' either "html" (the default), "docx" (Word), "pdf", +#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document), +#' "tex-document" (full LaTeX document), "Rmd", or "md" (markdown). #' (Note that choosing "pdf" requires a working installation of LaTeX, #' see ). #' @@ -158,7 +160,8 @@ cite_packages <- function(output = c("file", "paragraph", "table", "citekeys"), out.dir = NULL, - out.format = c("html", "docx", "pdf", "Rmd", "md"), + out.format = c("html", "docx", "pdf", "Rmd", "md", + "tex-fragment", "tex-document"), citation.style = NULL, pkgs = "All", omit = c("grateful"), diff --git a/R/create_rmd.R b/R/create_rmd.R index ba5e0fa..05cdde2 100644 --- a/R/create_rmd.R +++ b/R/create_rmd.R @@ -7,7 +7,9 @@ #' as produced by [get_pkgs_info()]. #' @param csl Optional. Citation style to format references. #' See . -#' @param out.format Output format. One of: "html", docx" (Word), "pdf", "Rmd", or "md" (markdown). +#' @param out.format Output format, either "html", docx" (Word), "pdf", +#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document), +#' "tex-document" (full LaTeX document), "Rmd"(Rmarkdown), or "md" (markdown). #' @param Rmd.file Name of the Rmarkdown file to be created. #' @param include.RStudio Include RStudio? #' @param passive.voice Logical. If `TRUE`, uses passive voice in any paragraph @@ -23,7 +25,8 @@ create_rmd <- function(pkgs.df = NULL, bib.file = "grateful-refs", csl = NULL, Rmd.file = "grateful-report", - out.format = c("html", "docx", "pdf", "Rmd", "md"), + out.format = c("html", "docx", "pdf", "Rmd", "md", + "tex-fragment", "tex-document"), include.RStudio = FALSE, passive.voice = FALSE, ...) { diff --git a/R/render_citations.R b/R/render_citations.R index d3b9150..e5d9206 100644 --- a/R/render_citations.R +++ b/R/render_citations.R @@ -2,7 +2,9 @@ #' #' @param Rmd.file Path to Rmarkdown file generated by [create_rmd()]. #' @param out.dir Directory to save the output document. -#' @param out.format Output format, either "html", docx" (Word), "pdf", or "md" (markdown). +#' @param out.format Output format, either "html", docx" (Word), "pdf", +#' "tex-fragment" (LaTeX fragment to be inserted into another LaTeX document), +#' "tex-document" (full LaTeX document), or "md" (markdown). #' #' @return A document in the format specified by `out.format`. #' @@ -11,7 +13,8 @@ render_citations <- function(Rmd.file = NULL, out.dir = NULL, - out.format = c("html", "docx", "pdf", "md") + out.format = c("html", "docx", "pdf", "md", + "tex-fragment", "tex-document") ) { if (is.null(Rmd.file)) { @@ -28,6 +31,14 @@ render_citations <- function(Rmd.file = NULL, if (out.format == "pdf") output <- "pdf_document" if (out.format == "html") output <- "html_document" if (out.format == "md") output <- "md_document" + if (out.format == "tex-fragment") output <- "latex_fragment" + if (out.format == "tex-document") output <- "latex_document" + + output.options <- NULL + if (out.format == "tex-fragment" | out.format == "tex-document") { + out.format <- "tex" + output.options <- list(latex_engine = "xelatex", citation_package = "natbib") + } out.name <- gsub(pattern = ".Rmd", replacement = "", basename(Rmd.file)) @@ -35,7 +46,8 @@ render_citations <- function(Rmd.file = NULL, input = Rmd.file, output_format = output, output_file = paste0(out.name, ".", out.format), - output_dir = out.dir + output_dir = out.dir, + output_options = output.options ) out.file <- file.path(out.dir, paste0(out.name, ".", out.format)) diff --git a/README.Rmd b/README.Rmd index cbb63f4..bbe82dc 100644 --- a/README.Rmd +++ b/README.Rmd @@ -22,12 +22,14 @@ knitr::opts_chunk$set( [![Codecov test coverage](https://codecov.io/gh/Pakillo/grateful/branch/master/graph/badge.svg)](https://app.codecov.io/gh/Pakillo/grateful?branch=master) `r badger::badge_lifecycle("stable")` `r badger::badge_repostatus("Active")` +[![HitCount since 2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square)](https://hits.dwyl.com/Pakillo/grateful) +[![HitCount since 2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square&show=unique)](https://hits.dwyl.com/Pakillo/grateful) [![](https://cranlogs.r-pkg.org/badges/grand-total/grateful)](https://cran.r-project.org/package=grateful) The goal of **grateful** is to make it very easy to cite R and the R packages used in any analyses, so that package authors receive their deserved credit. By calling a single function, **grateful** will scan the project for R packages used and generate a BibTeX file containing all citations for those packages. -**grateful** can then generate a new document with citations in the desired output format (Word, PDF, HTML, Markdown). These references can be formatted for a specific journal, so that we can just paste them directly into our manuscript or report. +**grateful** can then generate a new document with citations in the desired output format (Word, PDF, LaTeX, HTML, Markdown). These references can be formatted for a specific journal, so that we can just paste them directly into our manuscript or report. Alternatively, we can use **grateful** directly within an [Rmarkdown](https://rmarkdown.rstudio.com/) or [Quarto](https://quarto.org/) document. In this case, a paragraph containing in-text citations of all used R packages will (optionally) be inserted into the Rmarkdown/Quarto document, and these packages will be included in the reference list when rendering. @@ -75,7 +77,7 @@ cite_packages(out.dir = ".") # save report to working directory
-This document can also be a Word document, PDF file, markdown file, or left as the source Rmarkdown file using `out.format`: +This document can also be a Word or LaTeX document, PDF file, markdown file, or left as the source Rmarkdown file using `out.format`: ```{r eval = FALSE} cite_packages(out.format = "docx", out.dir = ".") diff --git a/README.md b/README.md index 54edbe8..3681cf6 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ coverage](https://codecov.io/gh/Pakillo/grateful/branch/master/graph/badge.svg)] [![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) +[![HitCount since +2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square)](https://hits.dwyl.com/Pakillo/grateful) +[![HitCount since +2024-06-05](https://hits.dwyl.com/Pakillo/grateful.svg?style=flat-square&show=unique)](https://hits.dwyl.com/Pakillo/grateful) [![](https://cranlogs.r-pkg.org/badges/grand-total/grateful)](https://cran.r-project.org/package=grateful) @@ -25,9 +29,9 @@ the project for R packages used and generate a BibTeX file containing all citations for those packages. **grateful** can then generate a new document with citations in the -desired output format (Word, PDF, HTML, Markdown). These references can -be formatted for a specific journal, so that we can just paste them -directly into our manuscript or report. +desired output format (Word, PDF, LaTeX, HTML, Markdown). These +references can be formatted for a specific journal, so that we can just +paste them directly into our manuscript or report. Alternatively, we can use **grateful** directly within an [Rmarkdown](https://rmarkdown.rstudio.com/) or @@ -85,8 +89,8 @@ cite_packages(out.dir = ".") # save report to working directory
-This document can also be a Word document, PDF file, markdown file, or -left as the source Rmarkdown file using `out.format`: +This document can also be a Word or LaTeX document, PDF file, markdown +file, or left as the source Rmarkdown file using `out.format`: ``` r cite_packages(out.format = "docx", out.dir = ".") diff --git a/man/cite_packages.Rd b/man/cite_packages.Rd index d51e673..3db5031 100644 --- a/man/cite_packages.Rd +++ b/man/cite_packages.Rd @@ -7,7 +7,7 @@ cite_packages( output = c("file", "paragraph", "table", "citekeys"), out.dir = NULL, - out.format = c("html", "docx", "pdf", "Rmd", "md"), + out.format = c("html", "docx", "pdf", "Rmd", "md", "tex-fragment", "tex-document"), citation.style = NULL, pkgs = "All", omit = c("grateful"), @@ -39,7 +39,9 @@ In all cases, a BibTeX file with package references is saved on disk the references. It is recommended to set \code{out.dir = getwd()}.} \item{out.format}{Output format when \code{output = "file"}: -either "html" (the default), "docx" (Word), "pdf", "Rmd", or "md" (markdown). +either "html" (the default), "docx" (Word), "pdf", +"tex-fragment" (LaTeX fragment to be inserted into another LaTeX document), +"tex-document" (full LaTeX document), "Rmd", or "md" (markdown). (Note that choosing "pdf" requires a working installation of LaTeX, see \url{https://yihui.org/tinytex/}).} diff --git a/tests/testthat/test-create_rmd.R b/tests/testthat/test-create_rmd.R index 453a06b..a5a6596 100644 --- a/tests/testthat/test-create_rmd.R +++ b/tests/testthat/test-create_rmd.R @@ -8,7 +8,7 @@ test_that("NULL arguments return error", { test_that("create_rmd produces correct Rmd", { skip_on_cran() - skip_on_ci() + # skip_on_ci() pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir()) rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "Rmd") @@ -27,7 +27,7 @@ test_that("create_rmd produces correct Rmd", { "", "|Package |Version |Citation |", "|:--------|:-------|:---------|", - paste0("|grateful |", utils::packageVersion("grateful")," |@grateful |"), + paste0("|grateful |", utils::packageVersion("grateful")," |@grateful |"), "", "**You can paste this paragraph directly in your report:**", "", @@ -39,3 +39,25 @@ test_that("create_rmd produces correct Rmd", { }) + + + +test_that("csl is downloaded if needed", { + skip_on_cran() + pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir()) + rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "Rmd", + csl = "peerj") + expect_true(file.exists(file.path(tempdir(), "peerj.csl"))) + +}) + + +test_that("rendered report is produced", { + skip_on_cran() + skip_on_ci() + pkgs <- get_pkgs_info(pkgs = "grateful", out.dir = tempdir()) + rmd <- create_rmd(pkgs.df = pkgs, out.dir = tempdir(), out.format = "html") + expect_true(file.exists(file.path(tempdir(), "grateful-report.html"))) + expect_true(!file.exists(file.path(tempdir(), "grateful-report.Rmd"))) + +}) diff --git a/tests/testthat/test-render_citations.R b/tests/testthat/test-render_citations.R index 24b6cf5..4493047 100644 --- a/tests/testthat/test-render_citations.R +++ b/tests/testthat/test-render_citations.R @@ -33,4 +33,14 @@ test_that("render_citations returns a report", { expect_equal(rendcit, file.path(tempdir(), "grateful-report.md")) expect_true(file.exists(file.path(tempdir(), "grateful-report.md"))) + # LaTeX fragment + rendcit <- render_citations(Rmd.file = rmd, out.dir = tempdir(), out.format = "tex-fragment") + expect_equal(rendcit, file.path(tempdir(), "grateful-report.tex")) + expect_true(file.exists(file.path(tempdir(), "grateful-report.tex"))) + + # LaTeX document + rendcit <- render_citations(Rmd.file = rmd, out.dir = tempdir(), out.format = "tex-document") + expect_equal(rendcit, file.path(tempdir(), "grateful-report.tex")) + expect_true(file.exists(file.path(tempdir(), "grateful-report.tex"))) + })