Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow html content to be added to a report #294

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
47 changes: 47 additions & 0 deletions R/HTMLBlock.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' @title `HTMLBlock`
#' @docType class
#' @description
#' Specialized `FileBlock` for managing HTML content in reports.
#' It's designed to handle various HTML content, and render the report as HTML,
#' however `htmlwidgets` objects can also be rendered to static document-ready format.
#'
#' @keywords internal
HTMLBlock <- R6::R6Class( # nolint: object_name_linter.
classname = "HTMLBlock",
inherit = FileBlock,
public = list(
#' @description Initialize a `HTMLBlock` object.
#'
#' @param content An object that can be rendered as a HTML content assigned to
#' this `HTMLBlock`
#'
#' @return Object of class `HTMLBlock`, invisibly.
initialize = function(content) {
if (!missing(content)) {
vedhav marked this conversation as resolved.
Show resolved Hide resolved
checkmate::assert_multi_class(content, c("shiny.tag", "shiny.tag.list", "htmlwidget"))
self$set_content(content)
}
invisible(self)
},
#' @description Sets content of this `HTMLBlock`.
#'
#' @param content An object that can be rendered as a HTML content
#' assigned to this `HTMLBlock`
#'
#' @return `self`, invisibly.
#' @examples
#' HTMLBlock <- getFromNamespace("HTMLBlock", "teal.reporter")
#' block <- HTMLBlock$new()
#' block$set_content(shiny::div("HTML Content"))
#'
set_content = function(content) {
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
path <- tempfile(fileext = ".rds")
saveRDS(content, file = path)
super$set_content(path)
invisible(self)
}
),
private = list(),
lock_objects = TRUE,
lock_class = TRUE
)
2 changes: 2 additions & 0 deletions R/Previewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ block_to_html <- function(b) {
)
} else if (inherits(b, "NewpageBlock")) {
shiny::tags$br()
} else if (inherits(b, "HTMLBlock")) {
readRDS(b_content)
} else {
stop("Unknown block class")
}
Expand Down
12 changes: 11 additions & 1 deletion R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
#' result_path <- Renderer$new()$renderRmd(reporter$get_blocks(), yaml_header)
#'
renderRmd = function(blocks, yaml_header, global_knitr = getOption("teal.reporter.global_knitr")) {
checkmate::assert_list(blocks, c("TextBlock", "PictureBlock", "NewpageBlock", "TableBlock", "RcodeBlock"))
checkmate::assert_list(
blocks,
c("TextBlock", "PictureBlock", "NewpageBlock", "TableBlock", "RcodeBlock", "HTMLBlock")
)
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get()))

if (missing(yaml_header)) {
Expand Down Expand Up @@ -220,6 +223,8 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
private$tableBlock2md(block)
} else if (inherits(block, "NewpageBlock")) {
block$get_content()
} else if (inherits(block, "HTMLBlock")) {
private$htmlBlock2md(block)
} else {
stop("Unknown block class")
}
Expand Down Expand Up @@ -275,6 +280,11 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
basename_table <- basename(block$get_content())
file.copy(block$get_content(), file.path(private$output_dir, basename_table))
sprintf("```{r echo = FALSE}\nreadRDS('%s')\n```", basename_table)
},
htmlBlock2md = function(block) {
basename_content <- basename(block$get_content())
file.copy(block$get_content(), file.path(private$output_dir, basename_content))
sprintf("```{r echo = FALSE}\nreadRDS('%s')\n```", basename_content)
gogonzo marked this conversation as resolved.
Show resolved Hide resolved
}
),
lock_objects = TRUE,
Expand Down
11 changes: 11 additions & 0 deletions R/ReportCard.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
self$append_content(TableBlock$new(table))
invisible(self)
},
#' @description Appends a html content to this `ReportCard`.
#'
#' @param content An object that can be rendered as a HTML content.
#' @return `self`, invisibly.
#' @examples
#' card <- ReportCard$new()$append_html(shiny::div("HTML Content"))
#'
append_html = function(content) {
self$append_content(HTMLBlock$new(content))
invisible(self)
},
#' @description Appends a plot to this `ReportCard`.
#'
#' @param plot (`ggplot` or `grob` or `trellis`) plot object.
Expand Down
115 changes: 115 additions & 0 deletions man/HTMLBlock.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions man/ReportCard.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ testthat::test_that("renderRmd asserts the argument is a list of TextBlocks/Pict
renderer <- Renderer$new()
testthat::expect_error(
renderer$renderRmd(append(blocks, "STH")),
regexp = "May only contain the following types: \\{TextBlock,PictureBlock,NewpageBlock,TableBlock,RcodeBlock\\}"
regexp = "May only contain the following types: \\{TextBlock,PictureBlock,NewpageBlock,TableBlock,RcodeBlock,HTMLBlock\\}" # nolint line_length
)
})

Expand Down
Loading