From 1cea776586ecedb9d712b097be0be9b540a80631 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Fri, 8 Mar 2024 17:53:03 +0100 Subject: [PATCH 1/3] feat(add_report): change template strategy - remove copy of templates in create_reports - create reports folder if needed - check template existence - add .template_dependencies file Refs #23 --- R/add_report.R | 29 ++++++++++++++++++++++++----- R/create_reports.R | 16 ---------------- tests/testthat/test-add_report.R | 24 +++++++++++++++--------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/R/add_report.R b/R/add_report.R index c202398..0aedbba 100644 --- a/R/add_report.R +++ b/R/add_report.R @@ -12,15 +12,31 @@ #' @inherit create_reports examples #' add_report <- function(name, - template = "inrae", + template = "fairify:inrae", lang = "english", path = pkgload::pkg_path()) { - if (!all(dir.exists(file.path(path, c("reports", "templates"))))) { - stop("Report structure not found, call `create_reports` first!") + # reports folder handling + if (!dir.exists(file.path(path, "reports"))) { + message("Creating reports folder in: ", path, "...") + create_reports(path) } - if (!dir.exists(file.path(path, "templates", template))) { - stop("Template\"", template, "\" not found in ", file.path(path, "templates")) + # Checking template existence + if (!grepl("^[a-zA-Z0-9_]*\\:[a-zA-Z0-9_]*$", template)) { + stop("`template` should be in the format `[package]:[template_name]`") } + template_location <- strsplit(template, ":", fixed = TRUE)[[1]] + if (!dir.exists(system.file("templates", package = template_location[1]))) { + stop("'templates' folder not found in the package '", template_location[1], "'\n", + "Complete folder: ",system.file("templates", package = template_location[1])) + } + template_folder <- system.file(file.path("templates", template_location[2]), + package = template_location[1]) + if (!dir.exists(template_folder)) { + stop("template '", template_location[2],"'' not found in the package '", template_location[1], "'\n", + "Complete folder: ", template_folder) + } + + # Creating report destpath <- file.path(path, "reports", name) if (dir.exists(destpath)) stop("The folder ", @@ -52,6 +68,7 @@ add_report <- function(name, append = TRUE ) + # Setting template in tex files lapply(c("in_header", "before_body", "after_body"), function(x) { tex_content <- sprintf("\\input{../../templates/%s/%s}", template, x) if (x == "in_header") { @@ -70,6 +87,8 @@ add_report <- function(name, "% Add your own settings below to append or overwrite template settings") writeLines(tex_content, file.path(destpath, sprintf("%s.tex", x))) }) + # Setting template dependency + writeLines(template, file.path(destpath, ".template_dependencies")) tinytex_install_babel_language_support(lang) diff --git a/R/create_reports.R b/R/create_reports.R index a082b44..2bc5a4a 100644 --- a/R/create_reports.R +++ b/R/create_reports.R @@ -18,22 +18,6 @@ #' report_path <- add_report("my_report", path = path) #' list.files(report_path) create_reports <- function(path = ".", overwrite = FALSE, git = TRUE) { - # Copy templates - if (dir.exists(file.path(path, "templates"))) { - if (overwrite) { - warning("Folder ", file.path(path, "templates"), " is overwritten") - } else { - stop("Folder ", file.path(path, "templates"), " already exists") - } - } - - ok <- file.copy( - from = pkg_sys("templates"), - to = path, - recursive = TRUE - ) - if (!ok) stop("Error when copying 'templates' folder to ", path) - # Create reports folder reports_path <- file.path(path, "reports") dir.create(reports_path, showWarnings = FALSE) diff --git a/tests/testthat/test-add_report.R b/tests/testthat/test-add_report.R index 3396919..0df00e2 100644 --- a/tests/testthat/test-add_report.R +++ b/tests/testthat/test-add_report.R @@ -1,7 +1,7 @@ -test_that("add_report should raise error if reports does not exists", { +test_that("add_report should raise message if reports does not exists", { path <- tempfile(pattern = "dir") dir.create(path, recursive = TRUE) - expect_error(add_report("test", path = path)) + expect_message(add_report("test", path = path)) unlink(path, recursive = TRUE) }) @@ -9,7 +9,12 @@ test_that("add_report should raise error if template does not exists", { path <- tempfile(pattern = "dir") dir.create(path, recursive = TRUE) create_reports(path) - expect_error(add_report("test", template = "wrong_template", path = path)) + expect_error(add_report("test", template = "wrong_syntax", path = path), + regexp = "format") + expect_error(add_report("test", template = "ggplot2:inrae", path = path), + regexp = "templates") + expect_error(add_report("test", template = "fairify:wrong_template", path = path), + regexp = "template") unlink(path, recursive = TRUE) }) @@ -18,11 +23,12 @@ test_that("add_report should create a report", { dir.create(path, recursive = TRUE) create_reports(path) add_report("test", path = path) - expect_true( - all(file.exists( - file.path(path, - "reports/test", - list.files(pkg_sys("bookdown_template"))) - ))) + sapply(file.path(path, + "reports/test", + c(list.files(pkg_sys("bookdown_template")), + ".template_dependencies")), + function(x) expect_true(file.exists(!!x))) + expect_equal(readLines(file.path(path, "reports/test/.template_dependencies")), + "fairify:inrae") unlink(path, recursive = TRUE) }) -- GitLab From 4a2d1986e40d480160c9e4a7c91325dda40f73dc Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Sat, 9 Mar 2024 18:25:00 +0100 Subject: [PATCH 2/3] feat: use templates from package data - templates are copied from package inst/templates during render - handling of dependencies between templates - setup.R is template dependant now - _output.yml is also template dependant and completed recursively through dependency chain Refs #23 --- R/add_report.R | 72 +++++++++++++------ R/create_reports.R | 3 +- R/render_report.R | 50 +++++++++++++ R/render_reports.R | 2 +- inst/bookdown_template/setup.R | 7 -- inst/reports_gitignore.txt | 2 +- .../basic}/_output.yml | 5 +- inst/templates/{ => basic}/setup.R | 0 inst/templates/inrae/.template_dependencies | 1 + inst/templates/inrae/_output.yml | 6 ++ inst/templates/inrae/after_body.tex | 2 +- inst/templates/inrae/backcover_inrae.tex | 6 +- inst/templates/inrae/cover.tex | 6 +- inst/templates/inrae/in_header.tex | 7 +- inst/templates/inrae/preamble.tex | 6 +- inst/templates/inrae/setup.R | 1 + .../templates/umr_geau/.template_dependencies | 2 + inst/templates/umr_geau/after_body.tex | 2 +- inst/templates/umr_geau/in_header.tex | 16 ++++- inst/templates/umr_geau/setup.R | 1 + man/add_report.Rd | 2 +- man/create_reports.Rd | 4 +- tests/testthat/test-add_report.R | 11 +-- tests/testthat/test-create_reports.R | 4 +- tests/testthat/test-render_reports.R | 7 +- 25 files changed, 157 insertions(+), 68 deletions(-) delete mode 100644 inst/bookdown_template/setup.R rename inst/{bookdown_template => templates/basic}/_output.yml (58%) rename inst/templates/{ => basic}/setup.R (100%) create mode 100644 inst/templates/inrae/.template_dependencies create mode 100644 inst/templates/inrae/_output.yml create mode 100644 inst/templates/inrae/setup.R create mode 100644 inst/templates/umr_geau/.template_dependencies create mode 100644 inst/templates/umr_geau/setup.R diff --git a/R/add_report.R b/R/add_report.R index 0aedbba..b771981 100644 --- a/R/add_report.R +++ b/R/add_report.R @@ -12,7 +12,7 @@ #' @inherit create_reports examples #' add_report <- function(name, - template = "fairify:inrae", + template = "fairify:basic", lang = "english", path = pkgload::pkg_path()) { # reports folder handling @@ -21,20 +21,7 @@ add_report <- function(name, create_reports(path) } # Checking template existence - if (!grepl("^[a-zA-Z0-9_]*\\:[a-zA-Z0-9_]*$", template)) { - stop("`template` should be in the format `[package]:[template_name]`") - } - template_location <- strsplit(template, ":", fixed = TRUE)[[1]] - if (!dir.exists(system.file("templates", package = template_location[1]))) { - stop("'templates' folder not found in the package '", template_location[1], "'\n", - "Complete folder: ",system.file("templates", package = template_location[1])) - } - template_folder <- system.file(file.path("templates", template_location[2]), - package = template_location[1]) - if (!dir.exists(template_folder)) { - stop("template '", template_location[2],"'' not found in the package '", template_location[1], "'\n", - "Complete folder: ", template_folder) - } + template_location <- get_template_location(template) # Creating report destpath <- file.path(path, "reports", name) @@ -70,7 +57,7 @@ add_report <- function(name, # Setting template in tex files lapply(c("in_header", "before_body", "after_body"), function(x) { - tex_content <- sprintf("\\input{../../templates/%s/%s}", template, x) + tex_content <- sprintf("\\input{templates/%s/%s}", template_location[2], x) if (x == "in_header") { tex_content <- c( sprintf("\\usepackage[%s]{babel}", lang), @@ -79,18 +66,63 @@ add_report <- function(name, ) babel_lang_tex <- sprintf("templates/basic/babel_%s.tex", lang) if (file.exists(pkg_sys(babel_lang_tex))) { - tex_content <- c(tex_content, sprintf("\\input{../../%s}", babel_lang_tex)) + tex_content <- c(tex_content, sprintf("\\input{%s}", babel_lang_tex)) } } tex_content <- c(tex_content, - "\n", - "% Add your own settings below to append or overwrite template settings") + "\n% Add your own settings below to append or overwrite template settings") writeLines(tex_content, file.path(destpath, sprintf("%s.tex", x))) }) # Setting template dependency writeLines(template, file.path(destpath, ".template_dependencies")) - + # Setting the setup R script + cat(NULL, file = file.path(destpath, ".here")) + writeLines( + c(sprintf("source(here::here(\"templates/%s/setup.R\"))\n", template_location[2]), + "# pkgload::load_all() # Uncomment to load current fairyfied project code and data\n", + "# Add your own settings below to append or overwrite template settings"), + file.path(destpath, "setup.R") + ) + # Setting up the _output.yml file from templates + write_output_yml(destpath) + # Babel language support installation tinytex_install_babel_language_support(lang) return(destpath) } + +write_output_yml <- function(path) { + template_dependencies <- get_template_dependencies(path) + template_paths <- sapply(template_dependencies, function(x) { + get_template_location(x)["path"] + }) + output_paths <- file.path(template_paths, "_output.yml") + + output <- NULL + for(output_path in output_paths) { + if (file.exists(output_path)) { + template_output <- yaml::read_yaml(output_path) + output <- config::merge(output, template_output) + } + } + yaml::write_yaml(output, file.path(path, "_output.yml")) +} + +get_template_dependencies <- function(path) { + dep_chain <- NULL + i <- 0 + while(file.exists(file.path(path, ".template_dependencies"))) { + dep_chain <- rbind(dep_chain, + data.frame(path = unname(path), + template = readLines( + file.path(path, ".template_dependencies") + ))) + i <- i + 1 + path <- get_template_location(dep_chain$template[i])["path"] + } + if (!is.null(dep_chain)) { + return(unique(rev(dep_chain$template))) + } else { + return(NULL) + } +} diff --git a/R/create_reports.R b/R/create_reports.R index 2bc5a4a..8836679 100644 --- a/R/create_reports.R +++ b/R/create_reports.R @@ -1,7 +1,6 @@ #' Set reports folder structure #' #' @param path Destination folder for reports and templates -#' @param overwrite Allow overwriting templates folder #' @param git add gitignore file suitable for TeX and HTML reports #' #' @return The path to the created reports folder. @@ -17,7 +16,7 @@ #' # Add a new report #' report_path <- add_report("my_report", path = path) #' list.files(report_path) -create_reports <- function(path = ".", overwrite = FALSE, git = TRUE) { +create_reports <- function(path = ".", git = TRUE) { # Create reports folder reports_path <- file.path(path, "reports") dir.create(reports_path, showWarnings = FALSE) diff --git a/R/render_report.R b/R/render_report.R index 98fcd45..c6563e0 100644 --- a/R/render_report.R +++ b/R/render_report.R @@ -27,10 +27,13 @@ render_report <- function(input, full.names = TRUE) unlink(cache_dir, recursive = TRUE) } + on.exit({unlink(file.path(input, "templates"), recursive = TRUE)}) + copy_templates(input) cfg_bookdown <- yaml::read_yaml(file.path(input, "_bookdown.yml")) unlink(file.path(input, paste0(cfg_bookdown$book_filename, ".*"))) attached_packages <- (.packages()) + here:::do_refresh_here(input) bookdown::render_book(input, output_format = output_format, output_dir = output_dir, @@ -44,3 +47,50 @@ render_report <- function(input, character.only = TRUE)) invisible() } + +copy_templates <- function(input) { + if (file.exists(input) && !dir.exists(input)) { + # Remove index.Rmd or whatever the file to get the folder + template <- basename(template) + } + templates_path <- file.path(input, "templates") + dir.create(templates_path, showWarnings = FALSE) + template_dependencies <- get_template_dependencies(input) + templates <- lapply(template_dependencies, function(x) { + get_template_location(x) + }) + sapply(templates, function(template) { + ok <- file.copy( + from = template["path"], + to = templates_path, + recursive = TRUE + ) + if (!ok) stop("Error when copying template '", template["name"], "'\n", + "From: ", template["path"], "\n", + "To: ", templates_path) + }) + invisible() +} + +get_template_location <- function(template, err_msg = "") { + if (!grepl("^[a-zA-Z0-9_]*\\:[a-zA-Z0-9_]*$", template)) { + stop(err_msg, + "`template` should be in the format `[package]:[template_name]`, ", + "found: ", template) + } + template_location <- strsplit(template, ":", fixed = TRUE)[[1]] + names(template_location) <- c("pkg", "name") + if (!dir.exists(system.file("templates", package = template_location[1]))) { + stop(err_msg, + "'templates' folder not found in the package '", template_location[1], "'\n", + "Complete folder: ",system.file("templates", package = template_location[1])) + } + template_folder <- system.file(file.path("templates", template_location[2]), + package = template_location[1]) + if (!dir.exists(template_folder)) { + stop(err_msg, + "template '", template_location[2],"'' not found in the package '", template_location[1], "'\n", + "Complete folder: ", template_folder) + } + return(c(template_location, path = template_folder)) +} diff --git a/R/render_reports.R b/R/render_reports.R index 62011a1..02245e0 100644 --- a/R/render_reports.R +++ b/R/render_reports.R @@ -25,7 +25,7 @@ render_reports <- function(reports_dir = file.path(pkgload::pkg_path(), "reports invisible(sapply(reports, message)) for(report in reports){ - message("*******************************************************************") + message("\n*******************************************************************") message("** RENDER ", report) message("*******************************************************************") render_report(input = file.path(reports_dir, report), diff --git a/inst/bookdown_template/setup.R b/inst/bookdown_template/setup.R deleted file mode 100644 index ff3ca7a..0000000 --- a/inst/bookdown_template/setup.R +++ /dev/null @@ -1,7 +0,0 @@ -# Global report configuration -source("../../templates/setup.R") - -# Add below specific configuration for this report - -# Load this package and its configuration -# library(myPackage) diff --git a/inst/reports_gitignore.txt b/inst/reports_gitignore.txt index 0c4b87e..2fb2f5a 100644 --- a/inst/reports_gitignore.txt +++ b/inst/reports_gitignore.txt @@ -1,5 +1,6 @@ myBook.* */_book +*/templates */*_files */*_cache */config.yml @@ -10,4 +11,3 @@ myBook.* *.wrt *.rds *.pdf - diff --git a/inst/bookdown_template/_output.yml b/inst/templates/basic/_output.yml similarity index 58% rename from inst/bookdown_template/_output.yml rename to inst/templates/basic/_output.yml index b231b09..3689c5f 100644 --- a/inst/bookdown_template/_output.yml +++ b/inst/templates/basic/_output.yml @@ -1,11 +1,8 @@ bookdown::gitbook: - css: style.css config: toc: - before: | - <li><a href="./"><img src="../../templates/inrae/bloc_etat.png" width = "130"></a></li> after: | - <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li> + <li>Published with <a href="https://forgemia.inra.fr/umr-g-eau/fairify" target="blank">fairify</a> and <a href="https://github.com/rstudio/bookdown" target="blank">bookdown</a></li> download: ["pdf"] bookdown::pdf_book: includes: diff --git a/inst/templates/setup.R b/inst/templates/basic/setup.R similarity index 100% rename from inst/templates/setup.R rename to inst/templates/basic/setup.R diff --git a/inst/templates/inrae/.template_dependencies b/inst/templates/inrae/.template_dependencies new file mode 100644 index 0000000..775b482 --- /dev/null +++ b/inst/templates/inrae/.template_dependencies @@ -0,0 +1 @@ +fairify:basic diff --git a/inst/templates/inrae/_output.yml b/inst/templates/inrae/_output.yml new file mode 100644 index 0000000..d23c54d --- /dev/null +++ b/inst/templates/inrae/_output.yml @@ -0,0 +1,6 @@ +bookdown::gitbook: + css: style.css + config: + toc: + before: | + <li><img src="templates/inrae/bloc_etat.png" width = "130"></li> diff --git a/inst/templates/inrae/after_body.tex b/inst/templates/inrae/after_body.tex index 84aab12..1b3c304 100644 --- a/inst/templates/inrae/after_body.tex +++ b/inst/templates/inrae/after_body.tex @@ -8,7 +8,7 @@ \cleartobackcover -\input{../../templates/inrae/backcover_inrae} +\input{templates/inrae/backcover_inrae} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % END AFTER_BODY.TEX diff --git a/inst/templates/inrae/backcover_inrae.tex b/inst/templates/inrae/backcover_inrae.tex index b259edf..4b00d51 100644 --- a/inst/templates/inrae/backcover_inrae.tex +++ b/inst/templates/inrae/backcover_inrae.tex @@ -11,7 +11,7 @@ \begin{center} \color{inrae} - \includegraphics{../../templates/inrae/chevron.png} + \includegraphics{templates/inrae/chevron.png} \vspace{3mm} @@ -23,7 +23,7 @@ \vspace{5.5mm} Rejoignez-nous sur :\\ - \includegraphics{../../templates/inrae/reseaux_sociaux.png}\\ + \includegraphics{templates/inrae/reseaux_sociaux.png}\\ \textbf{\website} \vspace{27mm} @@ -34,7 +34,7 @@ \vspace{3cm} - \includegraphics{../../templates/inrae/logo_inrae_dos_bas.png} + \includegraphics{templates/inrae/logo_inrae_dos_bas.png} \end{center} \thispagestyle{empty} diff --git a/inst/templates/inrae/cover.tex b/inst/templates/inrae/cover.tex index 7108ded..016644f 100644 --- a/inst/templates/inrae/cover.tex +++ b/inst/templates/inrae/cover.tex @@ -2,14 +2,14 @@ \thispagestyle{empty} \tikz[remember picture,overlay] \node[opacity=1,inner sep=0pt] at (current page.center) { - \includegraphics[width=\paperwidth]{../../templates/inrae/inrae_fond_page.jpg} + \includegraphics[width=\paperwidth]{templates/inrae/inrae_fond_page.jpg} }; \vspace{-1cm}% \hfill% \begin{minipage}[t][6cm][t]{0.20\textwidth} \begin{center} - \input{../../templates/inrae/logos} + \input{templates/inrae/logos} \end{center} \end{minipage} @@ -21,7 +21,7 @@ \vspace{1cm} -\includegraphics{../../templates/inrae/chevron.png}\\ +\includegraphics{templates/inrae/chevron.png}\\ \vspace{-3mm} diff --git a/inst/templates/inrae/in_header.tex b/inst/templates/inrae/in_header.tex index e41bcde..3e69a6b 100644 --- a/inst/templates/inrae/in_header.tex +++ b/inst/templates/inrae/in_header.tex @@ -2,21 +2,20 @@ % BEGIN PREAMBLE.TEX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\newcommand{\insertfirstlogo}{\includegraphics[width=4cm]{../../templates/inrae/logo_inrae_couv_haut.png}} \newcommand{\insertsecondlogo}{} \newcommand{\insertthirdlogo}{} \newcommand{\insertfourthlogo}{} \newcommand{\insertcoverimage}{} -\input{../../templates/basic/in_header} -\input{../../templates/inrae/preamble.tex} +\input{templates/basic/in_header} +\input{templates/inrae/preamble.tex} % Première de couverture \usepackage{tikz} \usepackage{titling} \makeatletter \def\maketitle{% -\input{../../templates/inrae/cover.tex} +\input{templates/inrae/cover.tex} } \makeatother diff --git a/inst/templates/inrae/preamble.tex b/inst/templates/inrae/preamble.tex index 467e03d..bdd5128 100644 --- a/inst/templates/inrae/preamble.tex +++ b/inst/templates/inrae/preamble.tex @@ -67,7 +67,7 @@ %\let\marginpar\oldmarginpar % Definition des couleurs -\RequirePackage{../../templates/inrae/inraecolors} +\RequirePackage{templates/inrae/inraecolors} % Pourquoi les couleurs chargées depuis "inraecolors" ne sont-elles pas disponibles pour \hypersetup ci-dessous ? \definecolor{titreColor}{HTML}{008C8E} % inraeDark @@ -110,7 +110,7 @@ \setlength{\parskip}{0.5em} % Format des titres utilisés dans le préambule du rapport (Auteurs, résumé...) -\newcommand{\titrepreambule}[1]{ +\newcommand{\titrepreamble}[1]{ \vspace{2\baselineskip} {\sffamily\Large\color{titreColor}\scshape{#1}} \vspace{\baselineskip}} @@ -134,4 +134,4 @@ % Acronymes \usepackage[printonlyused,withpage]{acronym} -\newcommand{\insertfirstlogo}{\includegraphics[width=3.5cm]{../../templates/inrae/logo_inrae_couv_haut.png}} +\newcommand{\insertfirstlogo}{\includegraphics[width=3.5cm]{templates/inrae/logo_inrae_couv_haut.png}} diff --git a/inst/templates/inrae/setup.R b/inst/templates/inrae/setup.R new file mode 100644 index 0000000..67ef853 --- /dev/null +++ b/inst/templates/inrae/setup.R @@ -0,0 +1 @@ +source(here::here("templates/basic/setup.R")) diff --git a/inst/templates/umr_geau/.template_dependencies b/inst/templates/umr_geau/.template_dependencies new file mode 100644 index 0000000..345bf38 --- /dev/null +++ b/inst/templates/umr_geau/.template_dependencies @@ -0,0 +1,2 @@ +fairify:inrae +fairify:basic diff --git a/inst/templates/umr_geau/after_body.tex b/inst/templates/umr_geau/after_body.tex index 44cc7cd..b697b2a 100644 --- a/inst/templates/umr_geau/after_body.tex +++ b/inst/templates/umr_geau/after_body.tex @@ -1 +1 @@ -\input{../../templates/inrae/after_body} +\input{templates/inrae/after_body} diff --git a/inst/templates/umr_geau/in_header.tex b/inst/templates/umr_geau/in_header.tex index 9a9b524..354dc18 100644 --- a/inst/templates/umr_geau/in_header.tex +++ b/inst/templates/umr_geau/in_header.tex @@ -4,9 +4,19 @@ %\usepackage[french]{babel} -\newcommand{\insertsecondlogo}{\includegraphics[width=4cm]{../../templates/umr_geau/logo_umr_geau.png}} +\newcommand{\insertsecondlogo}{\includegraphics[width=4cm]{templates/umr_geau/logo_umr_geau.png}} \newcommand{\insertthirdlogo}{} \newcommand{\insertfourthlogo}{} +\newcommand{\insertcoverimage}{} + +% Première de couverture +\usepackage{tikz} +\usepackage{titling} +\makeatletter +\def\maketitle{% +\input{templates/inrae/cover.tex} +} +\makeatother \newcommand{\service}{UMR G-EAU} \newcommand{\site}{ @@ -22,8 +32,8 @@ \url{www.inrae.fr} } -\input{../../templates/basic/in_header} -\input{../../templates/inrae/preambule_inrae.tex} +\input{templates/basic/in_header} +\input{templates/inrae/preamble.tex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % END PREAMBLE.TEX diff --git a/inst/templates/umr_geau/setup.R b/inst/templates/umr_geau/setup.R new file mode 100644 index 0000000..ce24fd2 --- /dev/null +++ b/inst/templates/umr_geau/setup.R @@ -0,0 +1 @@ +source(here::here("templates/inrae/setup.R")) diff --git a/man/add_report.Rd b/man/add_report.Rd index 7af2577..27b30e0 100644 --- a/man/add_report.Rd +++ b/man/add_report.Rd @@ -6,7 +6,7 @@ \usage{ add_report( name, - template = "inrae", + template = "fairify:basic", lang = "english", path = pkgload::pkg_path() ) diff --git a/man/create_reports.Rd b/man/create_reports.Rd index f61a6aa..107ace1 100644 --- a/man/create_reports.Rd +++ b/man/create_reports.Rd @@ -4,13 +4,11 @@ \alias{create_reports} \title{Set reports folder structure} \usage{ -create_reports(path = ".", overwrite = FALSE, git = TRUE) +create_reports(path = ".", git = TRUE) } \arguments{ \item{path}{Destination folder for reports and templates} -\item{overwrite}{Allow overwriting templates folder} - \item{git}{add gitignore file suitable for TeX and HTML reports} } \value{ diff --git a/tests/testthat/test-add_report.R b/tests/testthat/test-add_report.R index 0df00e2..b9822c7 100644 --- a/tests/testthat/test-add_report.R +++ b/tests/testthat/test-add_report.R @@ -8,7 +8,6 @@ test_that("add_report should raise message if reports does not exists", { test_that("add_report should raise error if template does not exists", { path <- tempfile(pattern = "dir") dir.create(path, recursive = TRUE) - create_reports(path) expect_error(add_report("test", template = "wrong_syntax", path = path), regexp = "format") expect_error(add_report("test", template = "ggplot2:inrae", path = path), @@ -21,14 +20,16 @@ test_that("add_report should raise error if template does not exists", { test_that("add_report should create a report", { path <- tempfile(pattern = "dir") dir.create(path, recursive = TRUE) - create_reports(path) - add_report("test", path = path) + add_report("test", template = "fairify:umr_geau", path = path) sapply(file.path(path, "reports/test", c(list.files(pkg_sys("bookdown_template")), - ".template_dependencies")), + ".template_dependencies", + "_output.yml")), function(x) expect_true(file.exists(!!x))) expect_equal(readLines(file.path(path, "reports/test/.template_dependencies")), - "fairify:inrae") + "fairify:umr_geau") + output_yml <- yaml::read_yaml(file.path(path, "reports/test/_output.yml")) + expect_true(grepl("templates/inrae", output_yml$`bookdown::gitbook`$config$toc$before)) unlink(path, recursive = TRUE) }) diff --git a/tests/testthat/test-create_reports.R b/tests/testthat/test-create_reports.R index dcec0e2..f8bd485 100644 --- a/tests/testthat/test-create_reports.R +++ b/tests/testthat/test-create_reports.R @@ -3,8 +3,6 @@ test_that("create_reports works", { 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)) + expect_true(all(dir.exists(file.path(path, "reports")))) unlink(path, recursive = TRUE) }) diff --git a/tests/testthat/test-render_reports.R b/tests/testthat/test-render_reports.R index 40e20ae..4e753b8 100644 --- a/tests/testthat/test-render_reports.R +++ b/tests/testthat/test-render_reports.R @@ -2,15 +2,16 @@ test_that("render_reports works for all templates", { path <- helper_create_fairify() dfC <- expand.grid( - template = list.dirs(file.path(path, "templates"), full.names = FALSE)[-1], - lang = c("french", "english") + template = list.dirs(pkg_sys("templates"), full.names = FALSE)[-1], + lang = c("french", "english"), + stringsAsFactors = FALSE ) mapply( FUN = function(template, lang, path) { add_report( paste("my_report", template, lang, sep = "_"), - template = template, + template = paste0("fairify:",template), lang = lang, path = path ) -- GitLab From 54d1c38e97d13c8ad197b701561f4a296c115f7a Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Sat, 9 Mar 2024 18:30:46 +0100 Subject: [PATCH 3/3] fix: missing dependency package here For fixing working directory to the report directory when rendering a report Refs #23 --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 46c59ea..d0e6598 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,6 +15,7 @@ Imports: devtools, dplyr, gert, + here, httr, jsonlite, magrittr, -- GitLab