Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
42 changes: 33 additions & 9 deletions R/to_rmd.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
.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")) {
"```{r echo = FALSE, eval = TRUE, fig.width = %f, fig.height = %f}\n._figure <- readRDS('%s')\ngrid::grid.newpage()\ngrid::grid.draw(._figure)\n```" # nolint line_length_linter.
} 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`
Expand Down