From 2d73101f08ff08f6341b5e947427e4e77b4d7139 Mon Sep 17 00:00:00 2001 From: Dorch <14124454+DDorch@users.noreply.github.com> Date: Tue, 5 Sep 2023 17:48:53 +0200 Subject: [PATCH] fix: error in create_reports - correction of reports/.gitignore file creation - resolve check issues Refs #3 --- NAMESPACE | 1 + R/add_gitignore.R | 9 ++++--- R/create_fairify.R | 2 +- R/create_reports.R | 27 +++++++++++-------- R/render_report.R | 2 +- R/render_reports.R | 5 ++-- R/utils.R | 21 +++++++++++++++ inst/bookdown_template/_bookdown.yml | 2 +- .../.gitignore => reports_gitignore.txt} | 0 inst/templates/inrae/preambule_inrae.tex | 3 ++- man/add_gitignore.Rd | 8 +++--- man/add_report.Rd | 3 ++- man/create_fairify.Rd | 2 +- man/create_reports.Rd | 5 ++-- man/purge_string.Rd | 23 ++++++++++++++++ man/render_reports.Rd | 2 +- tests/testthat/test-create_fairify.R | 2 +- tests/testthat/test-create_reports.R | 7 +++-- tests/testthat/test-render_reports.R | 15 +++++++++++ 19 files changed, 105 insertions(+), 34 deletions(-) rename inst/{reports/.gitignore => reports_gitignore.txt} (100%) create mode 100644 man/purge_string.Rd create mode 100644 tests/testthat/test-render_reports.R diff --git a/NAMESPACE b/NAMESPACE index c5aa1de..59fcbe1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(getDataPath) export(iconv_filename) export(loadConfig) export(pkg_sys) +export(purge_string) export(render_report) export(render_reports) export(updateMermaid) diff --git a/R/add_gitignore.R b/R/add_gitignore.R index 32c25c3..924fc93 100644 --- a/R/add_gitignore.R +++ b/R/add_gitignore.R @@ -1,7 +1,8 @@ #' Add .gitignore file suitable for R project #' #' @inheritParams create_fairify -#' @inheritParams utils::download.file +#' @param url URL of a `.gitignore` template file +#' @param additional_patterns Patterns to add to the `.gitignore` file #' #' @return `NULL`, this function is used for side effect. #' @export @@ -12,16 +13,16 @@ #' readLines(file.path(path, ".gitignore")) add_gitignore <- function(path = ".", url = "https://raw.githubusercontent.com/github/gitignore/main/R.gitignore", - additional_paths = c("/public")) { + additional_patterns = c("/public")) { destfile = file.path(path, ".gitignore") stopifnot(download.file(url, destfile) == 0) s <- readLines(destfile) - s <- c(add_comment_block("R template from:", + s <- c(add_comment_block(".gitignore template from:", url, paste("Downloaded the", Sys.time())), s, add_comment_block("Specific files for fairify"), - additional_paths) + additional_patterns) writeLines(s, destfile) } diff --git a/R/create_fairify.R b/R/create_fairify.R index 06bcb4e..7705584 100644 --- a/R/create_fairify.R +++ b/R/create_fairify.R @@ -12,7 +12,7 @@ #' @export #' #' @examples -#' path <- tempdir() +#' path <- tempfile(pattern = "dir") #' create_fairify(path, open = FALSE) #' list.files(path) create_fairify <- diff --git a/R/create_reports.R b/R/create_reports.R index be11bed..891500e 100644 --- a/R/create_reports.R +++ b/R/create_reports.R @@ -3,12 +3,13 @@ #' @param path Destination folder for reports and templates #' @param overwrite Allow overwriting templates folder #' -#' @return Use for side effect. +#' @return The path to the created reports folder. #' @export #' #' @examples #' # Create structure for reports -#' path <- tempdir() +#' path <- tempfile(pattern = "dir") +#' dir.create(path) #' create_reports(path) #' list.files(path, recursive = TRUE) #' @@ -24,15 +25,19 @@ create_reports <- function(path = ".", overwrite = FALSE) { stop("Folder ", file.path(path, "templates"), " already exists") } } - file.copy(from = pkg_sys("templates"), - to = path, - recursive = TRUE, - copy.date = TRUE) + + ok <- file.copy( + from = pkg_sys("templates"), + to = path, + recursive = TRUE + ) + if (!ok) stop("Error when copying 'templates' folder to ", path) # Create reports folder - file.copy(from = pkg_sys("reports"), - to = path, - recursive = TRUE, - copy.date = TRUE) - invisible() + reports_path <- file.path(path, "reports") + dir.create(reports_path) + add_gitignore(reports_path, + url = "https://raw.githubusercontent.com/github/gitignore/main/TeX.gitignore", + additional_patterns = readLines(pkg_sys("reports_gitignore.txt"))) + invisible(file.path(path, "reports")) } diff --git a/R/render_report.R b/R/render_report.R index a6f215a..01f7af6 100644 --- a/R/render_report.R +++ b/R/render_report.R @@ -39,5 +39,5 @@ render_report <- function(input, function(x) detach(paste0("package:", x), unload = TRUE, character.only = TRUE)) - NULL + invisible() } diff --git a/R/render_reports.R b/R/render_reports.R index 78dd7bf..35f29fd 100644 --- a/R/render_reports.R +++ b/R/render_reports.R @@ -15,7 +15,7 @@ #' #' @examples render_reports <- function(reports_dir = "./reports", - reports = list.dirs(reports_dir), + reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE), publish_dir = "../../public/reports", ...) { @@ -28,10 +28,9 @@ render_reports <- function(reports_dir = "./reports", message("*******************************************************************") message("** RENDER ", report) message("*******************************************************************") - path <- render_report(input = file.path(reports_dir, report), output_dir = file.path(publish_dir, report), ...) } - NULL + invisible() } diff --git a/R/utils.R b/R/utils.R index 845d244..37297ac 100644 --- a/R/utils.R +++ b/R/utils.R @@ -20,3 +20,24 @@ pkg_sys <- function( mustWork = mustWork ) } + + +#' Purge a string from all accents and special characters +#' +#' @param x the [character] string to purge +#' @param replacement the [character] to use for replacing special characters +#' +#' @return A purged [character] string +#' @export +#' +#' @examples +#' purge_string("Là chaîne pl€ine dé &#/* $péciaux", "_") +#' +purge_string <- function(x, replacement = " ") { + iconv(gsub(paste0(replacement, "+"), replacement, + gsub("&|=|-|:|\\.|'|\\/|<|>|e0|e9|e8|e7|e6|_|\\(|\\)|\"|\\*|#|€|\\$|\\%", + replacement, + enc2utf8(x))), + from = "UTF-8", + to = "ASCII//TRANSLIT") +} diff --git a/inst/bookdown_template/_bookdown.yml b/inst/bookdown_template/_bookdown.yml index 7f3b0e6..64f670d 100644 --- a/inst/bookdown_template/_bookdown.yml +++ b/inst/bookdown_template/_bookdown.yml @@ -1,4 +1,4 @@ -book_filename: "_01-GRS_specs" +book_filename: "report" delete_merged_file: true language: ui: diff --git a/inst/reports/.gitignore b/inst/reports_gitignore.txt similarity index 100% rename from inst/reports/.gitignore rename to inst/reports_gitignore.txt diff --git a/inst/templates/inrae/preambule_inrae.tex b/inst/templates/inrae/preambule_inrae.tex index 5d1ec4a..5293ad5 100644 --- a/inst/templates/inrae/preambule_inrae.tex +++ b/inst/templates/inrae/preambule_inrae.tex @@ -26,7 +26,8 @@ % http://tex.stackexchange.com/a/13691/124910 % \usepackage{tocstyle} % tocstyle is deprecated see https://ctan.org/pkg/tocstyle \usepackage{tocbasic} -\usepackage{scrwfile} +% In case of `no room for new \write` issue +% \usepackage{scrwfile} % Ajout de la gestion des couleurs % The \usepackage is obvious, but the initialization of additional commands like usenames allows you to use names of the default colors, the same 16 base colors as used in HTML. The dvipsnames allows you access to more colors, another 64, and svgnames allows access to about 150 colors. The initialization of "table" allows colors to be added to tables by placing the color command just before the table. https://en.wikibooks.org/wiki/LaTeX/Colors diff --git a/man/add_gitignore.Rd b/man/add_gitignore.Rd index abe3a8b..e6e5b6b 100644 --- a/man/add_gitignore.Rd +++ b/man/add_gitignore.Rd @@ -7,16 +7,16 @@ add_gitignore( path = ".", url = "https://raw.githubusercontent.com/github/gitignore/main/R.gitignore", - additional_paths = c("/public") + additional_patterns = c("/public") ) } \arguments{ \item{path}{A path. If it exists, it is used. If it does not exist, it is created, provided that the parent path exists.} -\item{url}{a \code{\link{character}} string (or longer vector - for the \code{"libcurl"} method) naming the URL of a resource to be - downloaded.} +\item{url}{URL of a \code{.gitignore} template file} + +\item{additional_patterns}{Patterns to add to the \code{.gitignore} file} } \value{ \code{NULL}, this function is used for side effect. diff --git a/man/add_report.Rd b/man/add_report.Rd index dd13a4d..2850c75 100644 --- a/man/add_report.Rd +++ b/man/add_report.Rd @@ -19,7 +19,8 @@ Add a new report in a fairify project } \examples{ # Create structure for reports -path <- tempdir() +path <- tempfile(pattern = "dir") +dir.create(path) create_reports(path) list.files(path, recursive = TRUE) diff --git a/man/create_fairify.Rd b/man/create_fairify.Rd index 490e7b2..20bd01f 100644 --- a/man/create_fairify.Rd +++ b/man/create_fairify.Rd @@ -43,7 +43,7 @@ Use for side effect. Create a package ready for fairify your project. } \examples{ -path <- tempdir() +path <- tempfile(pattern = "dir") create_fairify(path, open = FALSE) list.files(path) } diff --git a/man/create_reports.Rd b/man/create_reports.Rd index 1684265..cf6fd51 100644 --- a/man/create_reports.Rd +++ b/man/create_reports.Rd @@ -12,14 +12,15 @@ create_reports(path = ".", overwrite = FALSE) \item{overwrite}{Allow overwriting templates folder} } \value{ -Use for side effect. +The path to the created reports folder. } \description{ Set reports folder structure } \examples{ # Create structure for reports -path <- tempdir() +path <- tempfile(pattern = "dir") +dir.create(path) create_reports(path) list.files(path, recursive = TRUE) diff --git a/man/purge_string.Rd b/man/purge_string.Rd new file mode 100644 index 0000000..68c0379 --- /dev/null +++ b/man/purge_string.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{purge_string} +\alias{purge_string} +\title{Purge a string from all accents and special characters} +\usage{ +purge_string(x, replacement = " ") +} +\arguments{ +\item{x}{the \link{character} string to purge} + +\item{replacement}{the \link{character} to use for replacing special characters} +} +\value{ +A purged \link{character} string +} +\description{ +Purge a string from all accents and special characters +} +\examples{ +purge_string("Là chaîne pl€ine dé &#/* $péciaux", "_") + +} diff --git a/man/render_reports.Rd b/man/render_reports.Rd index 999b425..f249226 100644 --- a/man/render_reports.Rd +++ b/man/render_reports.Rd @@ -15,7 +15,7 @@ render_report( render_reports( reports_dir = "./reports", - reports = list.dirs(reports_dir), + reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE), publish_dir = "../../public/reports", ... ) diff --git a/tests/testthat/test-create_fairify.R b/tests/testthat/test-create_fairify.R index b7c981a..a11d0ca 100644 --- a/tests/testthat/test-create_fairify.R +++ b/tests/testthat/test-create_fairify.R @@ -6,7 +6,7 @@ test_that("create_fairify works", { expect_true(file.exists(file.path(path, ".git"))) }) -test_that("create_fairify with git = FALSE doesn't create git repoworks", { +test_that("create_fairify with git = FALSE doesn't create git repo works", { path <- tempfile(pattern = "dir") create_fairify(path, open = FALSE, git = FALSE) project <- basename(path) diff --git a/tests/testthat/test-create_reports.R b/tests/testthat/test-create_reports.R index 0003cbc..1649913 100644 --- a/tests/testthat/test-create_reports.R +++ b/tests/testthat/test-create_reports.R @@ -1,6 +1,9 @@ test_that("create_reports works", { - path <- tempdir() - create_reports(path) + path <- tempfile(pattern = "dir") + dir.create(path, recursive = TRUE) + expect_equal(create_reports(path), + file.path(path, "reports")) + expect_true(all(dir.exists(file.path(path, c("reports", "templates"))))) expect_error(create_reports(path)) expect_warning(create_reports(path, overwrite = TRUE)) }) diff --git a/tests/testthat/test-render_reports.R b/tests/testthat/test-render_reports.R new file mode 100644 index 0000000..be2ef3a --- /dev/null +++ b/tests/testthat/test-render_reports.R @@ -0,0 +1,15 @@ +skip_on_ci() + +test_that("render_reports works", { + path <- tempfile(pattern = "dir") + create_fairify(path, open = FALSE) + add_report("my_report", path) + render_reports(reports_dir = file.path(path, "reports"), + output_format = "bookdown::gitbook") + report_path <- file.path(path, "public/reports/my_report") + expect_true(dir.exists(report_path)) + expect_true(file.exists(file.path(report_path, "index.html"))) + render_reports(reports_dir = file.path(path, "reports"), + output_format = "bookdown::pdf_book") + expect_true(file.exists()) +}) -- GitLab