diff --git a/DESCRIPTION b/DESCRIPTION index 71ff2a38..70834be3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,6 +22,7 @@ URL: https://github.com/insightsengineering/teal.reporter, https://insightsengineering.github.io/teal.reporter/ BugReports: https://github.com/insightsengineering/teal.reporter/issues Imports: + archivist (>= 2.3.0), bsicons, bslib (>= 0.8.0), checkmate (>= 2.1.0), diff --git a/R/render.R b/R/render.R index 167d161b..4f29d1f5 100644 --- a/R/render.R +++ b/R/render.R @@ -45,7 +45,8 @@ render <- function( temp_rmd_content <- to_rmd( block = input, global_knitr = c(global_knitr, list(eval = FALSE)), # we don't want to rerun evaluated code chunks to render - include_chunk_output = TRUE + include_chunk_output = TRUE, + output_dir = output_dir ) cat(temp_rmd_content, file = rmd_filepath) args <- utils::modifyList(list(...), list(input = rmd_filepath)) diff --git a/R/to_rmd.R b/R/to_rmd.R index 3dc8e0c7..70c9e237 100644 --- a/R/to_rmd.R +++ b/R/to_rmd.R @@ -1,12 +1,11 @@ -.content_to_rmd <- function(block, ...) { - path <- basename(tempfile(pattern = "report_item_", fileext = ".rds")) - suppressWarnings(saveRDS(block, file = path)) - sprintf("```{r echo = FALSE, eval = TRUE}\nreadRDS('%s')\n```", path) +.content_to_rmd <- function(block, output_dir = getwd(), ...) { + archived_obj <- .archive(block, output_dir) + sprintf("```{r echo = FALSE, eval = TRUE}\nreadRDS('%s')\n```", + file.path(archived_obj$repo_dir, archived_obj$path)) } -.plot_to_rmd <- function(block, ...) { - path <- basename(tempfile(pattern = "report_item_", fileext = ".rds")) - suppressWarnings(saveRDS(block, file = path)) +.plot_to_rmd <- function(block, output_dir = getwd(), ...) { + archived_obj <- .archive(block, output_dir) dims <- .determine_default_dimensions(block, convert_to_inches = TRUE) chunk <- if (inherits(block, "grob")) { @@ -14,13 +13,38 @@ } else { "```{r echo = FALSE, eval = TRUE, fig.width = %f, fig.height = %f}\nreadRDS('%s')\n```" } - + sprintf( chunk, dims$width, dims$height, - path + file.path(archived_obj$repo_dir, archived_obj$path) + ) +} + +#' Archive object to archivist repository +#' +#' @param obj (`any`) object to archive +#' @param repo_dir (`character(1)`) path to archivist repository +#' +#' @return `list` with `path` (artifact ID) and `repo_dir` +#' +#' @keywords internal +.archive <- function(obj, repo_dir) { + if (!dir.exists(repo_dir)) { + dir.create(repo_dir, recursive = TRUE, showWarnings = FALSE) + } + + if (!file.exists(file.path(repo_dir, "backpack.db"))) { + archivist::createLocalRepo(repoDir = repo_dir, default = TRUE) + } + + artifact_id <- archivist::saveToLocalRepo( + obj = obj, + repoDir = repo_dir ) + + list(path = artifact_id, repo_dir = repo_dir) } #' Convert `ReporterCard`/`teal_card` content to `rmarkdown`