From 63fa726067ce6c9d1e32ad3f539bf6bd282fe4ed Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:41:11 +0200 Subject: [PATCH 1/9] Add note about parameter names in design document --- vignettes/design.Rmd | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vignettes/design.Rmd b/vignettes/design.Rmd index cc33dcb6..2395efae 100644 --- a/vignettes/design.Rmd +++ b/vignettes/design.Rmd @@ -16,6 +16,15 @@ knitr::opts_chunk$set( As always during software development, we were faced with tough design choices while developing this package. We want to clearly document which choices were made, and what are the reasons behind them. +## Report parameters + +### Names + +As much as possible, parameter names should be re-used across reports. This allows us to: + +- provide a consistent user experience +- pass a common set of arguments in a potential wrapper function that would render multiple wrappers at once + ## Package and version management Breaking changes during R package updates in one of the major sources of non-[reproducibility](https://cran.r-project.org/web/views/ReproducibleResearch.html) and of headaches for R users of all levels. To guard users against this, but also to ensure that the provided templates work out of the box, throughout the time a given `{episoap}` version is on CRAN, we [pin specific versions for our dependencies](https://cloud.google.com/blog/topics/developers-practitioners/best-practices-dependency-management), via `{renv}`. From 315d46b6723f1af45d3035e32dd5def05cebb91f Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:08:08 +0200 Subject: [PATCH 2/9] Create run_pipeline() first draft --- NAMESPACE | 1 + R/run_pipeline.R | 62 +++++++++++++++++++++++++++++++++++++++++++++ man/run_pipeline.Rd | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 R/run_pipeline.R create mode 100644 man/run_pipeline.Rd diff --git a/NAMESPACE b/NAMESPACE index a0a451c3..81f73abe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand export(list_templates) +export(run_pipeline) diff --git a/R/run_pipeline.R b/R/run_pipeline.R new file mode 100644 index 00000000..1fe61002 --- /dev/null +++ b/R/run_pipeline.R @@ -0,0 +1,62 @@ +#' Create report from template +#' +#' @param report A character vector of report template(s) to render (default to all +#' templates) +#' @param out_dir A character vector with the output directory (default to an +#' `episoap_report` in the current directory +#' @param preview A logical (default `TRUE`) indicating whether the rendered +#' report should be opened in the default browser +#' +#' @returns (invisibly) a character vector of paths to the rendered reports +#' +#' @export +#' +#' @examples +#' # Download data we need for this example +#' wd <- file.path(tempdir(), "episoap_report") +#' dir.create(wd) +#' download.file( +#' "https://github.com/epiverse-trace/episoap/blob/main/inst/rmarkdown/templates/transmissibility/skeleton/data/covid_linelist_england.rds?raw=true", +#' file.path(wd, "covid_linelist_england.rds") +#' ) +#' # Running the pipeline with custom data saved on your computer +#' run_pipeline( +#' report = "transmissibility", +#' out_dir = wd, +#' data_file = "covid_linelist_england.rds" +#' ) +run_pipeline <- function( + report = list_templates(), + out_dir = "episoap_report", + preview = TRUE, + ... +) { + + if (!dir.exists(out_dir)) { + dir.create(out_dir, recursive = TRUE) + } + + withr::local_dir(out_dir) + + report <- match.arg(report) + + rendered <- vapply(report, function(r) { + rmarkdown::draft( + file = paste0(r, ".Rmd"), + template = r, + create_dir = FALSE, + package = "episoap", + edit = FALSE + ) + rmarkdown::render( + paste0(r, ".Rmd"), + params = list(...) + ) + }, character(1)) + + if (preview) { + utils::browseURL(rendered[[1]]) + } + + return(invisible(rendered)) +} diff --git a/man/run_pipeline.Rd b/man/run_pipeline.Rd new file mode 100644 index 00000000..c3febf2b --- /dev/null +++ b/man/run_pipeline.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/run_pipeline.R +\name{run_pipeline} +\alias{run_pipeline} +\title{Create report from template} +\usage{ +run_pipeline( + report = list_templates(), + out_dir = "episoap_report", + preview = TRUE, + ... +) +} +\arguments{ +\item{report}{A character vector of report template(s) to render (default to all +templates)} + +\item{out_dir}{A character vector with the output directory (default to an +\code{episoap_report} in the current directory} + +\item{preview}{A logical (default \code{TRUE}) indicating whether the rendered +report should be opened in the default browser} +} +\value{ +(invisibly) a character vector of paths to the rendered reports +} +\description{ +Create report from template +} +\examples{ +# Download data we need for this example +wd <- file.path(tempdir(), "episoap_report") +dir.create(wd) +download.file( + "https://github.com/epiverse-trace/episoap/blob/main/inst/rmarkdown/templates/transmissibility/skeleton/data/covid_linelist_england.rds?raw=true", + file.path(wd, "covid_linelist_england.rds") +) +# Running the pipeline with custom data saved on your computer +run_pipeline( + report = "transmissibility", + out_dir = wd, + data_file = "covid_linelist_england.rds" +) +} From 245e8e904c0437a3e0c0dc2983dab20f69e0bb06 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:10:37 +0200 Subject: [PATCH 3/9] Add withr dependency to DESCRIPTION --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2e62e761..b66718c2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,8 @@ URL: https://epiverse-trace.github.io/episoap, BugReports: https://github.com/epiverse-trace/episoap/issues Imports: renv (>= 0.13.0), - rmarkdown + rmarkdown, + withr Suggests: knitr, spelling, From 4c1811d248714c83f7c5fe0b30a9e97cdec94cb4 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:11:55 +0200 Subject: [PATCH 4/9] Document ellipsis --- R/run_pipeline.R | 1 + man/run_pipeline.Rd | 2 ++ 2 files changed, 3 insertions(+) diff --git a/R/run_pipeline.R b/R/run_pipeline.R index 1fe61002..2f5c4add 100644 --- a/R/run_pipeline.R +++ b/R/run_pipeline.R @@ -6,6 +6,7 @@ #' `episoap_report` in the current directory #' @param preview A logical (default `TRUE`) indicating whether the rendered #' report should be opened in the default browser +#' @param ... Arguments passed as parameters to the report template(s) #' #' @returns (invisibly) a character vector of paths to the rendered reports #' diff --git a/man/run_pipeline.Rd b/man/run_pipeline.Rd index c3febf2b..7ea760b6 100644 --- a/man/run_pipeline.Rd +++ b/man/run_pipeline.Rd @@ -20,6 +20,8 @@ templates)} \item{preview}{A logical (default \code{TRUE}) indicating whether the rendered report should be opened in the default browser} + +\item{...}{Arguments passed as parameters to the report template(s)} } \value{ (invisibly) a character vector of paths to the rendered reports From 2f8124bfcea5e86f82fc21142632ac976ef67c05 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:17:36 +0200 Subject: [PATCH 5/9] Fix typo --- vignettes/design.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/design.Rmd b/vignettes/design.Rmd index 2395efae..34d4f70a 100644 --- a/vignettes/design.Rmd +++ b/vignettes/design.Rmd @@ -23,7 +23,7 @@ As always during software development, we were faced with tough design choices w As much as possible, parameter names should be re-used across reports. This allows us to: - provide a consistent user experience -- pass a common set of arguments in a potential wrapper function that would render multiple wrappers at once +- pass a common set of arguments in a potential wrapper function that would render multiple reports at once ## Package and version management From 645d32438492ecdc15399312a2e66cd8c367df21 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:18:18 +0200 Subject: [PATCH 6/9] Allow multiple report to be rendered at once --- R/run_pipeline.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/run_pipeline.R b/R/run_pipeline.R index 2f5c4add..6ddedf9e 100644 --- a/R/run_pipeline.R +++ b/R/run_pipeline.R @@ -39,7 +39,7 @@ run_pipeline <- function( withr::local_dir(out_dir) - report <- match.arg(report) + report <- match.arg(report, several.ok = TRUE) rendered <- vapply(report, function(r) { rmarkdown::draft( From 44ce47af827948997fc990255d33aa010abbbcd1 Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:19:19 +0200 Subject: [PATCH 7/9] Document where data_file arg comes from --- R/run_pipeline.R | 1 + man/run_pipeline.Rd | 1 + 2 files changed, 2 insertions(+) diff --git a/R/run_pipeline.R b/R/run_pipeline.R index 6ddedf9e..69a5c15f 100644 --- a/R/run_pipeline.R +++ b/R/run_pipeline.R @@ -24,6 +24,7 @@ #' run_pipeline( #' report = "transmissibility", #' out_dir = wd, +#' # this is passed as a rmarkdown parameter to the report #' data_file = "covid_linelist_england.rds" #' ) run_pipeline <- function( diff --git a/man/run_pipeline.Rd b/man/run_pipeline.Rd index 7ea760b6..8310378f 100644 --- a/man/run_pipeline.Rd +++ b/man/run_pipeline.Rd @@ -41,6 +41,7 @@ download.file( run_pipeline( report = "transmissibility", out_dir = wd, + # this is passed as a rmarkdown parameter to the report data_file = "covid_linelist_england.rds" ) } From 8e2a1debc48586da696e1ca35d879073281355bc Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:23:01 +0200 Subject: [PATCH 8/9] Add Karim as aut Since this is an implementation of his design --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index b66718c2..d8860fed 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,6 +6,7 @@ Authors@R: c( comment = c(ORCID = "0000-0002-4094-1476")), person("Thibaut", "Jombart", role = "aut"), person("Carmen", "Tamayo", role = "aut", comment = c(ORCID = "0000-0003-4184-2864")), + person("Karim", "Mané", role = "aut", comment = c(ORCID = "0000-0002-9892-2999")), person("data.org", role = "fnd") ) Description: A store of outbreak analytics pipelines using different From f6e5a9bd3951232af7f4ef839b38a96a65ec2c4b Mon Sep 17 00:00:00 2001 From: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> Date: Fri, 3 May 2024 12:42:49 +0200 Subject: [PATCH 9/9] Wrap in render() in Rscript_call() to isolate env session --- DESCRIPTION | 3 ++- R/run_pipeline.R | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d8860fed..1f5bc70c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,8 @@ BugReports: https://github.com/epiverse-trace/episoap/issues Imports: renv (>= 0.13.0), rmarkdown, - withr + withr, + xfun Suggests: knitr, spelling, diff --git a/R/run_pipeline.R b/R/run_pipeline.R index 69a5c15f..1f15c39d 100644 --- a/R/run_pipeline.R +++ b/R/run_pipeline.R @@ -50,9 +50,10 @@ run_pipeline <- function( package = "episoap", edit = FALSE ) - rmarkdown::render( - paste0(r, ".Rmd"), - params = list(...) + # We run in a new session the prevent renv to leak in the user's environment + xfun::Rscript_call( + rmarkdown::render, + list(input = paste0(r, ".Rmd"), params = list(...)) ) }, character(1))