Skip to content

Commit

Permalink
Merge 87fe53a into d62ceeb
Browse files Browse the repository at this point in the history
  • Loading branch information
Polkas authored Jun 15, 2022
2 parents d62ceeb + 87fe53a commit 2ca8d9a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 201 deletions.
76 changes: 22 additions & 54 deletions R/ReportCard.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
#'
initialize = function() {
private$content <- list()
private$chr_converters <- list()
private$metadata <- list()
invisible(self)
},
#' @description Appends a table to this `ReportCard`.
Expand Down Expand Up @@ -56,37 +56,26 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
},
#' @description Returns the content of this `ReportCard`.
#'
#' @param raw (`logical`) whether to include `content` as it was appended or apply `chr_converter`
#' functions on meta data objects.
#' @return `list()` list of `TableBlock`, `TextBlock` and `PictureBlock`.
#' If the `raw` argument is `TRUE`, meta data objects in the form they were added will be returned. Otherwise,
#' given `chr_converter` function given will be applied on these objects.
#' Only metadata elements are named.
#' @examples
#' card <- ReportCard$new()$append_text("Some text")$append_plot(
#' ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
#' )$append_metadata("rc", "a <- 2 + 2")
#' card <- ReportCard$new()$append_text("Some text")$append_metadata("rc", "a <- 2 + 2")
#'
#' card$get_content()
#'
get_content = function(raw = FALSE) {
checkmate::assert_logical(raw)
if (!raw) {
lapply(
seq_along(private$content),
function(index) {
if (inherits(private$content[[index]], "ContentBlock")) {
private$content[[index]]
} else {
key <- names(private$content)[index]
chr_converter_func <- private$chr_converters[[key]]
TextBlock$new(chr_converter_func(private$content[[index]]))
}
}
)
} else {
private$content
}
#'
get_content = function() {
private$content
},
#' @description Returns the metadata of this `ReportCard`.
#'
#' @return `named list` list of elements.
#' @examples
#' card <- ReportCard$new()$append_text("Some text")$append_metadata("rc", "a <- 2 + 2")
#'
#' card$get_metadata()
#'
get_metadata = function() {
private$metadata
},
#' @description Appends content elements to this `ReportCard`.
#'
Expand All @@ -95,42 +84,21 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
#' @param chr_converter (`function`) to convert a value to a character, by default `base::deparse1`.
#' @return invisibly self
#' @examples
#' custom_lm_chr_converter <- function(x) paste(capture.output(summary(x)), collapse = "\\n ")
#' card <- ReportCard$new()$append_text("Some text")$append_plot(
#' ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
#' )$append_text("Some text")$append_metadata(key = "lm",
#' value = lm(Ozone ~ Solar.R, airquality),
#' chr_converter = custom_lm_chr_converter)
#' value = lm(Ozone ~ Solar.R, airquality))
#' card$get_content()
#' card$get_content(raw = TRUE)
#' card$get_metadata()
#'
append_metadata = function(key, value, chr_converter = deparse1) {
append_metadata = function(key, value) {
checkmate::assert_character(key, min.len = 0, max.len = 1)
checkmate::assert_false(checkmate::test_choice(key, names(private$chr_converters)))
checkmate::assert_function(chr_converter)
checkmate::assert_string(chr_converter(value))

checkmate::assert_false(key %in% names(private$metadata))
meta_list <- list()
meta_list[[key]] <- value
private$content <- append(private$content, meta_list)
private$chr_converters[[key]] <- chr_converter
private$metadata <- append(private$metadata, meta_list)
invisible(self)
},
#' @description get all `chr_converter` functions of this `ReportCard`.
#' @return `named list`
#' @examples
#' custom_lm_chr_converter <- function(x) paste(capture.output(summary(x)), collapse = "\\n ")
#' card <- ReportCard$new()$append_text("Some text")$append_plot(
#' ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
#' )$append_text("Some text")$append_metadata(key = "lm",
#' value = lm(Ozone ~ Solar.R, airquality),
#' chr_converter = custom_lm_chr_converter
#' )$append_metadata(key = "code", value = lm(Ozone ~ Solar.R, airquality))
#' card$get_chr_converters()
#'
get_chr_converters = function() {
private$chr_converters
},
#' @description get the Card name
#'
#' @return `character` a Card name
Expand All @@ -153,7 +121,7 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
),
private = list(
content = list(),
chr_converters = list(),
metadata = list(),
name = character(0),
# @description The copy constructor.
#
Expand Down
33 changes: 19 additions & 14 deletions R/TealReportCard.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,52 @@ TealReportCard <- R6::R6Class( # nolint: object_name_linter.
#' @description Appends the source code to the `content` meta data of this `TealReportCard`.
#'
#' @param src (`character(1)`) code as text.
#' @param chr_converter (`function`) to convert `src` argument to a string,
#' by default `function(x) paste0("```\n", paste(x, collapse = "\n"), "\n```\n")`.
#' @return invisibly self
#' @examples
#' card <- TealReportCard$new()$append_src(
#' "ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()"
#' )
#' card$get_content()[[1]]$get_content()
append_src = function(src, chr_converter = function(x) paste0("```\n", paste(x, collapse = "\n"), "\n```\n")) {
append_src = function(src) {
checkmate::assert_character(src, min.len = 0, max.len = 1)
super$append_metadata("SRC", src, chr_converter)
super$append_text(src, "verbatim")
super$append_metadata("SRC", src)
invisible(self)
},
#' @description Appends the filter state list to the `content` meta data of this `TealReportCard`.
#'
#' @param fs (`list`) list of filter states.
#' @param chr_converter (`function`) to convert `fs` argument to a string, by default `base::deparse1`.
#' @param fs (`FilteredData`) with filter states.
#' @return invisibly self
#' @examples
#' card <- TealReportCard$new()$append_fs(
#' list(data = list(X = list(selected = c(1, 10))))
#' # Artificial FilteredData class
#' fs <- R6::R6Class("FilteredData",
#' public = list(
#' get_filter_state = function() list(a = 1, b = 3),
#' get_formatted_filter_state = function() "a = 1 and b = 3"
#' )
#' )
#' fs_inst <- fs$new()
#' card <- TealReportCard$new()$append_fs(fs_inst)
#' card$get_content()[[1]]$get_content()
#'
append_fs = function(fs, chr_converter = deparse1) {
checkmate::assert_list(fs)
super$append_metadata("FS", fs, chr_converter)
append_fs = function(fs) {
checkmate::assert_class(fs, "FilteredData")
super$append_text(fs$get_formatted_filter_state(), "verbatim")
super$append_metadata("FS", fs$get_filter_state())
invisible(self)
},
#' @description Appends the encodings list to the `content` meta data of this `TealReportCard`.
#'
#' @param encodings (`list`) list of encodings selections of the teal app.
#' @param chr_converter (`function`) to convert a encodings to a string, by default `base::deparse1`.
#' @return invisibly self
#' @examples
#' card <- TealReportCard$new()$append_encodings(list("variable 1 is X"))
#' card$get_content()[[1]]$get_content()
#'
append_encodings = function(encodings, chr_converter = deparse1) {
append_encodings = function(encodings) {
checkmate::assert_list(encodings)
super$append_metadata("Encodings", encodings, chr_converter)
super$append_text(yaml::as.yaml(encodings), "verbatim")
super$append_metadata("Encodings", encodings)
invisible(self)
}
),
Expand Down
109 changes: 41 additions & 68 deletions man/ReportCard.Rd

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

Loading

0 comments on commit 2ca8d9a

Please sign in to comment.