diff --git a/NAMESPACE b/NAMESPACE index c5aa1ded7b108832f6ce95a42289f1f1f6227d1c..59fcbe184f9ba00a7287e4b718acebe98c1d6f7b 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 32c25c3d87ae06a2ae3687f31754b8e286b27ac6..924fc93567980f9abf409c107bead22dc9bd6a34 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 06bcb4e41950bd3ee45d0cf1cc22f3298cf9d9dc..77055842788976c9324c6105428e4bcc79c3c9ca 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 be11beddc0012b35833091f80b496d2ce0bf8c69..891500ee3b62c702ca236343d69de66675d2ef54 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 a6f215a6e2fbbf0617585d7c03d42a1357097513..01f7af69512f3e1b2486da64cb55394deca94c43 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 78dd7bfbd5323f8639d4637780e4f6dc57222c47..35f29fde8a3cf07d17e40fcd207f0f10d02538e5 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 845d244559a3d564ba3de0d3ae5f98e71f63d074..37297ac88fab7170a1a020c23016975f2cab0d27 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 7f3b0e68acbf524fa25693a6baaabf059d319153..64f670d7a4740b9d3bf721b06a74f98c6363ae0c 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 5d1ec4ad7049748dfb0ba0982d17873bf6b7b86b..5293ad533e1f23f06d4ed9aa1490c22cdd720225 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 abe3a8bf08e2b4f448365c01a0dfe9d8eeca4e44..e6e5b6b8ef42973233ba31450554c51e3a34576e 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 dd13a4dc8c1e791855fc224227f99acc59782325..2850c75444a8f1215984e1dd8c08e28be72b44d0 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 490e7b2942353af8e93d28a70ede79f298bca089..20bd01f831a043cacb321668d4de5828a34b65b8 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 168426576e96372e6a6afb0d6f376c2522ed424d..cf6fd51cdbcd13891b9d5c18603d61b078795dbc 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 0000000000000000000000000000000000000000..68c03791b7389f24ca5e80c520696e25a0fe44da --- /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 999b4256572f9fdf2ef1f7397cdce7c06c555770..f249226fe1e1176d4b29e182f5303faf2e9d1cc8 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 b7c981aaf0f3240fc6582b099e4c0dbb298d8435..a11d0ca90cff6a92ad496678a0e65a34b7d5f2de 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 0003cbc770e9f6fdb8ae29b6d48a6ffa77e2331f..16499132bd41db5f5e9d9a7a6cf721374cef3563 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 0000000000000000000000000000000000000000..be2ef3aee323d99ed9d9ed81b13567dcc7357ba2 --- /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()) +})