1 |
#' Get bootstrap current version |
|
2 |
#' @note will work properly mainly inside a tag `.renderHook` |
|
3 |
#' @keywords internal |
|
4 |
get_bs_version <- function() { |
|
5 | 15x |
theme <- bslib::bs_current_theme() |
6 | 15x |
if (bslib::is_bs_theme(theme)) { |
7 | ! |
bslib::theme_version(theme) |
8 |
} else { |
|
9 | 15x |
"3" |
10 |
} |
|
11 |
} |
|
12 | ||
13 |
#' Panel group widget |
|
14 |
#' |
|
15 |
#' `r lifecycle::badge("experimental")` |
|
16 |
#' |
|
17 |
#' @param title (`character`) title of panel |
|
18 |
#' @param ... content of panel |
|
19 |
#' @param collapsed (`logical`, optional) |
|
20 |
#' whether to initially collapse panel |
|
21 |
#' @param input_id (`character`, optional) |
|
22 |
#' name of the panel item element. If supplied, this will register a shiny input variable that |
|
23 |
#' indicates whether the panel item is open or collapsed and is accessed with `input$input_id`. |
|
24 |
#' |
|
25 |
#' @return `shiny.tag`. |
|
26 |
#' |
|
27 |
#' @keywords internal |
|
28 |
panel_item <- function(title, ..., collapsed = TRUE, input_id = NULL) { |
|
29 | 1x |
stopifnot(checkmate::test_character(title, len = 1) || inherits(title, c("shiny.tag", "shiny.tag.list", "html"))) |
30 | 1x |
checkmate::assert_flag(collapsed) |
31 | 1x |
checkmate::assert_string(input_id, null.ok = TRUE) |
32 | ||
33 | 1x |
div_id <- paste0(input_id, "_div") |
34 | 1x |
panel_id <- paste0(input_id, "_panel_body_", sample(1:10000, 1)) |
35 | ||
36 | ||
37 | 1x |
shiny::tags$div(.renderHook = function(res_tag) { |
38 | ! |
bs_version <- get_bs_version() |
39 | ||
40 |
# alter tag structure |
|
41 | ! |
if (bs_version == "3") { |
42 | ! |
res_tag$children <- list( |
43 | ! |
shiny::tags$div( |
44 | ! |
class = "panel panel-default", |
45 | ! |
shiny::tags$div( |
46 | ! |
id = div_id, |
47 | ! |
class = paste("panel-heading", ifelse(collapsed, "collapsed", "")), |
48 | ! |
`data-toggle` = "collapse", |
49 | ! |
href = paste0("#", panel_id), |
50 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
51 | ! |
shiny::icon("angle-down", class = "dropdown-icon"), |
52 | ! |
shiny::tags$label( |
53 | ! |
class = "panel-title inline", |
54 | ! |
title, |
55 |
) |
|
56 |
), |
|
57 | ! |
shiny::tags$div( |
58 | ! |
class = paste("panel-collapse collapse", ifelse(collapsed, "", "in")), |
59 | ! |
id = panel_id, |
60 | ! |
shiny::tags$div( |
61 | ! |
class = "panel-body", |
62 |
... |
|
63 |
) |
|
64 |
) |
|
65 |
) |
|
66 |
) |
|
67 | ! |
} else if (bs_version %in% c("4", "5")) { |
68 | ! |
res_tag$children <- list( |
69 | ! |
shiny::tags$div( |
70 | ! |
class = "card my-2", |
71 | ! |
shiny::tags$div( |
72 | ! |
class = "card-header", |
73 | ! |
shiny::tags$div( |
74 | ! |
class = ifelse(collapsed, "collapsed", ""), |
75 |
# bs4 |
|
76 | ! |
`data-toggle` = "collapse", |
77 |
# bs5 |
|
78 | ! |
`data-bs-toggle` = "collapse", |
79 | ! |
href = paste0("#", panel_id), |
80 | ! |
`aria-expanded` = ifelse(collapsed, "false", "true"), |
81 | ! |
shiny::icon("angle-down", class = "dropdown-icon"), |
82 | ! |
shiny::tags$label( |
83 | ! |
class = "card-title inline", |
84 | ! |
title, |
85 |
) |
|
86 |
) |
|
87 |
), |
|
88 | ! |
shiny::tags$div( |
89 | ! |
id = panel_id, |
90 | ! |
class = paste("collapse", ifelse(collapsed, "", "show")), |
91 | ! |
shiny::tags$div( |
92 | ! |
class = "card-body", |
93 |
... |
|
94 |
) |
|
95 |
) |
|
96 |
) |
|
97 |
) |
|
98 |
} else { |
|
99 | ! |
stop("Bootstrap 3, 4, and 5 are supported.") |
100 |
} |
|
101 | ||
102 | ! |
shiny::tagList( |
103 | ! |
shiny::singleton( |
104 | ! |
shiny::tags$head( |
105 | ! |
shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter")) |
106 |
) |
|
107 |
), |
|
108 | ! |
res_tag |
109 |
) |
|
110 |
}) |
|
111 |
} |
|
112 | ||
113 |
#' Convert content into a `flextable` |
|
114 |
#' |
|
115 |
#' Converts supported table formats into a `flextable` for enhanced formatting and presentation. |
|
116 |
#' |
|
117 |
#' Function merges cells with `colspan` > 1, |
|
118 |
#' aligns columns to the center and row names to the left, |
|
119 |
#' indents the row names by 10 times indentation. |
|
120 |
#' |
|
121 |
#' @param content Supported formats: `data.frame`, `rtables`, `TableTree`, `ElementaryTable`, `listing_df` |
|
122 |
#' |
|
123 |
#' @return `flextable`. |
|
124 |
#' |
|
125 |
#' @keywords internal |
|
126 |
to_flextable <- function(content) { |
|
127 | 16x |
if (inherits(content, c("rtables", "TableTree", "ElementaryTable"))) { |
128 | 3x |
ft <- rtables.officer::tt_to_flextable(content) |
129 | 13x |
} else if (inherits(content, "listing_df")) { |
130 | 1x |
mf <- rlistings::matrix_form(content) |
131 | 1x |
nr_header <- attr(mf, "nrow_header") |
132 | 1x |
df <- as.data.frame(mf$strings[seq(nr_header + 1, nrow(mf$strings)), , drop = FALSE]) |
133 | 1x |
header_df <- as.data.frame(mf$strings[seq_len(nr_header), , drop = FALSE]) |
134 | ||
135 | 1x |
ft <- rtables::df_to_tt(df) |
136 | 1x |
if (length(mf$main_title) != 0) { |
137 | ! |
rtables::main_title(ft) <- mf$main_title |
138 |
} |
|
139 | 1x |
rtables::subtitles(ft) <- mf$subtitles |
140 | 1x |
rtables::main_footer(ft) <- mf$main_footer |
141 | 1x |
rtables::prov_footer(ft) <- mf$prov_footer |
142 | 1x |
rtables::header_section_div(ft) <- mf$header_section_div |
143 | 1x |
ft <- rtables.officer::tt_to_flextable(ft, total_width = c(grDevices::pdf.options()$width - 1)) |
144 | 12x |
} else if (inherits(content, "data.frame")) { |
145 | 11x |
ft <- rtables.officer::tt_to_flextable( |
146 | 11x |
rtables::df_to_tt(content) |
147 |
) |
|
148 |
} else { |
|
149 | 1x |
stop(paste0("Unsupported class `(", format(class(content)), ")` when exporting table")) |
150 |
} |
|
151 | ||
152 | 15x |
ft |
153 |
} |
|
154 | ||
155 |
#' Get the merge index for a single span. |
|
156 |
#' This function retrieves the merge index for a single span, |
|
157 |
#' which is used in merging cells. |
|
158 |
#' @noRd |
|
159 |
#' @keywords internal |
|
160 |
get_merge_index_single <- function(span) { |
|
161 | ! |
ret <- list() |
162 | ! |
j <- 1 |
163 | ! |
while (j < length(span)) { |
164 | ! |
if (span[j] != 1) { |
165 | ! |
ret <- c(ret, list(seq(j, j + span[j] - 1))) |
166 |
} |
|
167 | ! |
j <- j + span[j] |
168 |
} |
|
169 | ! |
return(ret) |
170 |
} |
|
171 | ||
172 |
#' Divide text block into smaller blocks |
|
173 |
#' |
|
174 |
#' Split a text block into smaller blocks with a specified number of lines. |
|
175 |
#' |
|
176 |
#' A single character string containing a text block of multiple lines (separated by `\n`) |
|
177 |
#' is split into multiple strings with n or less lines each. |
|
178 |
#' |
|
179 |
#' @param x (`character`) string containing the input block of text |
|
180 |
#' @param n (`integer`) number of lines per block |
|
181 |
#' |
|
182 |
#' @return |
|
183 |
#' List of character strings with up to `n` lines in each element. |
|
184 |
#' |
|
185 |
#' @keywords internal |
|
186 |
split_text_block <- function(x, n) { |
|
187 | 2x |
checkmate::assert_string(x) |
188 | 2x |
checkmate::assert_integerish(n, lower = 1L, len = 1L) |
189 | ||
190 | 2x |
lines <- strsplit(x, "\n")[[1]] |
191 | ||
192 | 2x |
if (length(lines) <= n) { |
193 | 1x |
return(list(x)) |
194 |
} |
|
195 | ||
196 | 1x |
nblocks <- ceiling(length(lines) / n) |
197 | 1x |
ind <- rep(1:nblocks, each = n)[seq_along(lines)] |
198 | 1x |
unname(lapply(split(lines, ind), paste, collapse = "\n")) |
199 |
} |
|
200 | ||
201 |
#' Retrieve text details for global_knitr options |
|
202 |
#' This function returns a character string describing the default settings for the global_knitr options. |
|
203 |
#' @noRd |
|
204 |
#' @keywords internal |
|
205 |
global_knitr_details <- function() { |
|
206 | ! |
paste0( |
207 | ! |
c( |
208 | ! |
" To access the default values for the `global_knitr` parameter,", |
209 | ! |
" use `getOption('teal.reporter.global_knitr')`. These defaults include:", |
210 | ! |
" - `echo = TRUE`", |
211 | ! |
" - `tidy.opts = list(width.cutoff = 60)`", |
212 | ! |
" - `tidy = TRUE` if `formatR` package is installed, `FALSE` otherwise" |
213 |
), |
|
214 | ! |
collapse = "\n" |
215 |
) |
|
216 |
} |
1 |
#' @title `Renderer` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' A class for rendering reports from `ContentBlock` into various formats using `rmarkdown`. |
|
5 |
#' It supports `TextBlock`, `PictureBlock`, `RcodeBlock`, `NewpageBlock`, and `TableBlock`. |
|
6 |
#' |
|
7 |
#' @keywords internal |
|
8 |
Renderer <- R6::R6Class( # nolint: object_name_linter. |
|
9 |
classname = "Renderer", |
|
10 |
public = list( |
|
11 |
#' @description Initialize a `Renderer` object. |
|
12 |
#' |
|
13 |
#' @details Creates a new instance of `Renderer` |
|
14 |
#' with a temporary directory for storing report files. |
|
15 |
#' |
|
16 |
#' @return Object of class `Renderer`, invisibly. |
|
17 |
#' @examples |
|
18 |
#' Renderer <- getFromNamespace("Renderer", "teal.reporter") |
|
19 |
#' Renderer$new() |
|
20 |
#' |
|
21 |
initialize = function() { |
|
22 | 10x |
tmp_dir <- tempdir() |
23 | 10x |
output_dir <- file.path(tmp_dir, sprintf("report_%s", gsub("[.]", "", format(Sys.time(), "%Y%m%d%H%M%OS4")))) |
24 | 10x |
dir.create(path = output_dir) |
25 | 10x |
private$output_dir <- output_dir |
26 | 10x |
invisible(self) |
27 |
}, |
|
28 |
#' @description Finalizes a `Renderer` object. |
|
29 |
finalize = function() { |
|
30 | 10x |
unlink(private$output_dir, recursive = TRUE) |
31 |
}, |
|
32 |
#' @description Getting the `Rmd` text which could be easily rendered later. |
|
33 |
#' |
|
34 |
#' @param blocks (`list`) of `TextBlock`, `PictureBlock` and `NewpageBlock` objects. |
|
35 |
#' @param yaml_header (`character`) an `rmarkdown` `yaml` header. |
|
36 |
#' @param global_knitr (`list`) of `knitr` parameters (passed to `knitr::opts_chunk$set`) |
|
37 |
#' for customizing the rendering process. |
|
38 |
#' @details `r global_knitr_details()` |
|
39 |
#' |
|
40 |
#' @return Character vector constituting `rmarkdown` text (`yaml` header + body), ready to be rendered. |
|
41 |
#' @examplesIf require("ggplot2") |
|
42 |
#' library(yaml) |
|
43 |
#' library(rtables) |
|
44 |
#' library(ggplot2) |
|
45 |
#' |
|
46 |
#' ReportCard <- getFromNamespace("ReportCard", "teal.reporter") |
|
47 |
#' card1 <- ReportCard$new() |
|
48 |
#' |
|
49 |
#' card1$append_text("Header 2 text", "header2") |
|
50 |
#' card1$append_text("A paragraph of default text") |
|
51 |
#' card1$append_plot( |
|
52 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
53 |
#' ) |
|
54 |
#' |
|
55 |
#' ReportCard <- getFromNamespace("ReportCard", "teal.reporter") |
|
56 |
#' card2 <- ReportCard$new() |
|
57 |
#' |
|
58 |
#' card2$append_text("Header 2 text", "header2") |
|
59 |
#' card2$append_text("A paragraph of default text", "header2") |
|
60 |
#' lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean) |
|
61 |
#' table_res2 <- build_table(lyt, airquality) |
|
62 |
#' card2$append_table(table_res2) |
|
63 |
#' card2$append_table(iris) |
|
64 |
#' card2$append_rcode("2+2", echo = FALSE) |
|
65 |
#' |
|
66 |
#' Reporter <- getFromNamespace("Reporter", "teal.reporter") |
|
67 |
#' reporter <- Reporter$new() |
|
68 |
#' reporter$append_cards(list(card1, card2)) |
|
69 |
#' |
|
70 |
#' yaml_quoted <- getFromNamespace("yaml_quoted", "teal.reporter") |
|
71 |
#' yaml_l <- list( |
|
72 |
#' author = yaml_quoted("NEST"), |
|
73 |
#' title = yaml_quoted("Report"), |
|
74 |
#' date = yaml_quoted("07/04/2019"), |
|
75 |
#' output = list(html_document = list(toc = FALSE)) |
|
76 |
#' ) |
|
77 |
#' |
|
78 |
#' md_header <- getFromNamespace("md_header", "teal.reporter") |
|
79 |
#' yaml_header <- md_header(as.yaml(yaml_l)) |
|
80 |
#' Renderer <- getFromNamespace("Renderer", "teal.reporter") |
|
81 |
#' result_path <- Renderer$new()$renderRmd(reporter$get_blocks(), yaml_header) |
|
82 |
#' |
|
83 |
renderRmd = function(blocks, yaml_header, global_knitr = getOption("teal.reporter.global_knitr")) { |
|
84 | 8x |
checkmate::assert_list(blocks, c("TextBlock", "PictureBlock", "NewpageBlock", "TableBlock", "RcodeBlock")) |
85 | 7x |
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get())) |
86 | ||
87 | 7x |
if (missing(yaml_header)) { |
88 | 2x |
yaml_header <- md_header(yaml::as.yaml(list(title = "Report"))) |
89 |
} |
|
90 | ||
91 | 7x |
private$report_type <- get_yaml_field(yaml_header, "output") |
92 | ||
93 | 7x |
parsed_global_knitr <- sprintf( |
94 | 7x |
"\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(%s)\n%s\n```\n", |
95 | 7x |
capture.output(dput(global_knitr)), |
96 | 7x |
if (identical(private$report_type, "powerpoint_presentation")) { |
97 | ! |
format_code_block_function <- quote( |
98 | ! |
code_block <- function(code_text) { |
99 | ! |
df <- data.frame(code_text) |
100 | ! |
ft <- flextable::flextable(df) |
101 | ! |
ft <- flextable::delete_part(ft, part = "header") |
102 | ! |
ft <- flextable::autofit(ft, add_h = 0) |
103 | ! |
ft <- flextable::fontsize(ft, size = 7, part = "body") |
104 | ! |
ft <- flextable::bg(x = ft, bg = "lightgrey") |
105 | ! |
ft <- flextable::border_outer(ft) |
106 | ! |
if (flextable::flextable_dim(ft)$widths > 8) { |
107 | ! |
ft <- flextable::width(ft, width = 8) |
108 |
} |
|
109 | ! |
ft |
110 |
} |
|
111 |
) |
|
112 | ! |
paste(deparse(format_code_block_function), collapse = "\n") |
113 |
} else { |
|
114 |
"" |
|
115 |
} |
|
116 |
) |
|
117 | ||
118 | 7x |
parsed_blocks <- paste( |
119 | 7x |
unlist( |
120 | 7x |
lapply(blocks, function(b) private$block2md(b)) |
121 |
), |
|
122 | 7x |
collapse = "\n\n" |
123 |
) |
|
124 | ||
125 | 7x |
rmd_text <- paste0(yaml_header, "\n", parsed_global_knitr, "\n", parsed_blocks, "\n") |
126 | 7x |
tmp <- tempfile(fileext = ".Rmd") |
127 | 7x |
input_path <- file.path( |
128 | 7x |
private$output_dir, |
129 | 7x |
sprintf("input_%s.Rmd", gsub("[.]", "", format(Sys.time(), "%Y%m%d%H%M%OS3"))) |
130 |
) |
|
131 | 7x |
cat(rmd_text, file = input_path) |
132 | 7x |
input_path |
133 |
}, |
|
134 |
#' @description Renders the `Report` to the desired output format by compiling the `rmarkdown` file. |
|
135 |
#' |
|
136 |
#' @param blocks (`list`) of `TextBlock`, `PictureBlock` or `NewpageBlock` objects. |
|
137 |
#' @param yaml_header (`character`) an `rmarkdown` `yaml` header. |
|
138 |
#' @param global_knitr (`list`) of `knitr` parameters (passed to `knitr::opts_chunk$set`) |
|
139 |
#' for customizing the rendering process. |
|
140 |
#' @param ... `rmarkdown::render` arguments, `input` and `output_dir` should not be updated. |
|
141 |
#' @details `r global_knitr_details()` |
|
142 |
#' |
|
143 |
#' @return `character` path to the output. |
|
144 |
#' @examplesIf require("ggplot2") |
|
145 |
#' library(yaml) |
|
146 |
#' library(ggplot2) |
|
147 |
#' |
|
148 |
#' ReportCard <- getFromNamespace("ReportCard", "teal.reporter") |
|
149 |
#' card1 <- ReportCard$new() |
|
150 |
#' |
|
151 |
#' card1$append_text("Header 2 text", "header2") |
|
152 |
#' card1$append_text("A paragraph of default text") |
|
153 |
#' card1$append_plot( |
|
154 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
155 |
#' ) |
|
156 |
#' |
|
157 |
#' ReportCard <- getFromNamespace("ReportCard", "teal.reporter") |
|
158 |
#' card2 <- ReportCard$new() |
|
159 |
#' |
|
160 |
#' card2$append_text("Header 2 text", "header2") |
|
161 |
#' card2$append_text("A paragraph of default text", "header2") |
|
162 |
#' lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean) |
|
163 |
#' table_res2 <- build_table(lyt, airquality) |
|
164 |
#' card2$append_table(table_res2) |
|
165 |
#' card2$append_table(iris) |
|
166 |
#' card2$append_rcode("2+2", echo = FALSE) |
|
167 |
#' Reporter <- getFromNamespace("Reporter", "teal.reporter")$new() |
|
168 |
#' Reporter$append_cards(list(card1, card2)) |
|
169 |
#' |
|
170 |
#' yaml_quoted <- getFromNamespace("yaml_quoted", "teal.reporter") |
|
171 |
#' yaml_l <- list( |
|
172 |
#' author = yaml_quoted("NEST"), |
|
173 |
#' title = yaml_quoted("Report"), |
|
174 |
#' date = yaml_quoted("07/04/2019"), |
|
175 |
#' output = list(html_document = list(toc = FALSE)) |
|
176 |
#' ) |
|
177 |
#' |
|
178 |
#' md_header <- getFromNamespace("md_header", "teal.reporter") |
|
179 |
#' yaml_header <- md_header(as.yaml(yaml_l)) |
|
180 |
#' Renderer <- getFromNamespace("Renderer", "teal.reporter") |
|
181 |
#' result_path <- Renderer$new()$render(Reporter$get_blocks(), yaml_header) |
|
182 |
#' |
|
183 |
render = function(blocks, yaml_header, global_knitr = getOption("teal.reporter.global_knitr"), ...) { |
|
184 | 6x |
args <- list(...) |
185 | 6x |
input_path <- self$renderRmd(blocks, yaml_header, global_knitr) |
186 | 6x |
args <- append(args, list( |
187 | 6x |
input = input_path, |
188 | 6x |
output_dir = private$output_dir, |
189 | 6x |
output_format = "all", |
190 | 6x |
quiet = TRUE |
191 |
)) |
|
192 | 6x |
args_nams <- unique(names(args)) |
193 | 6x |
args <- lapply(args_nams, function(x) args[[x]]) |
194 | 6x |
names(args) <- args_nams |
195 | 6x |
do.call(rmarkdown::render, args) |
196 |
}, |
|
197 |
#' @description Get `output_dir` field. |
|
198 |
#' |
|
199 |
#' @return `character` a `output_dir` field path. |
|
200 |
#' @examples |
|
201 |
#' Renderer <- getFromNamespace("Renderer", "teal.reporter")$new() |
|
202 |
#' Renderer$get_output_dir() |
|
203 |
#' |
|
204 |
get_output_dir = function() { |
|
205 | 7x |
private$output_dir |
206 |
} |
|
207 |
), |
|
208 |
private = list( |
|
209 |
output_dir = character(0), |
|
210 |
report_type = NULL, |
|
211 |
# factory method |
|
212 |
block2md = function(block) { |
|
213 | 25x |
if (inherits(block, "TextBlock")) { |
214 | 14x |
private$textBlock2md(block) |
215 | 11x |
} else if (inherits(block, "RcodeBlock")) { |
216 | ! |
private$rcodeBlock2md(block) |
217 | 11x |
} else if (inherits(block, "PictureBlock")) { |
218 | 7x |
private$pictureBlock2md(block) |
219 | 4x |
} else if (inherits(block, "TableBlock")) { |
220 | 2x |
private$tableBlock2md(block) |
221 | 2x |
} else if (inherits(block, "NewpageBlock")) { |
222 | 2x |
block$get_content() |
223 |
} else { |
|
224 | ! |
stop("Unknown block class") |
225 |
} |
|
226 |
}, |
|
227 |
# card specific methods |
|
228 |
textBlock2md = function(block) { |
|
229 | 14x |
text_style <- block$get_style() |
230 | 14x |
block_content <- block$get_content() |
231 | 14x |
switch(text_style, |
232 | 2x |
"default" = block_content, |
233 | ! |
"verbatim" = sprintf("\n```\n%s\n```\n", block_content), |
234 | 12x |
"header2" = paste0("## ", block_content), |
235 | ! |
"header3" = paste0("### ", block_content), |
236 | ! |
block_content |
237 |
) |
|
238 |
}, |
|
239 |
rcodeBlock2md = function(block) { |
|
240 | ! |
params <- block$get_params() |
241 | ! |
params <- lapply(params, function(l) if (is.character(l)) shQuote(l) else l) |
242 | ! |
if (identical(private$report_type, "powerpoint_presentation")) { |
243 | ! |
block_content_list <- split_text_block(block$get_content(), 30) |
244 | ! |
paste( |
245 | ! |
sprintf( |
246 | ! |
"\\newpage\n\n---\n\n```{r, echo=FALSE}\ncode_block(\n%s)\n```\n", |
247 | ! |
shQuote(block_content_list, type = "cmd") |
248 |
), |
|
249 | ! |
collapse = "\n\n" |
250 |
) |
|
251 |
} else { |
|
252 | ! |
sprintf( |
253 | ! |
"\\newpage\n\n--- \n\n```{r, %s}\n%s\n```\n", |
254 | ! |
paste(names(params), params, sep = "=", collapse = ", "), |
255 | ! |
block$get_content() |
256 |
) |
|
257 |
} |
|
258 |
}, |
|
259 |
pictureBlock2md = function(block) { |
|
260 | 7x |
basename_pic <- basename(block$get_content()) |
261 | 7x |
file.copy(block$get_content(), file.path(private$output_dir, basename_pic)) |
262 | 7x |
params <- c( |
263 | 7x |
`out.width` = "'100%'", |
264 | 7x |
`out.height` = "'100%'" |
265 |
) |
|
266 | 7x |
title <- block$get_title() |
267 | 7x |
if (length(title)) params["fig.cap"] <- shQuote(title) |
268 | 7x |
sprintf( |
269 | 7x |
"\n```{r, echo = FALSE, %s}\nknitr::include_graphics(path = '%s')\n```\n", |
270 | 7x |
paste(names(params), params, sep = "=", collapse = ", "), |
271 | 7x |
basename_pic |
272 |
) |
|
273 |
}, |
|
274 |
tableBlock2md = function(block) { |
|
275 | 2x |
basename_table <- basename(block$get_content()) |
276 | 2x |
file.copy(block$get_content(), file.path(private$output_dir, basename_table)) |
277 | 2x |
sprintf("```{r echo = FALSE}\nreadRDS('%s')\n```", basename_table) |
278 |
} |
|
279 |
), |
|
280 |
lock_objects = TRUE, |
|
281 |
lock_class = TRUE |
|
282 |
) |
1 |
#' @title `PictureBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' Specialized `FileBlock` for managing picture content in reports. |
|
5 |
#' It's designed to handle plots from packages such as `ggplot2`, `grid`, or `lattice`. |
|
6 |
#' It can save plots to files, set titles and specify dimensions. |
|
7 |
#' |
|
8 |
#' @keywords internal |
|
9 |
PictureBlock <- R6::R6Class( # nolint: object_name_linter. |
|
10 |
classname = "PictureBlock", |
|
11 |
inherit = FileBlock, |
|
12 |
public = list( |
|
13 |
#' @description Initialize a `PictureBlock` object. |
|
14 |
#' |
|
15 |
#' @param plot (`ggplot` or `grid`) a picture in this `PictureBlock` |
|
16 |
#' |
|
17 |
#' @return Object of class `PictureBlock`, invisibly. |
|
18 |
initialize = function(plot) { |
|
19 | 51x |
if (!missing(plot)) { |
20 | ! |
self$set_content(plot) |
21 |
} |
|
22 | 51x |
invisible(self) |
23 |
}, |
|
24 |
#' @description Sets the content of this `PictureBlock`. |
|
25 |
#' |
|
26 |
#' @details Raises error if argument is not a `ggplot`, `grob` or `trellis` plot. |
|
27 |
#' |
|
28 |
#' @param content (`ggplot` or `grob` or `trellis`) a picture in this `PictureBlock` |
|
29 |
#' |
|
30 |
#' @return `self`, invisibly. |
|
31 |
#' @examplesIf require("ggplot2") && require("lattice") |
|
32 |
#' library(ggplot2) |
|
33 |
#' library(lattice) |
|
34 |
#' |
|
35 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
36 |
#' block <- PictureBlock$new() |
|
37 |
#' block$set_content(ggplot(iris)) |
|
38 |
#' |
|
39 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
40 |
#' block <- PictureBlock$new() |
|
41 |
#' block$set_content(bwplot(1)) |
|
42 |
#' |
|
43 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
44 |
#' block <- PictureBlock$new() |
|
45 |
#' block$set_content(ggplotGrob(ggplot(iris))) |
|
46 |
set_content = function(content) { |
|
47 | 31x |
checkmate::assert_multi_class(content, private$supported_plots) |
48 | 29x |
path <- tempfile(fileext = ".png") |
49 | 29x |
grDevices::png(filename = path, width = private$dim[1], height = private$dim[2]) |
50 | 29x |
tryCatch( |
51 | 29x |
expr = { |
52 | 29x |
if (inherits(content, "grob")) { |
53 | 1x |
grid::grid.newpage() |
54 | 1x |
grid::grid.draw(content) |
55 | 28x |
} else if (inherits(content, c("gg", "Heatmap"))) { # "Heatmap" S4 from ComplexHeatmap |
56 | 27x |
print(content) |
57 | 1x |
} else if (inherits(content, "trellis")) { |
58 | 1x |
grid::grid.newpage() |
59 | 1x |
grid::grid.draw(grid::grid.grabExpr(print(content), warn = 0, wrap.grobs = TRUE)) |
60 |
} |
|
61 | 29x |
super$set_content(path) |
62 |
}, |
|
63 | 29x |
finally = grDevices::dev.off() |
64 |
) |
|
65 | 29x |
invisible(self) |
66 |
}, |
|
67 |
#' @description Sets the title of this `PictureBlock`. |
|
68 |
#' |
|
69 |
#' @details Raises error if argument is not `character(1)`. |
|
70 |
#' |
|
71 |
#' @param title (`character(1)`) a string assigned to this `PictureBlock` |
|
72 |
#' |
|
73 |
#' @return `self`, invisibly. |
|
74 |
#' @examples |
|
75 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
76 |
#' block <- PictureBlock$new() |
|
77 |
#' block$set_title("Title") |
|
78 |
#' |
|
79 |
set_title = function(title) { |
|
80 | 5x |
checkmate::assert_string(title) |
81 | 4x |
private$title <- title |
82 | 4x |
invisible(self) |
83 |
}, |
|
84 |
#' @description Get the title of this `PictureBlock`. |
|
85 |
#' |
|
86 |
#' @return The content of this `PictureBlock`. |
|
87 |
#' @examples |
|
88 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
89 |
#' block <- PictureBlock$new() |
|
90 |
#' block$get_title() |
|
91 |
#' |
|
92 |
get_title = function() { |
|
93 | 9x |
private$title |
94 |
}, |
|
95 |
#' @description Sets the dimensions of this `PictureBlock`. |
|
96 |
#' |
|
97 |
#' @param dim (`numeric(2)`) figure dimensions (width and height) in pixels. |
|
98 |
#' |
|
99 |
#' @return `self`, invisibly. |
|
100 |
#' @examples |
|
101 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
102 |
#' block <- PictureBlock$new() |
|
103 |
#' block$set_dim(c(800, 600)) |
|
104 |
#' |
|
105 |
set_dim = function(dim) { |
|
106 | 6x |
checkmate::assert_numeric(dim, len = 2) |
107 | 4x |
private$dim <- dim |
108 | 4x |
invisible(self) |
109 |
}, |
|
110 |
#' @description Get `PictureBlock` dimensions as a numeric vector. |
|
111 |
#' |
|
112 |
#' @return `numeric` the array of 2 numeric values representing width and height in pixels. |
|
113 |
#' @examples |
|
114 |
#' PictureBlock <- getFromNamespace("PictureBlock", "teal.reporter") |
|
115 |
#' block <- PictureBlock$new() |
|
116 |
#' block$get_dim() |
|
117 |
get_dim = function() { |
|
118 | ! |
private$dim |
119 |
} |
|
120 |
), |
|
121 |
private = list( |
|
122 |
supported_plots = c("ggplot", "grob", "trellis", "Heatmap"), |
|
123 |
type = character(0), |
|
124 |
title = "", |
|
125 |
dim = c(800, 600) |
|
126 |
), |
|
127 |
lock_objects = TRUE, |
|
128 |
lock_class = TRUE |
|
129 |
) |
1 |
#' @title `ReportCard`: An `R6` class for building report elements |
|
2 |
#' @docType class |
|
3 |
#' |
|
4 |
#' @description `r lifecycle::badge("experimental")` |
|
5 |
#' |
|
6 |
#' This `R6` class that supports creating a report card containing text, plot, table and |
|
7 |
#' metadata blocks that can be appended and rendered to form a report output from a `shiny` app. |
|
8 |
#' |
|
9 |
#' For more information about the various blocks, refer to the vignette: |
|
10 |
#' `vignette("teal-reporter-blocks-overview", "teal.reporter")`. |
|
11 |
#' |
|
12 |
#' @export |
|
13 |
#' |
|
14 |
ReportCard <- R6::R6Class( # nolint: object_name_linter. |
|
15 |
classname = "ReportCard", |
|
16 |
public = list( |
|
17 |
#' @description Initialize a `ReportCard` object. |
|
18 |
#' |
|
19 |
#' @return Object of class `ReportCard`, invisibly. |
|
20 |
#' @examples |
|
21 |
#' card <- ReportCard$new() |
|
22 |
#' |
|
23 |
initialize = function() { |
|
24 | 65x |
private$content <- list() |
25 | 65x |
private$metadata <- list() |
26 | 65x |
invisible(self) |
27 |
}, |
|
28 |
#' @description Appends a table to this `ReportCard`. |
|
29 |
#' |
|
30 |
#' @param table A (`data.frame` or `rtables` or `TableTree` or `ElementaryTable` or `listing_df`) |
|
31 |
#' that can be coerced into a table. |
|
32 |
#' @return `self`, invisibly. |
|
33 |
#' @examples |
|
34 |
#' card <- ReportCard$new()$append_table(iris) |
|
35 |
#' |
|
36 |
append_table = function(table) { |
|
37 | 4x |
self$append_content(TableBlock$new(table)) |
38 | 4x |
invisible(self) |
39 |
}, |
|
40 |
#' @description Appends a plot to this `ReportCard`. |
|
41 |
#' |
|
42 |
#' @param plot (`ggplot` or `grob` or `trellis`) plot object. |
|
43 |
#' @param dim (`numeric(2)`) width and height in pixels. |
|
44 |
#' @return `self`, invisibly. |
|
45 |
#' @examplesIf require("ggplot2") |
|
46 |
#' library(ggplot2) |
|
47 |
#' |
|
48 |
#' card <- ReportCard$new()$append_plot( |
|
49 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
50 |
#' ) |
|
51 |
#' |
|
52 |
append_plot = function(plot, dim = NULL) { |
|
53 | 19x |
pb <- PictureBlock$new() |
54 | 19x |
if (!is.null(dim) && length(dim) == 2) { |
55 | 1x |
pb$set_dim(dim) |
56 |
} |
|
57 | 19x |
pb$set_content(plot) |
58 | 19x |
self$append_content(pb) |
59 | 19x |
invisible(self) |
60 |
}, |
|
61 |
#' @description Appends a text paragraph to this `ReportCard`. |
|
62 |
#' |
|
63 |
#' @param text (`character`) The text content to add. |
|
64 |
#' @param style (`character(1)`) the style of the paragraph. One of: `default`, `header`, `verbatim` |
|
65 |
#' @return `self`, invisibly. |
|
66 |
#' @examples |
|
67 |
#' card <- ReportCard$new()$append_text("A paragraph of default text") |
|
68 |
#' |
|
69 |
append_text = function(text, style = TextBlock$new()$get_available_styles()[1]) { |
|
70 | 50x |
self$append_content(TextBlock$new(text, style)) |
71 | 50x |
invisible(self) |
72 |
}, |
|
73 |
#' @description Appends an `R` code chunk to `ReportCard`. |
|
74 |
#' |
|
75 |
#' @param text (`character`) The `R` code to include. |
|
76 |
#' @param ... Additional `rmarkdown` parameters for formatting the `R` code chunk. |
|
77 |
#' @return `self`, invisibly. |
|
78 |
#' @examples |
|
79 |
#' card <- ReportCard$new()$append_rcode("2+2", echo = FALSE) |
|
80 |
#' |
|
81 |
append_rcode = function(text, ...) { |
|
82 | 4x |
self$append_content(RcodeBlock$new(text, ...)) |
83 | 4x |
invisible(self) |
84 |
}, |
|
85 |
#' @description Appends a generic `ContentBlock` to this `ReportCard`. |
|
86 |
#' |
|
87 |
#' @param content (`ContentBlock`) object. |
|
88 |
#' @return `self`, invisibly. |
|
89 |
#' @examples |
|
90 |
#' NewpageBlock <- getFromNamespace("NewpageBlock", "teal.reporter") |
|
91 |
#' card <- ReportCard$new()$append_content(NewpageBlock$new()) |
|
92 |
#' |
|
93 |
append_content = function(content) { |
|
94 | 98x |
checkmate::assert_class(content, "ContentBlock") |
95 | 98x |
private$content <- append(private$content, content) |
96 | 98x |
invisible(self) |
97 |
}, |
|
98 |
#' @description Get all content blocks from this `ReportCard`. |
|
99 |
#' |
|
100 |
#' @return `list()` list of `TableBlock`, `TextBlock` and `PictureBlock`. |
|
101 |
#' @examples |
|
102 |
#' card <- ReportCard$new()$append_text("Some text")$append_metadata("rc", "a <- 2 + 2") |
|
103 |
#' |
|
104 |
#' card$get_content() |
|
105 |
#' |
|
106 |
#' |
|
107 |
get_content = function() { |
|
108 | 87x |
private$content |
109 |
}, |
|
110 |
#' @description Clears all content and metadata from `ReportCard`. |
|
111 |
#' |
|
112 |
#' @return `self`, invisibly. |
|
113 |
#' |
|
114 |
reset = function() { |
|
115 | 6x |
private$content <- list() |
116 | 6x |
private$metadata <- list() |
117 | 6x |
invisible(self) |
118 |
}, |
|
119 |
#' @description Get the metadata associated with `ReportCard`. |
|
120 |
#' |
|
121 |
#' @return `named list` list of elements. |
|
122 |
#' @examples |
|
123 |
#' card <- ReportCard$new()$append_text("Some text")$append_metadata("rc", "a <- 2 + 2") |
|
124 |
#' |
|
125 |
#' card$get_metadata() |
|
126 |
#' |
|
127 |
get_metadata = function() { |
|
128 | 15x |
private$metadata |
129 |
}, |
|
130 |
#' @description Appends metadata to this `ReportCard`. |
|
131 |
#' |
|
132 |
#' @param key (`character(1)`) string specifying the metadata key. |
|
133 |
#' @param value value associated with the metadata key. |
|
134 |
#' @return `self`, invisibly. |
|
135 |
#' @examplesIf require("ggplot2") |
|
136 |
#' library(ggplot2) |
|
137 |
#' |
|
138 |
#' card <- ReportCard$new()$append_text("Some text")$append_plot( |
|
139 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
140 |
#' )$append_text("Some text")$append_metadata(key = "lm", |
|
141 |
#' value = lm(Ozone ~ Solar.R, airquality)) |
|
142 |
#' card$get_content() |
|
143 |
#' card$get_metadata() |
|
144 |
#' |
|
145 |
append_metadata = function(key, value) { |
|
146 | 16x |
checkmate::assert_character(key, min.len = 0, max.len = 1) |
147 | 13x |
checkmate::assert_false(key %in% names(private$metadata)) |
148 | 12x |
meta_list <- list() |
149 | 12x |
meta_list[[key]] <- value |
150 | 11x |
private$metadata <- append(private$metadata, meta_list) |
151 | 11x |
invisible(self) |
152 |
}, |
|
153 |
#' @description Get the name of the `ReportCard`. |
|
154 |
#' |
|
155 |
#' @return `character` a card name. |
|
156 |
#' @examples |
|
157 |
#' ReportCard$new()$set_name("NAME")$get_name() |
|
158 |
get_name = function() { |
|
159 | 27x |
private$name |
160 |
}, |
|
161 |
#' @description Set the name of the `ReportCard`. |
|
162 |
#' |
|
163 |
#' @param name (`character(1)`) a card name. |
|
164 |
#' @return `self`, invisibly. |
|
165 |
#' @examples |
|
166 |
#' ReportCard$new()$set_name("NAME")$get_name() |
|
167 |
set_name = function(name) { |
|
168 | 7x |
checkmate::assert_character(name) |
169 | 7x |
private$name <- name |
170 | 7x |
invisible(self) |
171 |
}, |
|
172 |
#' @description Convert the `ReportCard` to a list, including content and metadata. |
|
173 |
#' @param output_dir (`character`) with a path to the directory where files will be copied. |
|
174 |
#' @return (`named list`) a `ReportCard` representation. |
|
175 |
#' @examplesIf require("ggplot2") |
|
176 |
#' library(ggplot2) |
|
177 |
#' |
|
178 |
#' card <- ReportCard$new()$append_text("Some text")$append_plot( |
|
179 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
180 |
#' )$append_text("Some text")$append_metadata(key = "lm", |
|
181 |
#' value = lm(Ozone ~ Solar.R, airquality)) |
|
182 |
#' card$get_content() |
|
183 |
#' |
|
184 |
#' card$to_list(tempdir()) |
|
185 |
#' |
|
186 |
to_list = function(output_dir) { |
|
187 | 11x |
new_blocks <- list() |
188 | 11x |
for (block in self$get_content()) { |
189 | 36x |
block_class <- class(block)[1] |
190 | 36x |
formal_args <- formalArgs(block$to_list) |
191 | 36x |
cblock <- if ("output_dir" %in% formal_args) { |
192 | 13x |
block$to_list(output_dir) |
193 |
} else { |
|
194 | 23x |
block$to_list() |
195 |
} |
|
196 | 36x |
new_block <- list() |
197 | 36x |
new_block[[block_class]] <- cblock |
198 | 36x |
new_blocks <- c(new_blocks, new_block) |
199 |
} |
|
200 | 11x |
new_card <- list() |
201 | 11x |
new_card[["blocks"]] <- new_blocks |
202 | 11x |
new_card[["metadata"]] <- self$get_metadata() |
203 | 11x |
new_card[["name"]] <- self$get_name() |
204 | 11x |
new_card |
205 |
}, |
|
206 |
#' @description Reconstructs the `ReportCard` from a list representation. |
|
207 |
#' @param card (`named list`) a `ReportCard` representation. |
|
208 |
#' @param output_dir (`character`) with a path to the directory where a file will be copied. |
|
209 |
#' @return `self`, invisibly. |
|
210 |
#' @examplesIf require("ggplot2") |
|
211 |
#' library(ggplot2) |
|
212 |
#' |
|
213 |
#' card <- ReportCard$new()$append_text("Some text")$append_plot( |
|
214 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
215 |
#' )$append_text("Some text")$append_metadata(key = "lm", |
|
216 |
#' value = lm(Ozone ~ Solar.R, airquality)) |
|
217 |
#' card$get_content() |
|
218 |
#' |
|
219 |
#' ReportCard$new()$from_list(card$to_list(tempdir()), tempdir()) |
|
220 |
#' |
|
221 |
from_list = function(card, output_dir) { |
|
222 | 6x |
self$reset() |
223 | 6x |
blocks <- card$blocks |
224 | 6x |
metadata <- card$metadata |
225 | 6x |
name <- card$name |
226 | 6x |
if (length(name) == 0) name <- character(0) |
227 | 6x |
blocks_names <- names(blocks) |
228 | 6x |
blocks_names <- gsub("[.][0-9]*$", "", blocks_names) |
229 | 6x |
for (iter_b in seq_along(blocks)) { |
230 | 21x |
block_class <- blocks_names[iter_b] |
231 | 21x |
block <- blocks[[iter_b]] |
232 | 21x |
instance <- private$dispatch_block(block_class) |
233 | 21x |
formal_args <- formalArgs(instance$new()$from_list) |
234 | 21x |
cblock <- if (all(c("x", "output_dir") %in% formal_args)) { |
235 | 8x |
instance$new()$from_list(block, output_dir) |
236 | 21x |
} else if ("x" %in% formal_args) { |
237 | 13x |
instance$new()$from_list(block) |
238 |
} else { |
|
239 | ! |
instance$new()$from_list() |
240 |
} |
|
241 | 21x |
self$append_content(cblock) |
242 |
} |
|
243 | 6x |
for (meta in names(metadata)) { |
244 | ! |
self$append_metadata(meta, metadata[[meta]]) |
245 |
} |
|
246 | 6x |
self$set_name(name) |
247 | 6x |
invisible(self) |
248 |
} |
|
249 |
), |
|
250 |
private = list( |
|
251 |
content = list(), |
|
252 |
metadata = list(), |
|
253 |
name = character(0), |
|
254 |
dispatch_block = function(block_class) { |
|
255 | 21x |
eval(str2lang(block_class)) |
256 |
}, |
|
257 |
# @description The copy constructor. |
|
258 |
# |
|
259 |
# @param name the name of the field |
|
260 |
# @param value the value of the field |
|
261 |
# @return the new value of the field |
|
262 |
# |
|
263 |
deep_clone = function(name, value) { |
|
264 | 60x |
if (name == "content") { |
265 | 3x |
lapply(value, function(content_block) { |
266 | 5x |
if (inherits(content_block, "R6")) { |
267 | 5x |
content_block$clone(deep = TRUE) |
268 |
} else { |
|
269 | ! |
content_block |
270 |
} |
|
271 |
}) |
|
272 |
} else { |
|
273 | 57x |
value |
274 |
} |
|
275 |
} |
|
276 |
), |
|
277 |
lock_objects = TRUE, |
|
278 |
lock_class = TRUE |
|
279 |
) |
1 |
#' Report previewer module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' Module offers functionalities to visualize, manipulate, |
|
6 |
#' and interact with report cards that have been added to a report. |
|
7 |
#' It includes a previewer interface to see the cards and options to modify the report before downloading. |
|
8 |
#' |
|
9 |
#' Cards are saved by the `shiny` bookmarking mechanism. |
|
10 |
#' |
|
11 |
#' For more details see the vignette: `vignette("previewerReporter", "teal.reporter")`. |
|
12 |
#' |
|
13 |
#' @details `r global_knitr_details()` |
|
14 |
#' |
|
15 |
#' @name reporter_previewer |
|
16 |
#' |
|
17 |
#' @param id (`character(1)`) `shiny` module instance id. |
|
18 |
#' @param reporter (`Reporter`) instance. |
|
19 |
#' @param global_knitr (`list`) of `knitr` parameters (passed to `knitr::opts_chunk$set`) |
|
20 |
#' for customizing the rendering process. |
|
21 |
#' @param previewer_buttons (`character`) set of modules to include with `c("download", "load", "reset")` possible |
|
22 |
#' values and `"download"` is required. |
|
23 |
#' Default `c("download", "load", "reset")` |
|
24 |
#' @inheritParams reporter_download_inputs |
|
25 |
#' |
|
26 |
#' @return `NULL`. |
|
27 |
NULL |
|
28 | ||
29 |
#' @rdname reporter_previewer |
|
30 |
#' @export |
|
31 |
reporter_previewer_ui <- function(id) { |
|
32 | 1x |
ns <- shiny::NS(id) |
33 | ||
34 | 1x |
shiny::fluidRow( |
35 | 1x |
add_previewer_js(ns), |
36 | 1x |
add_previewer_css(), |
37 | 1x |
shiny::tagList( |
38 | 1x |
shiny::tags$div( |
39 | 1x |
class = "col-md-3", |
40 | 1x |
shiny::tags$div(class = "well", shiny::uiOutput(ns("encoding"))) |
41 |
), |
|
42 | 1x |
shiny::tags$div( |
43 | 1x |
class = "col-md-9", |
44 | 1x |
shiny::tags$div( |
45 | 1x |
id = "reporter_previewer", |
46 | 1x |
shiny::uiOutput(ns("pcards")) |
47 |
) |
|
48 |
) |
|
49 |
) |
|
50 |
) |
|
51 |
} |
|
52 | ||
53 |
#' @rdname reporter_previewer |
|
54 |
#' @export |
|
55 |
reporter_previewer_srv <- function(id, |
|
56 |
reporter, |
|
57 |
global_knitr = getOption("teal.reporter.global_knitr"), |
|
58 |
rmd_output = c( |
|
59 |
"html" = "html_document", "pdf" = "pdf_document", |
|
60 |
"powerpoint" = "powerpoint_presentation", |
|
61 |
"word" = "word_document" |
|
62 |
), |
|
63 |
rmd_yaml_args = list( |
|
64 |
author = "NEST", title = "Report", |
|
65 |
date = as.character(Sys.Date()), output = "html_document", |
|
66 |
toc = FALSE |
|
67 |
), |
|
68 |
previewer_buttons = c("download", "load", "reset")) { |
|
69 | 12x |
checkmate::assert_subset(previewer_buttons, c("download", "load", "reset"), empty.ok = FALSE) |
70 | 12x |
checkmate::assert_true("download" %in% previewer_buttons) |
71 | 12x |
checkmate::assert_class(reporter, "Reporter") |
72 | 12x |
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get())) |
73 | 12x |
checkmate::assert_subset( |
74 | 12x |
rmd_output, |
75 | 12x |
c( |
76 | 12x |
"html_document", "pdf_document", |
77 | 12x |
"powerpoint_presentation", "word_document" |
78 |
), |
|
79 | 12x |
empty.ok = FALSE |
80 |
) |
|
81 | 12x |
checkmate::assert_list(rmd_yaml_args, names = "named") |
82 | 12x |
checkmate::assert_names( |
83 | 12x |
names(rmd_yaml_args), |
84 | 12x |
subset.of = c("author", "title", "date", "output", "toc"), |
85 | 12x |
must.include = "output" |
86 |
) |
|
87 | 10x |
checkmate::assert_true(rmd_yaml_args[["output"]] %in% rmd_output) |
88 | ||
89 | 9x |
shiny::moduleServer(id, function(input, output, session) { |
90 | 9x |
shiny::setBookmarkExclude(c( |
91 | 9x |
"card_remove_id", "card_down_id", "card_up_id", "remove_card_ok", "showrcode", "download_data_prev", |
92 | 9x |
"load_reporter_previewer", "load_reporter" |
93 |
)) |
|
94 | ||
95 | 9x |
session$onBookmark(function(state) { |
96 | ! |
reporterdir <- file.path(state$dir, "reporter") |
97 | ! |
dir.create(reporterdir) |
98 | ! |
reporter$to_jsondir(reporterdir) |
99 |
}) |
|
100 | 9x |
session$onRestored(function(state) { |
101 | ! |
reporterdir <- file.path(state$dir, "reporter") |
102 | ! |
reporter$from_jsondir(reporterdir) |
103 |
}) |
|
104 | ||
105 | 9x |
ns <- session$ns |
106 | ||
107 | 9x |
reset_report_button_srv("resetButtonPreviewer", reporter) |
108 | ||
109 | 9x |
output$encoding <- shiny::renderUI({ |
110 | 7x |
reporter$get_reactive_add_card() |
111 | 7x |
nr_cards <- length(reporter$get_cards()) |
112 | ||
113 | 7x |
previewer_buttons_list <- list( |
114 | 7x |
download = htmltools::tagAppendAttributes( |
115 | 7x |
shiny::tags$a( |
116 | 7x |
id = ns("download_data_prev"), |
117 | 7x |
class = "btn btn-primary shiny-download-link simple_report_button", |
118 | 7x |
href = "", |
119 | 7x |
target = "_blank", |
120 | 7x |
download = NA, |
121 | 7x |
shiny::tags$span("Download Report", shiny::icon("download")) |
122 |
), |
|
123 | 7x |
class = if (nr_cards) "" else "disabled" |
124 |
), |
|
125 | 7x |
load = shiny::tags$button( |
126 | 7x |
id = ns("load_reporter_previewer"), |
127 | 7x |
type = "button", |
128 | 7x |
class = "btn btn-primary action-button simple_report_button", |
129 | 7x |
`data-val` = shiny::restoreInput(id = ns("load_reporter_previewer"), default = NULL), |
130 | 7x |
NULL, |
131 | 7x |
shiny::tags$span( |
132 | 7x |
"Load Report", shiny::icon("upload") |
133 |
) |
|
134 |
), |
|
135 | 7x |
reset = reset_report_button_ui(ns("resetButtonPreviewer"), label = "Reset Report") |
136 |
) |
|
137 | ||
138 | 7x |
shiny::tags$div( |
139 | 7x |
id = "previewer_reporter_encoding", |
140 | 7x |
shiny::tags$h3("Download the Report"), |
141 | 7x |
shiny::tags$hr(), |
142 | 7x |
reporter_download_inputs( |
143 | 7x |
rmd_yaml_args = rmd_yaml_args, |
144 | 7x |
rmd_output = rmd_output, |
145 | 7x |
showrcode = any_rcode_block(reporter), |
146 | 7x |
session = session |
147 |
), |
|
148 | 7x |
shiny::tags$div( |
149 | 7x |
id = "previewer_reporter_buttons", |
150 | 7x |
class = "previewer_buttons_line", |
151 | 7x |
lapply(previewer_buttons_list[previewer_buttons], shiny::tags$div) |
152 |
) |
|
153 |
) |
|
154 |
}) |
|
155 | ||
156 | 9x |
output$pcards <- shiny::renderUI({ |
157 | 9x |
reporter$get_reactive_add_card() |
158 | 9x |
input$card_remove_id |
159 | 9x |
input$card_down_id |
160 | 9x |
input$card_up_id |
161 | ||
162 | 9x |
cards <- reporter$get_cards() |
163 | ||
164 | 9x |
if (length(cards)) { |
165 | 8x |
shiny::tags$div( |
166 | 8x |
class = "panel-group accordion", |
167 | 8x |
id = "reporter_previewer_panel", |
168 | 8x |
lapply(seq_along(cards), function(ic) { |
169 | 14x |
previewer_collapse_item(ic, cards[[ic]]$get_name(), cards[[ic]]$get_content()) |
170 |
}) |
|
171 |
) |
|
172 |
} else { |
|
173 | 1x |
shiny::tags$div( |
174 | 1x |
id = "reporter_previewer_panel_no_cards", |
175 | 1x |
shiny::tags$p( |
176 | 1x |
class = "text-danger mt-4", |
177 | 1x |
shiny::tags$strong("No Cards added") |
178 |
) |
|
179 |
) |
|
180 |
} |
|
181 |
}) |
|
182 | ||
183 | 9x |
shiny::observeEvent(input$load_reporter_previewer, { |
184 | ! |
nr_cards <- length(reporter$get_cards()) |
185 | ! |
shiny::showModal( |
186 | ! |
shiny::modalDialog( |
187 | ! |
easyClose = TRUE, |
188 | ! |
shiny::tags$h3("Load the Reporter"), |
189 | ! |
shiny::tags$hr(), |
190 | ! |
shiny::fileInput(ns("archiver_zip"), "Choose Reporter File to Load (a zip file)", |
191 | ! |
multiple = FALSE, |
192 | ! |
accept = c(".zip") |
193 |
), |
|
194 | ! |
footer = shiny::div( |
195 | ! |
shiny::tags$button( |
196 | ! |
type = "button", |
197 | ! |
class = "btn btn-danger", |
198 | ! |
`data-dismiss` = "modal", |
199 | ! |
`data-bs-dismiss` = "modal", |
200 | ! |
NULL, |
201 | ! |
"Cancel" |
202 |
), |
|
203 | ! |
shiny::tags$button( |
204 | ! |
id = ns("load_reporter"), |
205 | ! |
type = "button", |
206 | ! |
class = "btn btn-primary action-button", |
207 | ! |
`data-val` = shiny::restoreInput(id = ns("load_reporter"), default = NULL), |
208 | ! |
NULL, |
209 | ! |
"Load" |
210 |
) |
|
211 |
) |
|
212 |
) |
|
213 |
) |
|
214 |
}) |
|
215 | ||
216 | 9x |
shiny::observeEvent(input$load_reporter, { |
217 | ! |
switch("JSON", |
218 | ! |
JSON = load_json_report(reporter, input$archiver_zip[["datapath"]], input$archiver_zip[["name"]]), |
219 | ! |
stop("The provided Reporter file format is not supported") |
220 |
) |
|
221 | ||
222 | ! |
shiny::removeModal() |
223 |
}) |
|
224 | ||
225 | 9x |
shiny::observeEvent(input$card_remove_id, { |
226 | 1x |
shiny::showModal( |
227 | 1x |
shiny::modalDialog( |
228 | 1x |
title = "Remove the Report Card", |
229 | 1x |
shiny::tags$p( |
230 | 1x |
shiny::HTML( |
231 | 1x |
sprintf( |
232 | 1x |
"Do you really want to remove <strong>the card %s</strong> from the Report?", |
233 | 1x |
input$card_remove_id |
234 |
) |
|
235 |
) |
|
236 |
), |
|
237 | 1x |
footer = shiny::tagList( |
238 | 1x |
shiny::tags$button( |
239 | 1x |
type = "button", |
240 | 1x |
class = "btn btn-secondary", |
241 | 1x |
`data-dismiss` = "modal", |
242 | 1x |
`data-bs-dismiss` = "modal", |
243 | 1x |
NULL, |
244 | 1x |
"Cancel" |
245 |
), |
|
246 | 1x |
shiny::actionButton(ns("remove_card_ok"), "OK", class = "btn-danger") |
247 |
) |
|
248 |
) |
|
249 |
) |
|
250 |
}) |
|
251 | ||
252 | 9x |
shiny::observeEvent(input$remove_card_ok, { |
253 | 1x |
reporter$remove_cards(input$card_remove_id) |
254 | 1x |
shiny::removeModal() |
255 |
}) |
|
256 | ||
257 | 9x |
shiny::observeEvent(input$card_up_id, { |
258 | 3x |
if (input$card_up_id > 1) { |
259 | 2x |
reporter$swap_cards( |
260 | 2x |
as.integer(input$card_up_id), |
261 | 2x |
as.integer(input$card_up_id - 1) |
262 |
) |
|
263 |
} |
|
264 |
}) |
|
265 | ||
266 | 9x |
shiny::observeEvent(input$card_down_id, { |
267 | 3x |
if (input$card_down_id < length(reporter$get_cards())) { |
268 | 2x |
reporter$swap_cards( |
269 | 2x |
as.integer(input$card_down_id), |
270 | 2x |
as.integer(input$card_down_id + 1) |
271 |
) |
|
272 |
} |
|
273 |
}) |
|
274 | ||
275 | 9x |
output$download_data_prev <- shiny::downloadHandler( |
276 | 9x |
filename = function() { |
277 | 1x |
paste0( |
278 | 1x |
"report_", |
279 | 1x |
if (reporter$get_id() == "") NULL else paste0(reporter$get_id(), "_"), |
280 | 1x |
format(Sys.time(), "%y%m%d%H%M%S"), |
281 | 1x |
".zip" |
282 |
) |
|
283 |
}, |
|
284 | 9x |
content = function(file) { |
285 | 1x |
shiny::showNotification("Rendering and Downloading the document.") |
286 | 1x |
shinybusy::block(id = ns("download_data_prev"), text = "", type = "dots") |
287 | 1x |
input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]]) |
288 | 1x |
names(input_list) <- names(rmd_yaml_args) |
289 | ! |
if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode |
290 | 1x |
report_render_and_compress(reporter, input_list, global_knitr, file) |
291 | 1x |
shinybusy::unblock(id = ns("download_data_prev")) |
292 |
}, |
|
293 | 9x |
contentType = "application/zip" |
294 |
) |
|
295 |
}) |
|
296 |
} |
|
297 | ||
298 |
#' @noRd |
|
299 |
#' @keywords internal |
|
300 |
block_to_html <- function(b) { |
|
301 | 42x |
b_content <- b$get_content() |
302 | 42x |
if (inherits(b, "TextBlock")) { |
303 | 28x |
switch(b$get_style(), |
304 | ! |
header1 = shiny::tags$h1(b_content), |
305 | 28x |
header2 = shiny::tags$h2(b_content), |
306 | ! |
header3 = shiny::tags$h3(b_content), |
307 | ! |
header4 = shiny::tags$h4(b_content), |
308 | ! |
verbatim = shiny::tags$pre(b_content), |
309 | ! |
shiny::tags$pre(b_content) |
310 |
) |
|
311 | 14x |
} else if (inherits(b, "RcodeBlock")) { |
312 | ! |
panel_item("R Code", shiny::tags$pre(b_content)) |
313 | 14x |
} else if (inherits(b, "PictureBlock")) { |
314 | 14x |
shiny::tags$img(src = knitr::image_uri(b_content)) |
315 | ! |
} else if (inherits(b, "TableBlock")) { |
316 | ! |
b_table <- readRDS(b_content) |
317 | ! |
shiny::tags$pre( |
318 | ! |
flextable::htmltools_value(b_table) |
319 |
) |
|
320 | ! |
} else if (inherits(b, "NewpageBlock")) { |
321 | ! |
shiny::tags$br() |
322 |
} else { |
|
323 | ! |
stop("Unknown block class") |
324 |
} |
|
325 |
} |
|
326 | ||
327 |
#' @noRd |
|
328 |
#' @keywords internal |
|
329 |
add_previewer_css <- function() { |
|
330 | 1x |
shiny::tagList( |
331 | 1x |
shiny::singleton( |
332 | 1x |
shiny::tags$head(shiny::includeCSS(system.file("css/Previewer.css", package = "teal.reporter"))) |
333 |
), |
|
334 | 1x |
shiny::singleton( |
335 | 1x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
336 |
) |
|
337 |
) |
|
338 |
} |
|
339 | ||
340 |
#' @noRd |
|
341 |
#' @keywords internal |
|
342 |
add_previewer_js <- function(ns) { |
|
343 | 1x |
shiny::singleton( |
344 | 1x |
shiny::tags$head(shiny::tags$script( |
345 | 1x |
shiny::HTML(sprintf(' |
346 | 1x |
$(document).ready(function(event) { |
347 | 1x |
$("body").on("click", "span.card_remove_id", function() { |
348 | 1x |
let val = $(this).data("cardid"); |
349 | 1x |
Shiny.setInputValue("%s", val, {priority: "event"}); |
350 |
}); |
|
351 | ||
352 | 1x |
$("body").on("click", "span.card_up_id", function() { |
353 | 1x |
let val = $(this).data("cardid"); |
354 | 1x |
Shiny.setInputValue("%s", val, {priority: "event"}); |
355 |
}); |
|
356 | ||
357 | 1x |
$("body").on("click", "span.card_down_id", function() { |
358 | 1x |
let val = $(this).data("cardid"); |
359 | 1x |
Shiny.setInputValue("%s", val, {priority: "event"}); |
360 |
}); |
|
361 |
}); |
|
362 | 1x |
', ns("card_remove_id"), ns("card_up_id"), ns("card_down_id"))) |
363 |
)) |
|
364 |
) |
|
365 |
} |
|
366 | ||
367 |
#' @noRd |
|
368 |
#' @keywords internal |
|
369 |
nav_previewer_icon <- function(name, icon_name, idx, size = 1L) { |
|
370 | 42x |
checkmate::assert_string(name) |
371 | 42x |
checkmate::assert_string(icon_name) |
372 | 42x |
checkmate::assert_int(size) |
373 | ||
374 | 42x |
shiny::tags$span( |
375 | 42x |
class = paste(name, "icon_previewer"), |
376 |
# data field needed to record clicked card on the js side |
|
377 | 42x |
`data-cardid` = idx, |
378 | 42x |
shiny::icon(icon_name, sprintf("fa-%sx", size)) |
379 |
) |
|
380 |
} |
|
381 | ||
382 |
#' @noRd |
|
383 |
#' @keywords internal |
|
384 |
nav_previewer_icons <- function(idx, size = 1L) { |
|
385 | 14x |
shiny::tags$span( |
386 | 14x |
class = "preview_card_control", |
387 | 14x |
nav_previewer_icon(name = "card_remove_id", icon_name = "xmark", idx = idx, size = size), |
388 | 14x |
nav_previewer_icon(name = "card_up_id", icon_name = "arrow-up", idx = idx, size = size), |
389 | 14x |
nav_previewer_icon(name = "card_down_id", icon_name = "arrow-down", idx = idx, size = size) |
390 |
) |
|
391 |
} |
|
392 | ||
393 |
#' @noRd |
|
394 |
#' @keywords internal |
|
395 |
previewer_collapse_item <- function(idx, card_name, card_blocks) { |
|
396 | 14x |
shiny::tags$div(.renderHook = function(x) { |
397 |
# get bs version |
|
398 | 14x |
version <- get_bs_version() |
399 | ||
400 | 14x |
if (version == "3") { |
401 | 14x |
shiny::tags$div( |
402 | 14x |
id = paste0("panel_card_", idx), |
403 | 14x |
class = "panel panel-default", |
404 | 14x |
shiny::tags$div( |
405 | 14x |
class = "panel-heading overflow-auto", |
406 | 14x |
shiny::tags$div( |
407 | 14x |
class = "panel-title", |
408 | 14x |
shiny::tags$span( |
409 | 14x |
nav_previewer_icons(idx = idx), |
410 | 14x |
shiny::tags$a( |
411 | 14x |
class = "accordion-toggle block py-3 px-4 -my-3 -mx-4", |
412 | 14x |
`data-toggle` = "collapse", |
413 | 14x |
`data-parent` = "#reporter_previewer_panel", |
414 | 14x |
href = paste0("#collapse", idx), |
415 | 14x |
shiny::tags$h4(paste0("Card ", idx, ": ", card_name), shiny::icon("caret-down")) |
416 |
) |
|
417 |
) |
|
418 |
) |
|
419 |
), |
|
420 | 14x |
shiny::tags$div( |
421 | 14x |
id = paste0("collapse", idx), class = "collapse out", |
422 | 14x |
shiny::tags$div( |
423 | 14x |
class = "panel-body", |
424 | 14x |
shiny::tags$div( |
425 | 14x |
id = paste0("card", idx), |
426 | 14x |
lapply( |
427 | 14x |
card_blocks, |
428 | 14x |
function(b) { |
429 | 42x |
block_to_html(b) |
430 |
} |
|
431 |
) |
|
432 |
) |
|
433 |
) |
|
434 |
) |
|
435 |
) |
|
436 |
} else { |
|
437 | ! |
shiny::tags$div( |
438 | ! |
id = paste0("panel_card_", idx), |
439 | ! |
class = "card", |
440 | ! |
shiny::tags$div( |
441 | ! |
class = "overflow-auto", |
442 | ! |
shiny::tags$div( |
443 | ! |
class = "card-header", |
444 | ! |
shiny::tags$span( |
445 | ! |
nav_previewer_icons(idx = idx), |
446 | ! |
shiny::tags$a( |
447 | ! |
class = "accordion-toggle block py-3 px-4 -my-3 -mx-4", |
448 |
# bs4 |
|
449 | ! |
`data-toggle` = "collapse", |
450 |
# bs5 |
|
451 | ! |
`data-bs-toggle` = "collapse", |
452 | ! |
href = paste0("#collapse", idx), |
453 | ! |
shiny::tags$h4( |
454 | ! |
paste0("Card ", idx, ": ", card_name), |
455 | ! |
shiny::icon("caret-down") |
456 |
) |
|
457 |
) |
|
458 |
) |
|
459 |
) |
|
460 |
), |
|
461 | ! |
shiny::tags$div( |
462 | ! |
id = paste0("collapse", idx), |
463 | ! |
class = "collapse out", |
464 |
# bs4 |
|
465 | ! |
`data-parent` = "#reporter_previewer_panel", |
466 |
# bs5 |
|
467 | ! |
`data-bs-parent` = "#reporter_previewer_panel", |
468 | ! |
shiny::tags$div( |
469 | ! |
class = "card-body", |
470 | ! |
shiny::tags$div( |
471 | ! |
id = paste0("card", idx), |
472 | ! |
lapply( |
473 | ! |
card_blocks, |
474 | ! |
function(b) { |
475 | ! |
block_to_html(b) |
476 |
} |
|
477 |
) |
|
478 |
) |
|
479 |
) |
|
480 |
) |
|
481 |
) |
|
482 |
} |
|
483 |
}) |
|
484 |
} |
1 |
#' Mark strings for quotation in `yaml` serialization |
|
2 |
#' |
|
3 |
#' This function is designed for use with the `yaml` package to explicitly, |
|
4 |
#' It adds an attribute to character strings, indicating that they should be serialized with double quotes. |
|
5 |
#' |
|
6 |
#' @param x (`character`) |
|
7 |
#' @keywords internal |
|
8 |
#' @examples |
|
9 |
#' library(yaml) |
|
10 |
#' yaml_quoted <- getFromNamespace("yaml_quoted", "teal.reporter") |
|
11 |
#' yaml <- list( |
|
12 |
#' author = yaml_quoted("NEST"), |
|
13 |
#' title = yaml_quoted("Report"), |
|
14 |
#' date = yaml_quoted("07/04/2019"), |
|
15 |
#' output = list(pdf_document = list(keep_tex = TRUE)) |
|
16 |
#' ) |
|
17 |
#' as.yaml(yaml) |
|
18 |
yaml_quoted <- function(x) { |
|
19 | 2x |
attr(x, "quoted") <- TRUE |
20 | 2x |
x |
21 |
} |
|
22 | ||
23 |
#' Create `markdown` header from `yaml` string |
|
24 |
#' |
|
25 |
#' This function wraps a `yaml`-formatted string in Markdown header delimiters. |
|
26 |
#' |
|
27 |
#' @param x (`character`) `yaml` formatted string. |
|
28 |
#' @keywords internal |
|
29 |
#' @examples |
|
30 |
#' library(yaml) |
|
31 |
#' yaml_quoted <- getFromNamespace("yaml_quoted", "teal.reporter") |
|
32 |
#' yaml <- list( |
|
33 |
#' author = yaml_quoted("NEST"), |
|
34 |
#' title = yaml_quoted("Report"), |
|
35 |
#' date = yaml_quoted("07/04/2019"), |
|
36 |
#' output = list(pdf_document = list(keep_tex = TRUE)) |
|
37 |
#' ) |
|
38 |
#' md_header <- getFromNamespace("md_header", "teal.reporter") |
|
39 |
#' md_header(as.yaml(yaml)) |
|
40 |
md_header <- function(x) { |
|
41 | 14x |
paste0("---\n", x, "---\n") |
42 |
} |
|
43 | ||
44 |
#' Convert `yaml` representation of a boolean strings to logical Values |
|
45 |
#' |
|
46 |
#' Converts a single `character` string representing a `yaml` boolean value into a logical value in `R`. |
|
47 |
#' |
|
48 |
#' @param input (`character(1)`) |
|
49 |
#' @param name (`charcter(1)`) |
|
50 |
#' @param pos_logi (`character`) vector of `yaml` values which should be treated as `TRUE`. |
|
51 |
#' @param neg_logi (`character`) vector of `yaml` values which should be treated as `FALSE`. |
|
52 |
#' @param silent (`logical(1)`) if to suppress the messages and warnings. |
|
53 |
#' @return `input` argument or the appropriate `logical` value. |
|
54 |
#' @keywords internal |
|
55 |
#' @examples |
|
56 |
#' conv_str_logi <- getFromNamespace("conv_str_logi", "teal.reporter") |
|
57 |
#' conv_str_logi("TRUE") |
|
58 |
#' conv_str_logi("True") |
|
59 |
#' |
|
60 |
#' conv_str_logi("off") |
|
61 |
#' conv_str_logi("n") |
|
62 |
#' |
|
63 |
#' conv_str_logi("sth") |
|
64 |
conv_str_logi <- function(input, |
|
65 |
name = "", |
|
66 |
pos_logi = c("TRUE", "true", "True", "yes", "y", "Y", "on"), |
|
67 |
neg_logi = c("FALSE", "false", "False", "no", "n", "N", "off"), |
|
68 |
silent = TRUE) { |
|
69 | 18x |
checkmate::assert_string(input) |
70 | 17x |
checkmate::assert_string(name) |
71 | 17x |
checkmate::assert_character(pos_logi) |
72 | 17x |
checkmate::assert_character(neg_logi) |
73 | 17x |
checkmate::assert_flag(silent) |
74 | ||
75 | 17x |
all_logi <- c(pos_logi, neg_logi) |
76 | 17x |
if (input %in% all_logi) { |
77 | 15x |
if (isFALSE(silent)) { |
78 | ! |
message(sprintf("The '%s' value should be a logical, so it is automatically converted.", input)) |
79 |
} |
|
80 | 15x |
input %in% pos_logi |
81 |
} else { |
|
82 | 2x |
input |
83 |
} |
|
84 |
} |
|
85 | ||
86 |
#' Get document output types from the `rmarkdown` package |
|
87 |
#' |
|
88 |
#' @description `r lifecycle::badge("experimental")` |
|
89 |
#' |
|
90 |
#' Retrieves vector of available document output types from the `rmarkdown` package, |
|
91 |
#' such as `pdf_document`, `html_document`, etc. |
|
92 |
#' |
|
93 |
#' @return `character` vector. |
|
94 |
#' @export |
|
95 |
#' @examples |
|
96 |
#' rmd_outputs() |
|
97 |
rmd_outputs <- function() { |
|
98 | 18x |
rmarkdown_namespace <- asNamespace("rmarkdown") |
99 | 18x |
ls(rmarkdown_namespace)[grep("_document|_presentation", ls(rmarkdown_namespace))] |
100 |
} |
|
101 | ||
102 |
#' Get document output arguments from the `rmarkdown` package |
|
103 |
#' |
|
104 |
#' @description `r lifecycle::badge("experimental")` |
|
105 |
#' |
|
106 |
#' Retrieves the arguments for a specified document output type from the `rmarkdown` package. |
|
107 |
#' |
|
108 |
#' @param output_name (`character`) `rmarkdown` output name. |
|
109 |
#' @param default_values (`logical(1)`) if to return a default values for each argument. |
|
110 |
#' @export |
|
111 |
#' @examples |
|
112 |
#' rmd_output_arguments("pdf_document") |
|
113 |
#' rmd_output_arguments("pdf_document", TRUE) |
|
114 |
rmd_output_arguments <- function(output_name, default_values = FALSE) { |
|
115 | 17x |
checkmate::assert_string(output_name) |
116 | 17x |
checkmate::assert_subset(output_name, rmd_outputs()) |
117 | ||
118 | 16x |
rmarkdown_namespace <- asNamespace("rmarkdown") |
119 | 16x |
if (default_values) { |
120 | 14x |
formals(rmarkdown_namespace[[output_name]]) |
121 |
} else { |
|
122 | 2x |
names(formals(rmarkdown_namespace[[output_name]])) |
123 |
} |
|
124 |
} |
|
125 | ||
126 |
#' Parse a named list to `yaml` header for an `Rmd` file |
|
127 |
#' |
|
128 |
#' @description `r lifecycle::badge("experimental")` |
|
129 |
#' |
|
130 |
#' Converts a named list into a `yaml` header for `Rmd`, handling output types and arguments |
|
131 |
#' as defined in the `rmarkdown` package. This function simplifies the process of generating `yaml` headers. |
|
132 |
#' |
|
133 |
#' @details |
|
134 |
#' This function processes a non-nested (flat) named list into a `yaml` header for an `Rmd` document. |
|
135 |
#' It supports all standard `Rmd` `yaml` header fields, including `author`, `date`, `title`, `subtitle`, |
|
136 |
#' `abstract`, `keywords`, `subject`, `description`, `category`, and `lang`. |
|
137 |
#' Additionally, it handles `output` field types and arguments as defined in the `rmarkdown` package. |
|
138 |
#' |
|
139 |
#' @note Only non-nested lists are automatically parsed. |
|
140 |
#' Nested lists require direct processing with `yaml::as.yaml`. |
|
141 |
#' |
|
142 |
#' @param input_list (`named list`) non nested with slots names and their values compatible with `Rmd` `yaml` header. |
|
143 |
#' @param as_header (`logical(1)`) optionally wrap with result with the internal `md_header()`, default `TRUE`. |
|
144 |
#' @param convert_logi (`logical(1)`) convert a character values to logical, |
|
145 |
#' if they are recognized as quoted `yaml` logical values , default `TRUE`. |
|
146 |
#' @param multi_output (`logical(1)`) multi `output` slots in the `input` argument, default `FALSE`. |
|
147 |
#' @param silent (`logical(1)`) suppress messages and warnings, default `FALSE`. |
|
148 |
#' @return `character` with `rmd_yaml_header` class, |
|
149 |
#' result of [`yaml::as.yaml`], optionally wrapped with internal `md_header()`. |
|
150 |
#' @export |
|
151 |
#' @examples |
|
152 |
#' # nested so using yaml::as.yaml directly |
|
153 |
#' as_yaml_auto( |
|
154 |
#' list(author = "", output = list(pdf_document = list(toc = TRUE))) |
|
155 |
#' ) |
|
156 |
#' |
|
157 |
#' # auto parsing for a flat list, like shiny input |
|
158 |
#' input <- list(author = "", output = "pdf_document", toc = TRUE, keep_tex = TRUE) |
|
159 |
#' as_yaml_auto(input) |
|
160 |
#' |
|
161 |
#' as_yaml_auto(list(author = "", output = "pdf_document", toc = TRUE, keep_tex = "TRUE")) |
|
162 |
#' |
|
163 |
#' as_yaml_auto(list( |
|
164 |
#' author = "", output = "pdf_document", toc = TRUE, keep_tex = TRUE, |
|
165 |
#' wrong = 2 |
|
166 |
#' )) |
|
167 |
#' |
|
168 |
#' as_yaml_auto(list(author = "", output = "pdf_document", toc = TRUE, keep_tex = 2), |
|
169 |
#' silent = TRUE |
|
170 |
#' ) |
|
171 |
#' |
|
172 |
#' input <- list(author = "", output = "pdf_document", toc = TRUE, keep_tex = "True") |
|
173 |
#' as_yaml_auto(input) |
|
174 |
#' as_yaml_auto(input, convert_logi = TRUE, silent = TRUE) |
|
175 |
#' as_yaml_auto(input, silent = TRUE) |
|
176 |
#' as_yaml_auto(input, convert_logi = FALSE, silent = TRUE) |
|
177 |
#' |
|
178 |
#' as_yaml_auto( |
|
179 |
#' list( |
|
180 |
#' author = "", output = "pdf_document", |
|
181 |
#' output = "html_document", toc = TRUE, keep_tex = TRUE |
|
182 |
#' ), |
|
183 |
#' multi_output = TRUE |
|
184 |
#' ) |
|
185 |
#' as_yaml_auto( |
|
186 |
#' list( |
|
187 |
#' author = "", output = "pdf_document", |
|
188 |
#' output = "html_document", toc = "True", keep_tex = TRUE |
|
189 |
#' ), |
|
190 |
#' multi_output = TRUE |
|
191 |
#' ) |
|
192 |
as_yaml_auto <- function(input_list, |
|
193 |
as_header = TRUE, |
|
194 |
convert_logi = TRUE, |
|
195 |
multi_output = FALSE, |
|
196 |
silent = FALSE) { |
|
197 | 16x |
checkmate::assert_logical(as_header) |
198 | 16x |
checkmate::assert_logical(convert_logi) |
199 | 16x |
checkmate::assert_logical(silent) |
200 | 16x |
checkmate::assert_logical(multi_output) |
201 | ||
202 | 16x |
if (multi_output) { |
203 | 1x |
checkmate::assert_list(input_list, names = "named") |
204 |
} else { |
|
205 | 15x |
checkmate::assert_list(input_list, names = "unique") |
206 |
} |
|
207 | ||
208 | 13x |
is_nested <- function(x) any(unlist(lapply(x, is.list))) |
209 | 13x |
if (is_nested(input_list)) { |
210 | 2x |
result <- input_list |
211 |
} else { |
|
212 | 11x |
result <- list() |
213 | 11x |
input_nams <- names(input_list) |
214 | ||
215 |
# top fields |
|
216 | 11x |
top_fields <- c( |
217 | 11x |
"author", "date", "title", "subtitle", "abstract", |
218 | 11x |
"keywords", "subject", "description", "category", "lang" |
219 |
) |
|
220 | 11x |
for (itop in top_fields) { |
221 | 110x |
if (itop %in% input_nams) { |
222 | 20x |
result[[itop]] <- switch(itop, |
223 | 20x |
date = as.character(input_list[[itop]]), |
224 | 20x |
input_list[[itop]] |
225 |
) |
|
226 |
} |
|
227 |
} |
|
228 | ||
229 |
# output field |
|
230 | 11x |
doc_types <- unlist(input_list[input_nams == "output"]) |
231 | 11x |
if (length(doc_types)) { |
232 | 11x |
for (dtype in doc_types) { |
233 | 12x |
doc_type_args <- rmd_output_arguments(dtype, TRUE) |
234 | 12x |
doc_type_args_nams <- names(doc_type_args) |
235 | 12x |
any_output_arg <- any(input_nams %in% doc_type_args_nams) |
236 | ||
237 | 12x |
not_found_args <- setdiff(input_nams, c(doc_type_args_nams, top_fields, "output")) |
238 | 12x |
if (isFALSE(silent) && length(not_found_args) > 0 && isFALSE(multi_output)) { |
239 | 1x |
warning(sprintf("Not recognized and skipped arguments: %s", paste(not_found_args, collapse = ", "))) |
240 |
} |
|
241 | ||
242 | 12x |
if (any_output_arg) { |
243 | 11x |
doc_list <- list() |
244 | 11x |
doc_list[[dtype]] <- list() |
245 | 11x |
for (e in intersect(input_nams, doc_type_args_nams)) { |
246 | 17x |
if (is.logical(doc_type_args[[e]]) && is.character(input_list[[e]])) { |
247 | 1x |
pos_logi <- c("TRUE", "true", "True", "yes", "y", "Y", "on") |
248 | 1x |
neg_logi <- c("FALSE", "false", "False", "no", "n", "N", "off") |
249 | 1x |
all_logi <- c(pos_logi, neg_logi) |
250 | 1x |
if (input_list[[e]] %in% all_logi && convert_logi) { |
251 | 1x |
input_list[[e]] <- conv_str_logi(input_list[[e]], e, |
252 | 1x |
pos_logi = pos_logi, |
253 | 1x |
neg_logi = neg_logi, silent = silent |
254 |
) |
|
255 |
} |
|
256 |
} |
|
257 | ||
258 | 17x |
doc_list[[dtype]][[e]] <- input_list[[e]] |
259 |
} |
|
260 | 11x |
result[["output"]] <- append(result[["output"]], doc_list) |
261 |
} else { |
|
262 | 1x |
result[["output"]] <- append(result[["output"]], input_list[["output"]]) |
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 | ||
268 | 13x |
result <- yaml::as.yaml(result) |
269 | 13x |
if (as_header) { |
270 | 12x |
result <- md_header(result) |
271 |
} |
|
272 | 13x |
structure(result, class = "rmd_yaml_header") |
273 |
} |
|
274 | ||
275 |
#' Print method for the `yaml_header` class |
|
276 |
#' |
|
277 |
#' `r lifecycle::badge("experimental")` |
|
278 |
#' |
|
279 |
#' @param x (`rmd_yaml_header`) class object. |
|
280 |
#' @param ... optional text. |
|
281 |
#' @return `NULL`. |
|
282 |
#' @exportS3Method |
|
283 |
#' @examples |
|
284 |
#' input <- list(author = "", output = "pdf_document", toc = TRUE, keep_tex = TRUE) |
|
285 |
#' out <- as_yaml_auto(input) |
|
286 |
#' out |
|
287 |
#' print(out) |
|
288 |
print.rmd_yaml_header <- function(x, ...) { |
|
289 | ! |
cat(x, ...) |
290 |
} |
|
291 | ||
292 |
#' Extract field from `yaml` text |
|
293 |
#' |
|
294 |
#' Parses `yaml` text, extracting the specified field. Returns list names if it's a list; |
|
295 |
#' otherwise, the field itself. |
|
296 |
#' |
|
297 |
#' @param yaml_text (`rmd_yaml_header` or `character`) vector containing the `yaml` text. |
|
298 |
#' @param field_name (`character`) the name of the field to extract. |
|
299 |
#' |
|
300 |
#' @return If the field is a list, it returns the names of elements in the list; otherwise, |
|
301 |
#' it returns the extracted field. |
|
302 |
#' |
|
303 |
#' @keywords internal |
|
304 |
get_yaml_field <- function(yaml_text, field_name) { |
|
305 | 8x |
checkmate::assert_multi_class(yaml_text, c("rmd_yaml_header", "character")) |
306 | 8x |
checkmate::assert_string(field_name) |
307 | ||
308 | 8x |
yaml_obj <- yaml::yaml.load(yaml_text) |
309 | ||
310 | 8x |
result <- yaml_obj[[field_name]] |
311 | 8x |
if (is.list(result)) { |
312 | 5x |
result <- names(result) |
313 |
} |
|
314 | 8x |
result |
315 |
} |
1 |
#' Download report button module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' Provides a button that triggers downloading a report. |
|
6 |
#' |
|
7 |
#' For more information, refer to the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
8 |
#' |
|
9 |
#' @details `r global_knitr_details()` |
|
10 |
#' |
|
11 |
#' @name download_report_button |
|
12 |
#' |
|
13 |
#' @param id (`character(1)`) this `shiny` module's id. |
|
14 |
#' @param reporter (`Reporter`) instance. |
|
15 |
#' @param global_knitr (`list`) of `knitr` parameters (passed to `knitr::opts_chunk$set`) |
|
16 |
#' for customizing the rendering process. |
|
17 |
#' @inheritParams reporter_download_inputs |
|
18 |
#' |
|
19 |
#' @return `NULL`. |
|
20 |
NULL |
|
21 | ||
22 |
#' @rdname download_report_button |
|
23 |
#' @export |
|
24 |
download_report_button_ui <- function(id) { |
|
25 | 2x |
ns <- shiny::NS(id) |
26 | 2x |
shiny::tagList( |
27 | 2x |
shiny::singleton( |
28 | 2x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
29 |
), |
|
30 | 2x |
shiny::tags$button( |
31 | 2x |
id = ns("download_button"), |
32 | 2x |
type = "button", |
33 | 2x |
class = "simple_report_button btn btn-primary action-button", |
34 | 2x |
title = "Download", |
35 | 2x |
`data-val` = shiny::restoreInput(id = ns("download_button"), default = NULL), |
36 | 2x |
NULL, |
37 | 2x |
shiny::tags$span( |
38 | 2x |
shiny::icon("download") |
39 |
) |
|
40 |
) |
|
41 |
) |
|
42 |
} |
|
43 | ||
44 |
#' @rdname download_report_button |
|
45 |
#' @export |
|
46 |
download_report_button_srv <- function(id, |
|
47 |
reporter, |
|
48 |
global_knitr = getOption("teal.reporter.global_knitr"), |
|
49 |
rmd_output = c( |
|
50 |
"html" = "html_document", "pdf" = "pdf_document", |
|
51 |
"powerpoint" = "powerpoint_presentation", "word" = "word_document" |
|
52 |
), |
|
53 |
rmd_yaml_args = list( |
|
54 |
author = "NEST", title = "Report", |
|
55 |
date = as.character(Sys.Date()), output = "html_document", |
|
56 |
toc = FALSE |
|
57 |
)) { |
|
58 | 10x |
checkmate::assert_class(reporter, "Reporter") |
59 | 10x |
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get())) |
60 | 10x |
checkmate::assert_subset( |
61 | 10x |
rmd_output, |
62 | 10x |
c( |
63 | 10x |
"html_document", "pdf_document", |
64 | 10x |
"powerpoint_presentation", "word_document" |
65 |
), |
|
66 | 10x |
empty.ok = FALSE |
67 |
) |
|
68 | 10x |
checkmate::assert_list(rmd_yaml_args, names = "named") |
69 | 10x |
checkmate::assert_names( |
70 | 10x |
names(rmd_yaml_args), |
71 | 10x |
subset.of = c("author", "title", "date", "output", "toc"), |
72 | 10x |
must.include = "output" |
73 |
) |
|
74 | 8x |
checkmate::assert_true(rmd_yaml_args[["output"]] %in% rmd_output) |
75 | ||
76 | 7x |
shiny::moduleServer(id, function(input, output, session) { |
77 | 7x |
shiny::setBookmarkExclude(c("download_button")) |
78 | ||
79 | 7x |
ns <- session$ns |
80 | ||
81 | 7x |
download_modal <- function() { |
82 | 1x |
nr_cards <- length(reporter$get_cards()) |
83 | 1x |
downb <- shiny::tags$a( |
84 | 1x |
id = ns("download_data"), |
85 | 1x |
class = paste("btn btn-primary shiny-download-link", if (nr_cards) NULL else "disabled"), |
86 | 1x |
style = if (nr_cards) NULL else "pointer-events: none;", |
87 | 1x |
href = "", |
88 | 1x |
target = "_blank", |
89 | 1x |
download = NA, |
90 | 1x |
shiny::icon("download"), |
91 | 1x |
"Download" |
92 |
) |
|
93 | 1x |
shiny::modalDialog( |
94 | 1x |
easyClose = TRUE, |
95 | 1x |
shiny::tags$h3("Download the Report"), |
96 | 1x |
shiny::tags$hr(), |
97 | 1x |
if (length(reporter$get_cards()) == 0) { |
98 | ! |
shiny::tags$div( |
99 | ! |
class = "mb-4", |
100 | ! |
shiny::tags$p( |
101 | ! |
class = "text-danger", |
102 | ! |
shiny::tags$strong("No Cards Added") |
103 |
) |
|
104 |
) |
|
105 |
} else { |
|
106 | 1x |
shiny::tags$div( |
107 | 1x |
class = "mb-4", |
108 | 1x |
shiny::tags$p( |
109 | 1x |
class = "text-success", |
110 | 1x |
shiny::tags$strong(paste("Number of cards: ", nr_cards)) |
111 |
), |
|
112 |
) |
|
113 |
}, |
|
114 | 1x |
reporter_download_inputs( |
115 | 1x |
rmd_yaml_args = rmd_yaml_args, |
116 | 1x |
rmd_output = rmd_output, |
117 | 1x |
showrcode = any_rcode_block(reporter), |
118 | 1x |
session = session |
119 |
), |
|
120 | 1x |
footer = shiny::tagList( |
121 | 1x |
shiny::tags$button( |
122 | 1x |
type = "button", |
123 | 1x |
class = "btn btn-secondary", |
124 | 1x |
`data-dismiss` = "modal", |
125 | 1x |
`data-bs-dismiss` = "modal", |
126 | 1x |
NULL, |
127 | 1x |
"Cancel" |
128 |
), |
|
129 | 1x |
downb |
130 |
) |
|
131 |
) |
|
132 |
} |
|
133 | ||
134 | 7x |
shiny::observeEvent(input$download_button, { |
135 | 1x |
shiny::showModal(download_modal()) |
136 |
}) |
|
137 | ||
138 | 7x |
output$download_data <- shiny::downloadHandler( |
139 | 7x |
filename = function() { |
140 | 2x |
paste0( |
141 | 2x |
"report_", |
142 | 2x |
if (reporter$get_id() == "") NULL else paste0(reporter$get_id(), "_"), |
143 | 2x |
format(Sys.time(), "%y%m%d%H%M%S"), |
144 | 2x |
".zip" |
145 |
) |
|
146 |
}, |
|
147 | 7x |
content = function(file) { |
148 | 2x |
shiny::showNotification("Rendering and Downloading the document.") |
149 | 2x |
shinybusy::block(id = ns("download_data"), text = "", type = "dots") |
150 | 2x |
input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]]) |
151 | 2x |
names(input_list) <- names(rmd_yaml_args) |
152 | ! |
if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode |
153 | 2x |
report_render_and_compress(reporter, input_list, global_knitr, file) |
154 | 2x |
shinybusy::unblock(id = ns("download_data")) |
155 |
}, |
|
156 | 7x |
contentType = "application/zip" |
157 |
) |
|
158 |
}) |
|
159 |
} |
|
160 | ||
161 |
#' Render the report |
|
162 |
#' |
|
163 |
#' Render the report and zip the created directory. |
|
164 |
#' |
|
165 |
#' @param reporter (`Reporter`) instance. |
|
166 |
#' @param input_list (`list`) like `shiny` input converted to a regular named list. |
|
167 |
#' @param global_knitr (`list`) a global `knitr` parameters, like echo. |
|
168 |
#' But if local parameter is set it will have priority. |
|
169 |
#' @param file (`character(1)`) where to copy the returned directory. |
|
170 |
#' |
|
171 |
#' @return `file` argument, invisibly. |
|
172 |
#' |
|
173 |
#' @keywords internal |
|
174 |
report_render_and_compress <- function(reporter, input_list, global_knitr, file = tempdir()) { |
|
175 | 8x |
checkmate::assert_class(reporter, "Reporter") |
176 | 8x |
checkmate::assert_list(input_list, names = "named") |
177 | 7x |
checkmate::assert_string(file) |
178 | ||
179 |
if ( |
|
180 | 5x |
identical("pdf_document", input_list$output) && |
181 | 5x |
inherits(try(system2("pdflatex", "--version", stdout = TRUE), silent = TRUE), "try-error") |
182 |
) { |
|
183 | ! |
shiny::showNotification( |
184 | ! |
ui = "pdflatex is not available so the pdf_document could not be rendered. Please use other output type.", |
185 | ! |
action = "Please contact app developer", |
186 | ! |
type = "error" |
187 |
) |
|
188 | ! |
stop("pdflatex is not available so the pdf_document could not be rendered.") |
189 |
} |
|
190 | ||
191 | 5x |
yaml_header <- as_yaml_auto(input_list) |
192 | 5x |
renderer <- Renderer$new() |
193 | ||
194 | 5x |
tryCatch( |
195 | 5x |
renderer$render(reporter$get_blocks(), yaml_header, global_knitr), |
196 | 5x |
warning = function(cond) { |
197 | ! |
print(cond) |
198 | ! |
shiny::showNotification( |
199 | ! |
ui = "Render document warning!", |
200 | ! |
action = "Please contact app developer", |
201 | ! |
type = "warning" |
202 |
) |
|
203 |
}, |
|
204 | 5x |
error = function(cond) { |
205 | ! |
print(cond) |
206 | ! |
shiny::showNotification( |
207 | ! |
ui = "Render document error!", |
208 | ! |
action = "Please contact app developer", |
209 | ! |
type = "error" |
210 |
) |
|
211 |
} |
|
212 |
) |
|
213 | ||
214 | 5x |
output_dir <- renderer$get_output_dir() |
215 | ||
216 | 5x |
tryCatch( |
217 | 5x |
archiver_dir <- reporter$to_jsondir(output_dir), |
218 | 5x |
warning = function(cond) { |
219 | ! |
print(cond) |
220 | ! |
shiny::showNotification( |
221 | ! |
ui = "Archive document warning!", |
222 | ! |
action = "Please contact app developer", |
223 | ! |
type = "warning" |
224 |
) |
|
225 |
}, |
|
226 | 5x |
error = function(cond) { |
227 | ! |
print(cond) |
228 | ! |
shiny::showNotification( |
229 | ! |
ui = "Archive document error!", |
230 | ! |
action = "Please contact app developer", |
231 | ! |
type = "error" |
232 |
) |
|
233 |
} |
|
234 |
) |
|
235 | ||
236 | 5x |
temp_zip_file <- tempfile(fileext = ".zip") |
237 | 5x |
tryCatch( |
238 | 5x |
expr = zip::zipr(temp_zip_file, output_dir), |
239 | 5x |
warning = function(cond) { |
240 | ! |
print(cond) |
241 | ! |
shiny::showNotification( |
242 | ! |
ui = "Zipping folder warning!", |
243 | ! |
action = "Please contact app developer", |
244 | ! |
type = "warning" |
245 |
) |
|
246 |
}, |
|
247 | 5x |
error = function(cond) { |
248 | ! |
print(cond) |
249 | ! |
shiny::showNotification( |
250 | ! |
ui = "Zipping folder error!", |
251 | ! |
action = "Please contact app developer", |
252 | ! |
type = "error" |
253 |
) |
|
254 |
} |
|
255 |
) |
|
256 | ||
257 | 5x |
tryCatch( |
258 | 5x |
expr = file.copy(temp_zip_file, file), |
259 | 5x |
warning = function(cond) { |
260 | ! |
print(cond) |
261 | ! |
shiny::showNotification( |
262 | ! |
ui = "Copying file warning!", |
263 | ! |
action = "Please contact app developer", |
264 | ! |
type = "warning" |
265 |
) |
|
266 |
}, |
|
267 | 5x |
error = function(cond) { |
268 | ! |
print(cond) |
269 | ! |
shiny::showNotification( |
270 | ! |
ui = "Copying file error!", |
271 | ! |
action = "Please contact app developer", |
272 | ! |
type = "error" |
273 |
) |
|
274 |
} |
|
275 |
) |
|
276 | ||
277 | 5x |
rm(renderer) |
278 | 5x |
invisible(file) |
279 |
} |
|
280 | ||
281 |
#' Get the custom list of UI inputs |
|
282 |
#' |
|
283 |
#' @param rmd_output (`character`) vector with `rmarkdown` output types, |
|
284 |
#' by default all possible `pdf_document`, `html_document`, `powerpoint_presentation`, and `word_document`. |
|
285 |
#' If vector is named then those names will appear in the `UI`. |
|
286 |
#' @param rmd_yaml_args (`named list`) with `Rmd` `yaml` header fields and their default values. |
|
287 |
#' This `list` will result in the custom subset of UI inputs for the download reporter functionality. |
|
288 |
#' Default `list(author = "NEST", title = "Report", date = Sys.Date(), output = "html_document", toc = FALSE)`. |
|
289 |
#' The `list` must include at least `"output"` field. |
|
290 |
#' The default value for `"output"` has to be in the `rmd_output` argument. |
|
291 |
#' |
|
292 |
#' @keywords internal |
|
293 |
reporter_download_inputs <- function(rmd_yaml_args, rmd_output, showrcode, session) { |
|
294 | 8x |
shiny::tagList( |
295 | 8x |
lapply(names(rmd_yaml_args), function(e) { |
296 | 40x |
switch(e, |
297 | 8x |
author = shiny::textInput(session$ns("author"), label = "Author:", value = rmd_yaml_args$author), |
298 | 8x |
title = shiny::textInput(session$ns("title"), label = "Title:", value = rmd_yaml_args$title), |
299 | 8x |
date = shiny::dateInput(session$ns("date"), "Date:", value = rmd_yaml_args$date), |
300 | 8x |
output = shiny::tags$div( |
301 | 8x |
shinyWidgets::pickerInput( |
302 | 8x |
inputId = session$ns("output"), |
303 | 8x |
label = "Choose a document type: ", |
304 | 8x |
choices = rmd_output, |
305 | 8x |
selected = rmd_yaml_args$output |
306 |
) |
|
307 |
), |
|
308 | 8x |
toc = shiny::checkboxInput(session$ns("toc"), label = "Include Table of Contents", value = rmd_yaml_args$toc) |
309 |
) |
|
310 |
}), |
|
311 | 8x |
if (showrcode) { |
312 | ! |
shiny::checkboxInput( |
313 | ! |
session$ns("showrcode"), |
314 | ! |
label = "Include R Code", |
315 | ! |
value = FALSE |
316 |
) |
|
317 |
} |
|
318 |
) |
|
319 |
} |
|
320 | ||
321 |
#' @noRd |
|
322 |
#' @keywords internal |
|
323 |
any_rcode_block <- function(reporter) { |
|
324 | 10x |
any( |
325 | 10x |
vapply( |
326 | 10x |
reporter$get_blocks(), |
327 | 10x |
function(e) inherits(e, "RcodeBlock"), |
328 | 10x |
logical(1) |
329 |
) |
|
330 |
) |
|
331 |
} |
1 |
#' @title `Reporter`: An `R6` class for managing report cards |
|
2 |
#' @docType class |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' This `R6` class is designed to store and manage report cards, |
|
6 |
#' facilitating the creation, manipulation, and serialization of report-related data. |
|
7 |
#' |
|
8 |
#' @export |
|
9 |
#' |
|
10 |
Reporter <- R6::R6Class( # nolint: object_name_linter. |
|
11 |
classname = "Reporter", |
|
12 |
public = list( |
|
13 |
#' @description Initialize a `Reporter` object. |
|
14 |
#' |
|
15 |
#' @return Object of class `Reporter`, invisibly. |
|
16 |
#' @examples |
|
17 |
#' reporter <- Reporter$new() |
|
18 |
#' |
|
19 |
initialize = function() { |
|
20 | 38x |
private$cards <- list() |
21 | 38x |
private$reactive_add_card <- shiny::reactiveVal(0) |
22 | 38x |
invisible(self) |
23 |
}, |
|
24 |
#' @description Append one or more `ReportCard` objects to the `Reporter`. |
|
25 |
#' |
|
26 |
#' @param cards (`ReportCard`) or a list of such objects |
|
27 |
#' @return `self`, invisibly. |
|
28 |
#' @examplesIf require("ggplot2") |
|
29 |
#' library(ggplot2) |
|
30 |
#' library(rtables) |
|
31 |
#' |
|
32 |
#' card1 <- ReportCard$new() |
|
33 |
#' |
|
34 |
#' card1$append_text("Header 2 text", "header2") |
|
35 |
#' card1$append_text("A paragraph of default text", "header2") |
|
36 |
#' card1$append_plot( |
|
37 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
38 |
#' ) |
|
39 |
#' |
|
40 |
#' card2 <- ReportCard$new() |
|
41 |
#' |
|
42 |
#' card2$append_text("Header 2 text", "header2") |
|
43 |
#' card2$append_text("A paragraph of default text", "header2") |
|
44 |
#' lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean) |
|
45 |
#' table_res2 <- build_table(lyt, airquality) |
|
46 |
#' card2$append_table(table_res2) |
|
47 |
#' card2$append_table(iris) |
|
48 |
#' |
|
49 |
#' reporter <- Reporter$new() |
|
50 |
#' reporter$append_cards(list(card1, card2)) |
|
51 |
append_cards = function(cards) { |
|
52 | 36x |
checkmate::assert_list(cards, "ReportCard") |
53 | 36x |
private$cards <- append(private$cards, cards) |
54 | 36x |
private$reactive_add_card(length(private$cards)) |
55 | 36x |
invisible(self) |
56 |
}, |
|
57 |
#' @description Retrieves all `ReportCard` objects contained in the `Reporter`. |
|
58 |
#' |
|
59 |
#' @return A (`list`) of [`ReportCard`] objects. |
|
60 |
#' @examplesIf require("ggplot2") |
|
61 |
#' library(ggplot2) |
|
62 |
#' library(rtables) |
|
63 |
#' |
|
64 |
#' card1 <- ReportCard$new() |
|
65 |
#' |
|
66 |
#' card1$append_text("Header 2 text", "header2") |
|
67 |
#' card1$append_text("A paragraph of default text", "header2") |
|
68 |
#' card1$append_plot( |
|
69 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
70 |
#' ) |
|
71 |
#' |
|
72 |
#' card2 <- ReportCard$new() |
|
73 |
#' |
|
74 |
#' card2$append_text("Header 2 text", "header2") |
|
75 |
#' card2$append_text("A paragraph of default text", "header2") |
|
76 |
#' lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean) |
|
77 |
#' table_res2 <- build_table(lyt, airquality) |
|
78 |
#' card2$append_table(table_res2) |
|
79 |
#' card2$append_table(iris) |
|
80 |
#' |
|
81 |
#' reporter <- Reporter$new() |
|
82 |
#' reporter$append_cards(list(card1, card2)) |
|
83 |
#' reporter$get_cards() |
|
84 |
get_cards = function() { |
|
85 | 79x |
private$cards |
86 |
}, |
|
87 |
#' @description Compiles and returns all content blocks from the [`ReportCard`] in the `Reporter`. |
|
88 |
#' |
|
89 |
#' @param sep An optional separator to insert between each content block. |
|
90 |
#' Default is a `NewpageBlock$new()`object. |
|
91 |
#' @return `list()` list of `TableBlock`, `TextBlock`, `PictureBlock` and `NewpageBlock`. |
|
92 |
#' @examplesIf require("ggplot2") |
|
93 |
#' library(ggplot2) |
|
94 |
#' library(rtables) |
|
95 |
#' |
|
96 |
#' card1 <- ReportCard$new() |
|
97 |
#' |
|
98 |
#' card1$append_text("Header 2 text", "header2") |
|
99 |
#' card1$append_text("A paragraph of default text", "header2") |
|
100 |
#' card1$append_plot( |
|
101 |
#' ggplot(iris, aes(x = Petal.Length)) + geom_histogram() |
|
102 |
#' ) |
|
103 |
#' |
|
104 |
#' card2 <- ReportCard$new() |
|
105 |
#' |
|
106 |
#' card2$append_text("Header 2 text", "header2") |
|
107 |
#' card2$append_text("A paragraph of default text", "header2") |
|
108 |
#' lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean) |
|
109 |
#' table_res2 <- build_table(lyt, airquality) |
|
110 |
#' card2$append_table(table_res2) |
|
111 |
#' card2$append_table(iris) |
|
112 |
#' |
|
113 |
#' reporter <- Reporter$new() |
|
114 |
#' reporter$append_cards(list(card1, card2)) |
|
115 |
#' reporter$get_blocks() |
|
116 |
#' |
|
117 |
get_blocks = function(sep = NewpageBlock$new()) { |
|
118 | 38x |
blocks <- list() |
119 | 38x |
if (length(private$cards) > 0) { |
120 | 35x |
for (card_idx in head(seq_along(private$cards), -1)) { |
121 | 10x |
blocks <- append(blocks, append(private$cards[[card_idx]]$get_content(), sep)) |
122 |
} |
|
123 | 35x |
blocks <- append(blocks, private$cards[[length(private$cards)]]$get_content()) |
124 |
} |
|
125 | 38x |
blocks |
126 |
}, |
|
127 |
#' @description Resets the `Reporter`, removing all [`ReportCard`] objects and metadata. |
|
128 |
#' |
|
129 |
#' @return `self`, invisibly. |
|
130 |
#' |
|
131 |
reset = function() { |
|
132 | 20x |
private$cards <- list() |
133 | 20x |
private$metadata <- list() |
134 | 20x |
private$reactive_add_card(0) |
135 | 20x |
invisible(self) |
136 |
}, |
|
137 |
#' @description Removes specific `ReportCard` objects from the `Reporter` by their indices. |
|
138 |
#' |
|
139 |
#' @param ids (`integer(id)`) the indexes of cards |
|
140 |
#' @return `self`, invisibly. |
|
141 |
remove_cards = function(ids = NULL) { |
|
142 | 1x |
checkmate::assert( |
143 | 1x |
checkmate::check_null(ids), |
144 | 1x |
checkmate::check_integer(ids, min.len = 1, max.len = length(private$cards)) |
145 |
) |
|
146 | 1x |
if (!is.null(ids)) { |
147 | 1x |
private$cards <- private$cards[-ids] |
148 |
} |
|
149 | 1x |
private$reactive_add_card(length(private$cards)) |
150 | 1x |
invisible(self) |
151 |
}, |
|
152 |
#' @description Swaps the positions of two `ReportCard` objects within the `Reporter`. |
|
153 |
#' |
|
154 |
#' @param start (`integer`) the index of the first card |
|
155 |
#' @param end (`integer`) the index of the second card |
|
156 |
#' @return `self`, invisibly. |
|
157 |
swap_cards = function(start, end) { |
|
158 | 6x |
checkmate::assert( |
159 | 6x |
checkmate::check_integer(start, |
160 | 6x |
min.len = 1, max.len = 1, lower = 1, upper = length(private$cards) |
161 |
), |
|
162 | 6x |
checkmate::check_integer(end, |
163 | 6x |
min.len = 1, max.len = 1, lower = 1, upper = length(private$cards) |
164 |
), |
|
165 | 6x |
combine = "and" |
166 |
) |
|
167 | 6x |
start_val <- private$cards[[start]]$clone() |
168 | 6x |
end_val <- private$cards[[end]]$clone() |
169 | 6x |
private$cards[[start]] <- end_val |
170 | 6x |
private$cards[[end]] <- start_val |
171 | 6x |
invisible(self) |
172 |
}, |
|
173 |
#' @description Gets the current value of the reactive variable for adding cards. |
|
174 |
#' |
|
175 |
#' @return `reactive_add_card` current `numeric` value of the reactive variable. |
|
176 |
#' @note The function has to be used in the shiny reactive context. |
|
177 |
#' @examples |
|
178 |
#' library(shiny) |
|
179 |
#' |
|
180 |
#' isolate(Reporter$new()$get_reactive_add_card()) |
|
181 |
get_reactive_add_card = function() { |
|
182 | 23x |
private$reactive_add_card() |
183 |
}, |
|
184 |
#' @description Get the metadata associated with this `Reporter`. |
|
185 |
#' |
|
186 |
#' @return `named list` of metadata to be appended. |
|
187 |
#' @examples |
|
188 |
#' reporter <- Reporter$new()$append_metadata(list(sth = "sth")) |
|
189 |
#' reporter$get_metadata() |
|
190 |
#' |
|
191 |
get_metadata = function() { |
|
192 | 23x |
private$metadata |
193 |
}, |
|
194 |
#' @description Appends metadata to this `Reporter`. |
|
195 |
#' |
|
196 |
#' @param meta (`named list`) of metadata to be appended. |
|
197 |
#' @return `self`, invisibly. |
|
198 |
#' @examples |
|
199 |
#' reporter <- Reporter$new()$append_metadata(list(sth = "sth")) |
|
200 |
#' reporter$get_metadata() |
|
201 |
#' |
|
202 |
append_metadata = function(meta) { |
|
203 | 20x |
checkmate::assert_list(meta, names = "unique") |
204 | 17x |
checkmate::assert_true(length(meta) == 0 || all(!names(meta) %in% names(private$metadata))) |
205 | 16x |
private$metadata <- append(private$metadata, meta) |
206 | 16x |
invisible(self) |
207 |
}, |
|
208 |
#' @description |
|
209 |
#' Reinitializes a `Reporter` instance by copying the report cards and metadata from another `Reporter`. |
|
210 |
#' @param reporter (`Reporter`) instance to copy from. |
|
211 |
#' @return invisibly self |
|
212 |
#' @examples |
|
213 |
#' reporter <- Reporter$new() |
|
214 |
#' reporter$from_reporter(reporter) |
|
215 |
from_reporter = function(reporter) { |
|
216 | 8x |
checkmate::assert_class(reporter, "Reporter") |
217 | 8x |
self$reset() |
218 | 8x |
self$append_cards(reporter$get_cards()) |
219 | 8x |
self$append_metadata(reporter$get_metadata()) |
220 | 8x |
invisible(self) |
221 |
}, |
|
222 |
#' @description Convert a `Reporter` to a list and transfer any associated files to specified directory. |
|
223 |
#' @param output_dir (`character(1)`) a path to the directory where files will be copied. |
|
224 |
#' @return `named list` representing the `Reporter` instance, including version information, |
|
225 |
#' metadata, and report cards. |
|
226 |
#' @examples |
|
227 |
#' reporter <- Reporter$new() |
|
228 |
#' tmp_dir <- file.path(tempdir(), "testdir") |
|
229 |
#' dir.create(tmp_dir) |
|
230 |
#' reporter$to_list(tmp_dir) |
|
231 |
to_list = function(output_dir) { |
|
232 | 14x |
checkmate::assert_directory_exists(output_dir) |
233 | 12x |
rlist <- list(name = "teal Reporter", version = "1", id = self$get_id(), cards = list()) |
234 | 12x |
rlist[["metadata"]] <- self$get_metadata() |
235 | 12x |
for (card in self$get_cards()) { |
236 |
# we want to have list names being a class names to indicate the class for $from_list |
|
237 | 10x |
card_class <- class(card)[1] |
238 | 10x |
u_card <- list() |
239 | 10x |
u_card[[card_class]] <- card$to_list(output_dir) |
240 | 10x |
rlist$cards <- c(rlist$cards, u_card) |
241 |
} |
|
242 | 12x |
rlist |
243 |
}, |
|
244 |
#' @description Reinitializes a `Reporter` from a list representation and associated files in a specified directory. |
|
245 |
#' @param rlist (`named list`) representing a `Reporter` instance. |
|
246 |
#' @param output_dir (`character(1)`) a path to the directory from which files will be copied. |
|
247 |
#' @return `self`, invisibly. |
|
248 |
#' @note if Report has an id when converting to JSON then It will be compared to the currently available one. |
|
249 |
#' @examples |
|
250 |
#' reporter <- Reporter$new() |
|
251 |
#' tmp_dir <- file.path(tempdir(), "testdir") |
|
252 |
#' unlink(tmp_dir, recursive = TRUE) |
|
253 |
#' dir.create(tmp_dir) |
|
254 |
#' reporter$from_list(reporter$to_list(tmp_dir), tmp_dir) |
|
255 |
from_list = function(rlist, output_dir) { |
|
256 | 6x |
id <- self$get_id() |
257 | 6x |
checkmate::assert_list(rlist) |
258 | 6x |
checkmate::assert_directory_exists(output_dir) |
259 | 6x |
stopifnot("Report JSON has to have name slot equal to teal Reporter" = rlist$name == "teal Reporter") |
260 | 6x |
stopifnot("Loaded Report id has to match the current instance one" = rlist$id == id) |
261 | 5x |
if (rlist$version %in% c("1")) { |
262 | 5x |
new_cards <- list() |
263 | 5x |
cards_names <- names(rlist$cards) |
264 | 5x |
cards_names <- gsub("[.][0-9]*$", "", cards_names) |
265 | 5x |
for (iter_c in seq_along(rlist$cards)) { |
266 | 5x |
card_class <- cards_names[iter_c] |
267 | 5x |
card <- rlist$cards[[iter_c]] |
268 | 5x |
new_card <- eval(str2lang(card_class))$new() |
269 | 5x |
new_card$from_list(card, output_dir) |
270 | 5x |
new_cards <- c(new_cards, new_card) |
271 |
} |
|
272 |
} else { |
|
273 | ! |
stop( |
274 | ! |
sprintf( |
275 | ! |
"The provided %s reporter version is not supported.", |
276 | ! |
rlist$version |
277 |
) |
|
278 |
) |
|
279 |
} |
|
280 | 5x |
self$reset() |
281 | 5x |
self$set_id(rlist$id) |
282 | 5x |
self$append_cards(new_cards) |
283 | 5x |
self$append_metadata(rlist$metadata) |
284 | 5x |
invisible(self) |
285 |
}, |
|
286 |
#' @description Serializes the `Reporter` to a `JSON` file and copies any associated files to a specified directory. |
|
287 |
#' @param output_dir (`character(1)`) a path to the directory where files will be copied, `JSON` and statics. |
|
288 |
#' @return `output_dir` argument. |
|
289 |
#' @examples |
|
290 |
#' reporter <- Reporter$new() |
|
291 |
#' tmp_dir <- file.path(tempdir(), "jsondir") |
|
292 |
#' dir.create(tmp_dir) |
|
293 |
#' reporter$to_jsondir(tmp_dir) |
|
294 |
to_jsondir = function(output_dir) { |
|
295 | 11x |
checkmate::assert_directory_exists(output_dir) |
296 | 9x |
json <- self$to_list(output_dir) |
297 | 9x |
cat( |
298 | 9x |
jsonlite::toJSON(json, auto_unbox = TRUE, force = TRUE), |
299 | 9x |
file = file.path(output_dir, "Report.json") |
300 |
) |
|
301 | 9x |
output_dir |
302 |
}, |
|
303 |
#' @description Reinitializes a `Reporter` from a `JSON ` file and files in a specified directory. |
|
304 |
#' @param output_dir (`character(1)`) a path to the directory with files, `JSON` and statics. |
|
305 |
#' @return `self`, invisibly. |
|
306 |
#' @note if Report has an id when converting to JSON then It will be compared to the currently available one. |
|
307 |
#' @examples |
|
308 |
#' reporter <- Reporter$new() |
|
309 |
#' tmp_dir <- file.path(tempdir(), "jsondir") |
|
310 |
#' dir.create(tmp_dir) |
|
311 |
#' unlink(list.files(tmp_dir, recursive = TRUE)) |
|
312 |
#' reporter$to_jsondir(tmp_dir) |
|
313 |
#' reporter$from_jsondir(tmp_dir) |
|
314 |
from_jsondir = function(output_dir) { |
|
315 | 4x |
checkmate::assert_directory_exists(output_dir) |
316 | 4x |
dir_files <- list.files(output_dir) |
317 | 4x |
stopifnot("There has to be at least one file in the loaded directory" = length(dir_files) > 0) |
318 | 4x |
stopifnot("Report.json file has to be in the loaded directory" = "Report.json" %in% basename(dir_files)) |
319 | 4x |
json <- jsonlite::read_json(file.path(output_dir, "Report.json")) |
320 | 4x |
self$reset() |
321 | 4x |
self$from_list(json, output_dir) |
322 | 3x |
invisible(self) |
323 |
}, |
|
324 |
#' @description Set the `Reporter` id |
|
325 |
#' Optionally add id to a `Reporter` which will be compared when it is rebuilt from a list. |
|
326 |
#' The id is added to the downloaded file name. |
|
327 |
#' @param id (`character(1)`) a Report id. |
|
328 |
#' @return `self`, invisibly. |
|
329 |
set_id = function(id) { |
|
330 | 10x |
private$id <- id |
331 | 10x |
invisible(self) |
332 |
}, |
|
333 |
#' @description Get the `Reporter` id |
|
334 |
#' @return `character(1)` the `Reporter` id. |
|
335 |
get_id = function() { |
|
336 | 23x |
private$id |
337 |
} |
|
338 |
), |
|
339 |
private = list( |
|
340 |
id = "", |
|
341 |
cards = list(), |
|
342 |
metadata = list(), |
|
343 |
reactive_add_card = NULL, |
|
344 |
# @description The copy constructor. |
|
345 |
# |
|
346 |
# @param name the name of the field |
|
347 |
# @param value the value of the field |
|
348 |
# @return the new value of the field |
|
349 |
# |
|
350 |
deep_clone = function(name, value) { |
|
351 | 23x |
if (name == "cards") { |
352 | 1x |
lapply(value, function(card) card$clone(deep = TRUE)) |
353 |
} else { |
|
354 | 22x |
value |
355 |
} |
|
356 |
} |
|
357 |
), |
|
358 |
lock_objects = TRUE, |
|
359 |
lock_class = TRUE |
|
360 |
) |
1 |
#' @title `ContentBlock`: A building block for report content |
|
2 |
#' @docType class |
|
3 |
#' @description This class represents a basic content unit in a report, |
|
4 |
#' such as text, images, or other multimedia elements. |
|
5 |
#' It serves as a foundation for constructing complex report structures. |
|
6 |
#' |
|
7 |
#' @keywords internal |
|
8 |
ContentBlock <- R6::R6Class( # nolint: object_name_linter. |
|
9 |
classname = "ContentBlock", |
|
10 |
public = list( |
|
11 |
#' @description Initialize a `ContentBlock` object. |
|
12 |
#' |
|
13 |
#' @details Returns a `ContentBlock` object with no content and the default style. |
|
14 |
#' |
|
15 |
#' @return Object of class `ContentBlock`, invisibly. |
|
16 |
#' @examples |
|
17 |
#' ContentBlock <- getFromNamespace("ContentBlock", "teal.reporter") |
|
18 |
#' ContentBlock$new() |
|
19 |
#' |
|
20 |
initialize = function() { |
|
21 | 17x |
private$content <- character(0) |
22 | 17x |
invisible(self) |
23 |
}, |
|
24 |
#' @description Sets content of this `ContentBlock`. |
|
25 |
#' |
|
26 |
#' @param content (`character(0)` or `character(1)`) string or file path assigned to this `ContentBlock` |
|
27 |
#' |
|
28 |
#' @return `self`, invisibly. |
|
29 |
#' @examples |
|
30 |
#' ContentBlock <- getFromNamespace("ContentBlock", "teal.reporter") |
|
31 |
#' block <- ContentBlock$new() |
|
32 |
#' block$set_content("Base64 encoded picture") |
|
33 |
#' |
|
34 |
set_content = function(content) { |
|
35 | 305x |
checkmate::assert_character(content, min.len = 0, max.len = 1) |
36 | 302x |
private$content <- content |
37 | 302x |
invisible(self) |
38 |
}, |
|
39 |
#' @description Retrieves the content assigned to this block. |
|
40 |
#' |
|
41 |
#' @return `character` string or file path assigned to this `ContentBlock`. |
|
42 |
#' @examples |
|
43 |
#' ContentBlock <- getFromNamespace("ContentBlock", "teal.reporter") |
|
44 |
#' block <- ContentBlock$new() |
|
45 |
#' block$get_content() |
|
46 |
#' |
|
47 |
get_content = function() { |
|
48 | 265x |
private$content |
49 |
}, |
|
50 |
#' @description Create the `ContentBlock` from a list. |
|
51 |
#' |
|
52 |
#' @param x (`named list`) with two fields `text` and `style`. |
|
53 |
#' Use the `get_available_styles` method to get all possible styles. |
|
54 |
#' |
|
55 |
#' @return `self`, invisibly. |
|
56 |
from_list = function(x) { |
|
57 | ! |
invisible(self) |
58 |
}, |
|
59 |
#' @description Convert the `ContentBlock` to a list. |
|
60 |
#' |
|
61 |
#' @return `named list` with a text and style. |
|
62 |
to_list = function() { |
|
63 | ! |
list() |
64 |
} |
|
65 |
), |
|
66 |
private = list( |
|
67 |
content = character(0), |
|
68 |
# @description The copy constructor. |
|
69 |
# |
|
70 |
# @param name (`character(1)`) the name of the field |
|
71 |
# @param value the value assigned to the field |
|
72 |
# |
|
73 |
# @return the value of the copied field |
|
74 |
deep_clone = function(name, value) { |
|
75 | 168x |
if (name == "content" && checkmate::test_file_exists(value)) { |
76 | 7x |
extension <- "" |
77 | 7x |
split <- strsplit(basename(value), split = "\\.") |
78 |
# The below ensures no extension is found for files such as this: .gitignore but is found for files like |
|
79 |
# .gitignore.txt |
|
80 | 7x |
if (length(split[[1]]) > 1 && split[[1]][length(split[[1]]) - 1] != "") { |
81 | 5x |
extension <- split[[1]][length(split[[1]])] |
82 | 5x |
extension <- paste0(".", extension) |
83 |
} |
|
84 | 7x |
copied_file <- tempfile(fileext = extension) |
85 | 7x |
file.copy(value, copied_file, copy.date = TRUE, copy.mode = TRUE) |
86 | 7x |
copied_file |
87 |
} else { |
|
88 | 161x |
value |
89 |
} |
|
90 |
} |
|
91 |
), |
|
92 |
lock_objects = TRUE, |
|
93 |
lock_class = TRUE |
|
94 |
) |
1 |
#' Simple reporter module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' Module provides compact UI and server functions for managing a report in a `shiny` app. |
|
6 |
#' This module combines functionalities for [adding cards to a report][add_card_button], |
|
7 |
#' [downloading the report][download_report_button], and [resetting report content][reset_report_button]. |
|
8 |
#' |
|
9 |
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
10 |
#' |
|
11 |
#' @details `r global_knitr_details()` |
|
12 |
#' |
|
13 |
#' @name simple_reporter |
|
14 |
#' |
|
15 |
#' @param id (`character(1)`) `shiny` module instance id. |
|
16 |
#' @param reporter (`Reporter`) instance. |
|
17 |
#' @param card_fun (`function`) which returns a [`ReportCard`] instance, |
|
18 |
#' the function has a `card` argument and an optional `comment` argument. |
|
19 |
#' @param global_knitr (`list`) a global `knitr` parameters for customizing the rendering process. |
|
20 |
#' @inheritParams reporter_download_inputs |
|
21 |
#' |
|
22 |
#' @return `NULL`. |
|
23 |
#' |
|
24 |
#' @examples |
|
25 |
#' if (interactive()) { |
|
26 |
#' library(shiny) |
|
27 |
#' |
|
28 |
#' shinyApp( |
|
29 |
#' ui = fluidPage(simple_reporter_ui("simple")), |
|
30 |
#' server = function(input, output, session) { |
|
31 |
#' simple_reporter_srv("simple", Reporter$new(), function(card) card) |
|
32 |
#' } |
|
33 |
#' ) |
|
34 |
#' } |
|
35 |
NULL |
|
36 | ||
37 |
#' @rdname simple_reporter |
|
38 |
#' @export |
|
39 |
simple_reporter_ui <- function(id) { |
|
40 | 1x |
ns <- shiny::NS(id) |
41 | 1x |
shiny::tagList( |
42 | 1x |
shiny::singleton( |
43 | 1x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
44 |
), |
|
45 | 1x |
shiny::tags$div( |
46 | 1x |
class = "block mb-4 p-1", |
47 | 1x |
shiny::tags$label(class = "text-primary block -ml-1", shiny::tags$strong("Reporter")), |
48 | 1x |
shiny::tags$div( |
49 | 1x |
class = "simple_reporter_container", |
50 | 1x |
add_card_button_ui(ns("add_report_card_simple")), |
51 | 1x |
download_report_button_ui(ns("download_button_simple")), |
52 | 1x |
report_load_ui(ns("archive_load_simple")), |
53 | 1x |
reset_report_button_ui(ns("reset_button_simple")) |
54 |
) |
|
55 |
) |
|
56 |
) |
|
57 |
} |
|
58 | ||
59 |
#' @rdname simple_reporter |
|
60 |
#' @export |
|
61 |
simple_reporter_srv <- function( |
|
62 |
id, |
|
63 |
reporter, |
|
64 |
card_fun, |
|
65 |
global_knitr = getOption("teal.reporter.global_knitr"), |
|
66 |
rmd_output = c( |
|
67 |
"html" = "html_document", "pdf" = "pdf_document", |
|
68 |
"powerpoint" = "powerpoint_presentation", "word" = "word_document" |
|
69 |
), |
|
70 |
rmd_yaml_args = list( |
|
71 |
author = "NEST", title = "Report", |
|
72 |
date = as.character(Sys.Date()), output = "html_document", |
|
73 |
toc = FALSE |
|
74 |
)) { |
|
75 | 3x |
shiny::moduleServer( |
76 | 3x |
id, |
77 | 3x |
function(input, output, session) { |
78 | 3x |
add_card_button_srv("add_report_card_simple", reporter = reporter, card_fun = card_fun) |
79 | 3x |
download_report_button_srv( |
80 | 3x |
"download_button_simple", |
81 | 3x |
reporter = reporter, |
82 | 3x |
global_knitr = global_knitr, |
83 | 3x |
rmd_output = rmd_output, |
84 | 3x |
rmd_yaml_args = rmd_yaml_args |
85 |
) |
|
86 | 3x |
report_load_srv("archive_load_simple", reporter = reporter) |
87 | 3x |
reset_report_button_srv("reset_button_simple", reporter = reporter) |
88 |
} |
|
89 |
) |
|
90 |
} |
1 |
#' @title `RcodeBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' Specialized `ContentBlock` designed to embed `R` code in reports. |
|
5 |
#' |
|
6 |
#' @keywords internal |
|
7 |
RcodeBlock <- R6::R6Class( # nolint: object_name_linter. |
|
8 |
classname = "RcodeBlock", |
|
9 |
inherit = ContentBlock, |
|
10 |
public = list( |
|
11 |
#' @description Initialize a `RcodeBlock` object. |
|
12 |
#' |
|
13 |
#' @details Returns a `RcodeBlock` object with no content and no parameters. |
|
14 |
#' |
|
15 |
#' @param content (`character(1)` or `character(0)`) a string assigned to this `RcodeBlock` |
|
16 |
#' @param ... any `rmarkdown` `R` chunk parameter and it value. |
|
17 |
#' |
|
18 |
#' @return Object of class `RcodeBlock`, invisibly. |
|
19 |
#' @examples |
|
20 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
21 |
#' block <- RcodeBlock$new() |
|
22 |
#' |
|
23 |
initialize = function(content = character(0), ...) { |
|
24 | 75x |
super$set_content(content) |
25 | 75x |
self$set_params(list(...)) |
26 | 75x |
invisible(self) |
27 |
}, |
|
28 |
#' @description Sets the parameters of this `RcodeBlock`. |
|
29 |
#' |
|
30 |
#' @details Configures `rmarkdown` chunk parameters for the `R` code block, |
|
31 |
#' influencing its rendering and execution behavior. |
|
32 |
#' |
|
33 |
#' @param params (`list`) any `rmarkdown` R chunk parameter and its value. |
|
34 |
#' |
|
35 |
#' @return `self`, invisibly. |
|
36 |
#' @examples |
|
37 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
38 |
#' block <- RcodeBlock$new() |
|
39 |
#' block$set_params(list(echo = TRUE)) |
|
40 |
#' |
|
41 |
set_params = function(params) { |
|
42 | 133x |
checkmate::assert_list(params, names = "named") |
43 | 133x |
checkmate::assert_subset(names(params), self$get_available_params()) |
44 | 133x |
private$params <- params |
45 | 133x |
invisible(self) |
46 |
}, |
|
47 |
#' @description Get the parameters of this `RcodeBlock`. |
|
48 |
#' |
|
49 |
#' @return `character` the parameters of this `RcodeBlock`. |
|
50 |
#' @examples |
|
51 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
52 |
#' block <- RcodeBlock$new() |
|
53 |
#' block$get_params() |
|
54 |
#' |
|
55 |
get_params = function() { |
|
56 | 3x |
private$params |
57 |
}, |
|
58 |
#' @description Get available array of parameters available to this `RcodeBlock`. |
|
59 |
#' |
|
60 |
#' @return A `character` array of parameters. |
|
61 |
#' @examples |
|
62 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
63 |
#' block <- RcodeBlock$new() |
|
64 |
#' block$get_available_params() |
|
65 |
#' |
|
66 |
get_available_params = function() { |
|
67 | 5x |
names(knitr::opts_chunk$get()) |
68 |
}, |
|
69 |
#' @description Create the `RcodeBlock` from a list. |
|
70 |
#' |
|
71 |
#' @param x (`named list`) with two fields `text` and `params`. |
|
72 |
#' Use the `get_available_params` method to get all possible parameters. |
|
73 |
#' |
|
74 |
#' @return `self`, invisibly. |
|
75 |
#' @examples |
|
76 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
77 |
#' block <- RcodeBlock$new() |
|
78 |
#' block$from_list(list(text = "sth", params = list())) |
|
79 |
#' |
|
80 |
from_list = function(x) { |
|
81 | 3x |
checkmate::assert_list(x) |
82 | 3x |
checkmate::assert_names(names(x), must.include = c("text", "params")) |
83 | 3x |
self$set_content(x$text) |
84 | 3x |
self$set_params(x$params) |
85 | 3x |
invisible(self) |
86 |
}, |
|
87 |
#' @description Convert the `RcodeBlock` to a list. |
|
88 |
#' |
|
89 |
#' @return `named list` with a text and `params`. |
|
90 |
#' @examples |
|
91 |
#' RcodeBlock <- getFromNamespace("RcodeBlock", "teal.reporter") |
|
92 |
#' block <- RcodeBlock$new() |
|
93 |
#' block$to_list() |
|
94 |
#' |
|
95 |
to_list = function() { |
|
96 | 3x |
list(text = self$get_content(), params = self$get_params()) |
97 |
} |
|
98 |
), |
|
99 |
private = list( |
|
100 |
params = list() |
|
101 |
), |
|
102 |
lock_objects = TRUE, |
|
103 |
lock_class = TRUE |
|
104 |
) |
1 |
#' User Interface to Load `Reporter` |
|
2 |
#' @description `r lifecycle::badge("experimental")` |
|
3 |
#' Button to upload `ReporterCard`(s) to the `Reporter`. |
|
4 |
#' |
|
5 |
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
6 |
#' @param id `character(1)` this `shiny` module's id. |
|
7 |
#' @return `shiny::tagList` |
|
8 |
#' @export |
|
9 |
report_load_ui <- function(id) { |
|
10 | 2x |
ns <- shiny::NS(id) |
11 | ||
12 | 2x |
shiny::tagList( |
13 | 2x |
shiny::singleton( |
14 | 2x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
15 |
), |
|
16 | 2x |
shiny::tags$button( |
17 | 2x |
id = ns("reporter_load"), |
18 | 2x |
type = "button", |
19 | 2x |
class = "simple_report_button btn btn-primary action-button", |
20 | 2x |
title = "Load", |
21 | 2x |
NULL, |
22 | 2x |
shiny::tags$span( |
23 | 2x |
shiny::icon("upload") |
24 |
) |
|
25 |
) |
|
26 |
) |
|
27 |
} |
|
28 | ||
29 |
#' Server to Load `Reporter` |
|
30 |
#' @description `r lifecycle::badge("experimental")` |
|
31 |
#' Server to load `ReporterCard`(s) to the `Reporter` |
|
32 |
#' |
|
33 |
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
34 |
#' |
|
35 |
#' @param id `character(1)` this `shiny` module's id. |
|
36 |
#' @param reporter [`Reporter`] instance. |
|
37 |
#' |
|
38 |
#' @return `shiny::moduleServer` |
|
39 |
#' @export |
|
40 |
report_load_srv <- function(id, reporter) { |
|
41 | 5x |
checkmate::assert_class(reporter, "Reporter") |
42 | ||
43 | 5x |
shiny::moduleServer( |
44 | 5x |
id, |
45 | 5x |
function(input, output, session) { |
46 | 5x |
shiny::setBookmarkExclude(c("reporter_load_main", "reporter_load")) |
47 | 5x |
ns <- session$ns |
48 | ||
49 | 5x |
archiver_modal <- function() { |
50 | 2x |
nr_cards <- length(reporter$get_cards()) |
51 | 2x |
shiny::modalDialog( |
52 | 2x |
easyClose = TRUE, |
53 | 2x |
shiny::tags$h3("Load the Report"), |
54 | 2x |
shiny::tags$hr(), |
55 | 2x |
shiny::fileInput(ns("archiver_zip"), "Choose saved Reporter file to Load (a zip file)", |
56 | 2x |
multiple = FALSE, |
57 | 2x |
accept = c(".zip") |
58 |
), |
|
59 | 2x |
footer = shiny::div( |
60 | 2x |
shiny::tags$button( |
61 | 2x |
type = "button", |
62 | 2x |
class = "btn btn-danger", |
63 | 2x |
`data-dismiss` = "modal", |
64 | 2x |
`data-bs-dismiss` = "modal", |
65 | 2x |
NULL, |
66 | 2x |
"Cancel" |
67 |
), |
|
68 | 2x |
shiny::tags$button( |
69 | 2x |
id = ns("reporter_load_main"), |
70 | 2x |
type = "button", |
71 | 2x |
class = "btn btn-primary action-button", |
72 | 2x |
NULL, |
73 | 2x |
"Load" |
74 |
) |
|
75 |
) |
|
76 |
) |
|
77 |
} |
|
78 | ||
79 | 5x |
shiny::observeEvent(input$reporter_load, { |
80 | 2x |
shiny::showModal(archiver_modal()) |
81 |
}) |
|
82 | ||
83 | 5x |
shiny::observeEvent(input$reporter_load_main, { |
84 | 2x |
load_json_report(reporter, input$archiver_zip[["datapath"]], input$archiver_zip[["name"]]) |
85 | 2x |
shiny::removeModal() |
86 |
}) |
|
87 |
} |
|
88 |
) |
|
89 |
} |
|
90 | ||
91 |
#' @keywords internal |
|
92 |
load_json_report <- function(reporter, zip_path, filename) { |
|
93 | 2x |
tmp_dir <- tempdir() |
94 | 2x |
output_dir <- file.path(tmp_dir, sprintf("report_load_%s", gsub("[.]", "", format(Sys.time(), "%Y%m%d%H%M%OS4")))) |
95 | 2x |
dir.create(path = output_dir) |
96 | 2x |
if (!is.null(zip_path) && grepl("report_", filename)) { |
97 | 2x |
tryCatch( |
98 | 2x |
expr = zip::unzip(zip_path, exdir = output_dir, junkpaths = TRUE), |
99 | 2x |
warning = function(cond) { |
100 | ! |
print(cond) |
101 | ! |
shiny::showNotification( |
102 | ! |
ui = "Unzipping folder warning!", |
103 | ! |
action = "Please contact app developer", |
104 | ! |
type = "warning" |
105 |
) |
|
106 |
}, |
|
107 | 2x |
error = function(cond) { |
108 | ! |
print(cond) |
109 | ! |
shiny::showNotification( |
110 | ! |
ui = "Unzipping folder error!", |
111 | ! |
action = "Please contact app developer", |
112 | ! |
type = "error" |
113 |
) |
|
114 |
} |
|
115 |
) |
|
116 | 2x |
tryCatch( |
117 | 2x |
reporter$from_jsondir(output_dir), |
118 | 2x |
warning = function(cond) { |
119 | ! |
print(cond) |
120 | ! |
shiny::showNotification( |
121 | ! |
ui = "Loading reporter warning!", |
122 | ! |
action = "Please contact app developer", |
123 | ! |
type = "warning" |
124 |
) |
|
125 |
}, |
|
126 | 2x |
error = function(cond) { |
127 | 1x |
print(cond) |
128 | 1x |
shiny::showNotification( |
129 | 1x |
ui = "Loading reporter error!", |
130 | 1x |
action = "Please contact app developer", |
131 | 1x |
type = "error" |
132 |
) |
|
133 |
} |
|
134 |
) |
|
135 |
} else { |
|
136 | ! |
shiny::showNotification("Failed to load the Reporter file.", type = "error") |
137 |
} |
|
138 |
} |
1 |
#' Reset report button module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' Provides a button that triggers resetting the report content. |
|
6 |
#' |
|
7 |
#' For more information, refer to the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
8 |
#' |
|
9 |
#' @name reset_report_button |
|
10 |
#' |
|
11 |
#' @param id (`character(1)`) `shiny` module instance id. |
|
12 |
#' @param label (`character(1)`) label before the icon. |
|
13 |
#' By default `NULL`. |
|
14 |
#' @param reporter (`Reporter`) instance. |
|
15 |
#' @return `NULL`. |
|
16 |
NULL |
|
17 | ||
18 |
#' @rdname reset_report_button |
|
19 |
#' @export |
|
20 |
reset_report_button_ui <- function(id, label = NULL) { |
|
21 | 8x |
checkmate::assert_string(label, null.ok = TRUE) |
22 | ||
23 | 8x |
ns <- shiny::NS(id) |
24 | 8x |
shiny::tagList( |
25 | 8x |
shiny::singleton( |
26 | 8x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
27 |
), |
|
28 | 8x |
shiny::tags$button( |
29 | 8x |
id = ns("reset_reporter"), |
30 | 8x |
type = "button", |
31 | 8x |
class = "simple_report_button btn btn-warning action-button", |
32 | 8x |
title = "Reset", |
33 | 8x |
`data-val` = shiny::restoreInput(id = ns("reset_reporter"), default = NULL), |
34 | 8x |
NULL, |
35 | 8x |
shiny::tags$span( |
36 | 8x |
if (!is.null(label)) label, |
37 | 8x |
shiny::icon("xmark") |
38 |
) |
|
39 |
) |
|
40 |
) |
|
41 |
} |
|
42 | ||
43 |
#' @rdname reset_report_button |
|
44 |
#' @export |
|
45 |
reset_report_button_srv <- function(id, reporter) { |
|
46 | 12x |
checkmate::assert_class(reporter, "Reporter") |
47 | ||
48 | 12x |
shiny::moduleServer(id, function(input, output, session) { |
49 | 12x |
shiny::setBookmarkExclude(c("reset_reporter")) |
50 | ||
51 | 12x |
ns <- session$ns |
52 | 12x |
nr_cards <- length(reporter$get_cards()) |
53 | ||
54 | ||
55 | 12x |
shiny::observeEvent(input$reset_reporter, { |
56 | 1x |
shiny::showModal( |
57 | 1x |
shiny::modalDialog( |
58 | 1x |
shiny::tags$h3("Reset the Report"), |
59 | 1x |
shiny::tags$hr(), |
60 | 1x |
shiny::tags$strong( |
61 | 1x |
shiny::tags$p( |
62 | 1x |
"Are you sure you want to reset the report? (This will remove ALL previously added cards)." |
63 |
) |
|
64 |
), |
|
65 | 1x |
footer = shiny::tagList( |
66 | 1x |
shiny::tags$button( |
67 | 1x |
type = "button", |
68 | 1x |
class = "btn btn-secondary", |
69 | 1x |
`data-dismiss` = "modal", |
70 | 1x |
`data-bs-dismiss` = "modal", |
71 | 1x |
NULL, |
72 | 1x |
"Cancel" |
73 |
), |
|
74 | 1x |
shiny::actionButton(ns("reset_reporter_ok"), "Reset", class = "btn-danger") |
75 |
) |
|
76 |
) |
|
77 |
) |
|
78 |
}) |
|
79 | ||
80 | 12x |
shiny::observeEvent(input$reset_reporter_ok, { |
81 | 1x |
reporter$reset() |
82 | 1x |
shiny::removeModal() |
83 |
}) |
|
84 |
}) |
|
85 |
} |
1 |
.onLoad <- function(libname, pkgname) { |
|
2 | ! |
op <- options() |
3 | ! |
default_global_knitr <- list(teal.reporter.global_knitr = list( |
4 | ! |
echo = TRUE, |
5 | ! |
tidy.opts = list(width.cutoff = 60), |
6 | ! |
tidy = requireNamespace("formatR", quietly = TRUE) |
7 |
)) |
|
8 | ||
9 | ! |
if (!("teal.reporter.global_knitr" %in% names(op))) { |
10 | ! |
options(default_global_knitr) |
11 |
} |
|
12 | ||
13 | ! |
invisible() |
14 |
} |
|
15 | ||
16 |
.onAttach <- function(libname, pkgname) { |
|
17 | 2x |
packageStartupMessage( |
18 | 2x |
if (!requireNamespace("formatR", quietly = TRUE)) { |
19 | ! |
"For better code formatting, consider installing the formatR package." |
20 |
} |
|
21 |
) |
|
22 |
} |
1 |
#' Add card button module |
|
2 |
#' |
|
3 |
#' @description `r lifecycle::badge("experimental")` |
|
4 |
#' |
|
5 |
#' Provides a button to add views/cards to a report. |
|
6 |
#' |
|
7 |
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. |
|
8 |
#' |
|
9 |
#' @details |
|
10 |
#' The `card_fun` function is designed to create a new `ReportCard` instance and optionally customize it: |
|
11 |
#' - The `card` parameter allows for specifying a custom or default `ReportCard` instance. |
|
12 |
#' - Use the `comment` parameter to add a comment to the card via `card$append_text()` - if `card_fun` does not |
|
13 |
#' have the `comment` parameter, then `comment` from `Add Card UI` module will be added at the end of the content of the |
|
14 |
#' card. |
|
15 |
#' - The `label` parameter enables customization of the card's name and its content through `card$append_text()`- |
|
16 |
#' if `card_fun` does not have the `label` parameter, then card name will be set to the name passed in |
|
17 |
#' `Add Card UI` module, but no text will be added to the content of the `card`. |
|
18 |
#' |
|
19 |
#' This module supports using a subclass of [`ReportCard`] for added flexibility. |
|
20 |
#' A subclass instance should be passed as the default value of |
|
21 |
#' the `card` argument in the `card_fun` function. |
|
22 |
#' See below: |
|
23 |
#' ```{r} |
|
24 |
#' CustomReportCard <- R6::R6Class( |
|
25 |
#' classname = "CustomReportCard", |
|
26 |
#' inherit = teal.reporter::ReportCard |
|
27 |
#' ) |
|
28 |
#' |
|
29 |
#' custom_function <- function(card = CustomReportCard$new()) { |
|
30 |
#' card |
|
31 |
#' } |
|
32 |
#' ``` |
|
33 |
#' @name add_card_button |
|
34 |
#' |
|
35 |
#' @param id (`character(1)`) this `shiny` module's id. |
|
36 |
#' @param reporter (`Reporter`) instance. |
|
37 |
#' @param card_fun (`function`) which returns a [`ReportCard`] instance. See `Details`. |
|
38 |
#' |
|
39 |
#' @return `NULL`. |
|
40 |
NULL |
|
41 | ||
42 |
#' @rdname add_card_button |
|
43 |
#' @export |
|
44 |
add_card_button_ui <- function(id) { |
|
45 | 2x |
ns <- shiny::NS(id) |
46 | ||
47 |
# Buttons with custom css and |
|
48 |
# js code to disable the add card button when clicked to prevent multi-clicks |
|
49 | 2x |
shiny::tagList( |
50 | 2x |
shiny::singleton( |
51 | 2x |
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter"))) |
52 |
), |
|
53 | 2x |
shiny::singleton( |
54 | 2x |
shiny::tags$head( |
55 | 2x |
shiny::tags$script( |
56 | 2x |
shiny::HTML( |
57 | 2x |
sprintf( |
58 |
' |
|
59 | 2x |
$(document).ready(function(event) { |
60 | 2x |
$("body").on("click", "#%s", function() { |
61 | 2x |
$(this).addClass("disabled"); |
62 |
}) |
|
63 |
})', |
|
64 | 2x |
ns("add_card_ok") |
65 |
) |
|
66 |
) |
|
67 |
) |
|
68 |
) |
|
69 |
), |
|
70 | 2x |
shiny::tags$button( |
71 | 2x |
id = ns("add_report_card_button"), |
72 | 2x |
type = "button", |
73 | 2x |
class = "simple_report_button btn btn-primary action-button", |
74 | 2x |
title = "Add Card", |
75 | 2x |
`data-val` = shiny::restoreInput(id = ns("add_report_card_button"), default = NULL), |
76 | 2x |
NULL, |
77 | 2x |
shiny::tags$span( |
78 | 2x |
shiny::icon("plus") |
79 |
) |
|
80 |
) |
|
81 |
) |
|
82 |
} |
|
83 | ||
84 |
#' @rdname add_card_button |
|
85 |
#' @export |
|
86 |
add_card_button_srv <- function(id, reporter, card_fun) { |
|
87 | 13x |
checkmate::assert_function(card_fun) |
88 | 13x |
checkmate::assert_class(reporter, "Reporter") |
89 | 13x |
checkmate::assert_subset(names(formals(card_fun)), c("card", "comment", "label"), empty.ok = TRUE) |
90 | ||
91 | 13x |
shiny::moduleServer(id, function(input, output, session) { |
92 | 13x |
shiny::setBookmarkExclude(c( |
93 | 13x |
"add_report_card_button", "download_button", "reset_reporter", |
94 | 13x |
"add_card_ok", "download_data", "reset_reporter_ok", |
95 | 13x |
"label", "comment" |
96 |
)) |
|
97 | ||
98 | 13x |
ns <- session$ns |
99 | ||
100 | 13x |
add_modal <- function() { |
101 | 11x |
shiny::modalDialog( |
102 | 11x |
easyClose = TRUE, |
103 | 11x |
shiny::tags$h3("Add a Card to the Report"), |
104 | 11x |
shiny::tags$hr(), |
105 | 11x |
shiny::textInput( |
106 | 11x |
ns("label"), |
107 | 11x |
"Card Name", |
108 | 11x |
value = "", |
109 | 11x |
placeholder = "Add the card title here", |
110 | 11x |
width = "100%" |
111 |
), |
|
112 | 11x |
shiny::textAreaInput( |
113 | 11x |
ns("comment"), |
114 | 11x |
"Comment", |
115 | 11x |
value = "", |
116 | 11x |
placeholder = "Add a comment here...", |
117 | 11x |
width = "100%" |
118 |
), |
|
119 | 11x |
shiny::tags$script( |
120 | 11x |
shiny::HTML( |
121 | 11x |
sprintf( |
122 |
" |
|
123 | 11x |
$('#shiny-modal').on('shown.bs.modal', () => { |
124 | 11x |
$('#%s').focus() |
125 |
}) |
|
126 |
", |
|
127 | 11x |
ns("label") |
128 |
) |
|
129 |
) |
|
130 |
), |
|
131 | 11x |
footer = shiny::div( |
132 | 11x |
shiny::tags$button( |
133 | 11x |
type = "button", |
134 | 11x |
class = "btn btn-secondary", |
135 | 11x |
`data-dismiss` = "modal", |
136 | 11x |
`data-bs-dismiss` = "modal", |
137 | 11x |
NULL, |
138 | 11x |
"Cancel" |
139 |
), |
|
140 | 11x |
shiny::tags$button( |
141 | 11x |
id = ns("add_card_ok"), |
142 | 11x |
type = "button", |
143 | 11x |
class = "btn btn-primary action-button", |
144 | 11x |
`data-val` = shiny::restoreInput(id = ns("add_card_ok"), default = NULL), |
145 | 11x |
NULL, |
146 | 11x |
"Add Card" |
147 |
) |
|
148 |
) |
|
149 |
) |
|
150 |
} |
|
151 | ||
152 | 13x |
shiny::observeEvent(input$add_report_card_button, { |
153 | 11x |
shiny::showModal(add_modal()) |
154 |
}) |
|
155 | ||
156 |
# the add card button is disabled when clicked to prevent multi-clicks |
|
157 |
# please check the ui part for more information |
|
158 | 13x |
shiny::observeEvent(input$add_card_ok, { |
159 | 11x |
card_fun_args_nams <- names(formals(card_fun)) |
160 | 11x |
has_card_arg <- "card" %in% card_fun_args_nams |
161 | 11x |
has_comment_arg <- "comment" %in% card_fun_args_nams |
162 | 11x |
has_label_arg <- "label" %in% card_fun_args_nams |
163 | ||
164 | 11x |
arg_list <- list() |
165 | ||
166 | 11x |
if (has_comment_arg) { |
167 | 4x |
arg_list <- c(arg_list, list(comment = input$comment)) |
168 |
} |
|
169 | 11x |
if (has_label_arg) { |
170 | ! |
arg_list <- c(arg_list, list(label = input$label)) |
171 |
} |
|
172 | ||
173 | 11x |
if (has_card_arg) { |
174 |
# The default_card is defined here because formals() returns a pairedlist object |
|
175 |
# of formal parameter names and their default values. The values are missing |
|
176 |
# if not defined and the missing check does not work if supplied formals(card_fun)[[1]] |
|
177 | 8x |
default_card <- formals(card_fun)$card |
178 | 8x |
card <- `if`( |
179 | 8x |
missing(default_card), |
180 | 8x |
ReportCard$new(), |
181 | 8x |
eval(default_card, envir = environment(card_fun)) |
182 |
) |
|
183 | 8x |
arg_list <- c(arg_list, list(card = card)) |
184 |
} |
|
185 | ||
186 | 11x |
card <- try(do.call(card_fun, arg_list)) |
187 | ||
188 | 11x |
if (inherits(card, "try-error")) { |
189 | 3x |
msg <- paste0( |
190 | 3x |
"The card could not be added to the report. ", |
191 | 3x |
"Have the outputs for the report been created yet? If not please try again when they ", |
192 | 3x |
"are ready. Otherwise contact your application developer" |
193 |
) |
|
194 | 3x |
warning(msg) |
195 | 3x |
shiny::showNotification( |
196 | 3x |
msg, |
197 | 3x |
type = "error" |
198 |
) |
|
199 |
} else { |
|
200 | 8x |
checkmate::assert_class(card, "ReportCard") |
201 | 8x |
if (!has_comment_arg && length(input$comment) > 0 && input$comment != "") { |
202 | 1x |
card$append_text("Comment", "header3") |
203 | 1x |
card$append_text(input$comment) |
204 |
} |
|
205 | ||
206 | 8x |
if (!has_label_arg && length(input$label) == 1 && input$label != "") { |
207 | ! |
card$set_name(input$label) |
208 |
} |
|
209 | ||
210 | 8x |
reporter$append_cards(list(card)) |
211 | 8x |
shiny::showNotification(sprintf("The card added successfully."), type = "message") |
212 | 8x |
shiny::removeModal() |
213 |
} |
|
214 |
}) |
|
215 |
}) |
|
216 |
} |
1 |
#' @title `TextBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' Specialized `ContentBlock` for embedding styled text within reports. |
|
5 |
#' It supports multiple styling options to accommodate various text roles, |
|
6 |
#' such as headers or verbatim text, in the report content. |
|
7 |
#' |
|
8 |
#' @keywords internal |
|
9 |
TextBlock <- R6::R6Class( # nolint: object_name_linter. |
|
10 |
classname = "TextBlock", |
|
11 |
inherit = ContentBlock, |
|
12 |
public = list( |
|
13 |
#' @description Initialize a `TextBlock` object. |
|
14 |
#' |
|
15 |
#' @details Constructs a `TextBlock` object with no content and the default style. |
|
16 |
#' |
|
17 |
#' @param content (`character`) a string assigned to this `TextBlock` |
|
18 |
#' @param style (`character(1)`) one of: `"default"`, `"header2"`, `"header3"` `"verbatim"` |
|
19 |
#' |
|
20 |
#' @return Object of class `TextBlock`, invisibly. |
|
21 |
#' @examples |
|
22 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
23 |
#' block <- TextBlock$new() |
|
24 |
#' |
|
25 |
initialize = function(content = character(0), style = private$styles[1]) { |
|
26 | 118x |
super$set_content(content) |
27 | 118x |
self$set_style(style) |
28 | 118x |
invisible(self) |
29 |
}, |
|
30 |
#' @description Sets the style of this `TextBlock`. |
|
31 |
#' |
|
32 |
#' @details The style has bearing on the rendering of this block. |
|
33 |
#' |
|
34 |
#' @param style (`character(1)`) one of: `"default"`, `"header2"`, `"header3"` `"verbatim"` |
|
35 |
#' |
|
36 |
#' @return `self`, invisibly. |
|
37 |
#' @examples |
|
38 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
39 |
#' block <- TextBlock$new() |
|
40 |
#' block$set_style("header2") |
|
41 |
#' |
|
42 |
set_style = function(style) { |
|
43 | 140x |
private$style <- match.arg(style, private$styles) |
44 | 139x |
invisible(self) |
45 |
}, |
|
46 |
#' @description Get the style of this `TextBlock`. |
|
47 |
#' |
|
48 |
#' @return `character(1)` the style of this `TextBlock`. |
|
49 |
#' @examples |
|
50 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
51 |
#' block <- TextBlock$new() |
|
52 |
#' block$get_style() |
|
53 |
#' |
|
54 |
get_style = function() { |
|
55 | 67x |
private$style |
56 |
}, |
|
57 |
#' @description Get available an array of styles available to this `TextBlock`. |
|
58 |
#' |
|
59 |
#' @return A `character` array of styles. |
|
60 |
#' @examples |
|
61 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
62 |
#' block <- TextBlock$new() |
|
63 |
#' block$get_available_styles() |
|
64 |
#' |
|
65 |
get_available_styles = function() { |
|
66 | 23x |
private$styles |
67 |
}, |
|
68 |
#' @description Create the `TextBlock` from a list. |
|
69 |
#' |
|
70 |
#' @param x (`named list`) with two fields `text` and `style`. |
|
71 |
#' Use the `get_available_styles` method to get all possible styles. |
|
72 |
#' |
|
73 |
#' @return `self`, invisibly. |
|
74 |
#' @examples |
|
75 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
76 |
#' block <- TextBlock$new() |
|
77 |
#' block$from_list(list(text = "sth", style = "default")) |
|
78 |
#' |
|
79 |
from_list = function(x) { |
|
80 | 14x |
checkmate::assert_list(x) |
81 | 14x |
checkmate::assert_names(names(x), must.include = c("text", "style")) |
82 | 14x |
self$set_content(x$text) |
83 | 14x |
self$set_style(x$style) |
84 | 14x |
invisible(self) |
85 |
}, |
|
86 |
#' @description Convert the `TextBlock` to a list. |
|
87 |
#' |
|
88 |
#' @return `named list` with a text and style. |
|
89 |
#' @examples |
|
90 |
#' TextBlock <- getFromNamespace("TextBlock", "teal.reporter") |
|
91 |
#' block <- TextBlock$new() |
|
92 |
#' block$to_list() |
|
93 |
#' |
|
94 |
to_list = function() { |
|
95 | 24x |
list(text = self$get_content(), style = self$get_style()) |
96 |
} |
|
97 |
), |
|
98 |
private = list( |
|
99 |
style = character(0), |
|
100 |
styles = c("default", "header2", "header3", "verbatim") |
|
101 |
), |
|
102 |
lock_objects = TRUE, |
|
103 |
lock_class = TRUE |
|
104 |
) |
1 |
#' @title `TableBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' Specialized `FileBlock` for managing table content in reports. |
|
5 |
#' It's designed to handle various table formats, converting them into a consistent, |
|
6 |
#' document-ready format (e.g., `flextable`) for inclusion in reports. |
|
7 |
#' |
|
8 |
#' @keywords internal |
|
9 |
TableBlock <- R6::R6Class( # nolint: object_name_linter. |
|
10 |
classname = "TableBlock", |
|
11 |
inherit = FileBlock, |
|
12 |
public = list( |
|
13 |
#' @description Initialize a `TableBlock` object. |
|
14 |
#' |
|
15 |
#' @param table (`data.frame` or `rtables` or `TableTree` or `ElementaryTable` or `listing_df`) a table assigned to |
|
16 |
#' this `TableBlock` |
|
17 |
#' |
|
18 |
#' @return Object of class `TableBlock`, invisibly. |
|
19 |
initialize = function(table) { |
|
20 | 26x |
if (!missing(table)) { |
21 | 4x |
self$set_content(table) |
22 |
} |
|
23 | 26x |
invisible(self) |
24 |
}, |
|
25 |
#' @description Sets content of this `TableBlock`. |
|
26 |
#' |
|
27 |
#' @details Raises error if argument is not a table-like object. |
|
28 |
#' |
|
29 |
#' @param content (`data.frame` or `rtables` or `TableTree` or `ElementaryTable` or `listing_df`) |
|
30 |
#' a table assigned to this `TableBlock` |
|
31 |
#' |
|
32 |
#' @return `self`, invisibly. |
|
33 |
#' @examples |
|
34 |
#' TableBlock <- getFromNamespace("TableBlock", "teal.reporter") |
|
35 |
#' block <- TableBlock$new() |
|
36 |
#' block$set_content(iris) |
|
37 |
#' |
|
38 |
set_content = function(content) { |
|
39 | 13x |
checkmate::assert_multi_class(content, private$supported_tables) |
40 | 12x |
content <- to_flextable(content) |
41 | 12x |
path <- tempfile(fileext = ".rds") |
42 | 12x |
saveRDS(content, file = path) |
43 | 12x |
super$set_content(path) |
44 | 12x |
invisible(self) |
45 |
} |
|
46 |
), |
|
47 |
private = list( |
|
48 |
supported_tables = c("data.frame", "rtables", "TableTree", "ElementaryTable", "listing_df") |
|
49 |
), |
|
50 |
lock_objects = TRUE, |
|
51 |
lock_class = TRUE |
|
52 |
) |
1 |
#' @title `FileBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' `FileBlock` manages file-based content in a report, |
|
5 |
#' ensuring appropriate handling of content files. |
|
6 |
#' |
|
7 |
#' @keywords internal |
|
8 |
FileBlock <- R6::R6Class( # nolint: object_name_linter. |
|
9 |
classname = "FileBlock", |
|
10 |
inherit = ContentBlock, |
|
11 |
public = list( |
|
12 |
#' @description Finalize the `FileBlock`. |
|
13 |
#' |
|
14 |
#' @details Removes the temporary file created in the constructor. |
|
15 |
finalize = function() { |
|
16 | 86x |
try(unlink(super$get_content())) |
17 |
}, |
|
18 |
#' @description Create the `FileBlock` from a list. |
|
19 |
#' The list should contain one named field, `"basename"`. |
|
20 |
#' |
|
21 |
#' @param x (`named list`) with one field `"basename"`, a name of the file. |
|
22 |
#' @param output_dir (`character`) with a path to the directory where a file will be copied. |
|
23 |
#' |
|
24 |
#' @return `self`, invisibly. |
|
25 |
#' @examples |
|
26 |
#' FileBlock <- getFromNamespace("FileBlock", "teal.reporter") |
|
27 |
#' block <- FileBlock$new() |
|
28 |
#' file_path <- tempfile(fileext = ".png") |
|
29 |
#' saveRDS(iris, file_path) |
|
30 |
#' block$from_list(list(basename = basename(file_path)), dirname(file_path)) |
|
31 |
#' |
|
32 |
from_list = function(x, output_dir) { |
|
33 | 11x |
checkmate::assert_list(x) |
34 | 11x |
checkmate::assert_names(names(x), must.include = "basename") |
35 | 11x |
path <- file.path(output_dir, x$basename) |
36 | 11x |
file_type <- paste0(".", tools::file_ext(path)) |
37 | 11x |
checkmate::assert_file_exists(path, extension = file_type) |
38 | 11x |
new_file_path <- tempfile(fileext = file_type) |
39 | 11x |
file.copy(path, new_file_path) |
40 | 11x |
super$set_content(new_file_path) |
41 | 11x |
invisible(self) |
42 |
}, |
|
43 |
#' @description Convert the `FileBlock` to a list. |
|
44 |
#' |
|
45 |
#' @param output_dir (`character`) with a path to the directory where a file will be copied. |
|
46 |
#' |
|
47 |
#' @return `named list` with a `basename` of the file. |
|
48 |
#' @examples |
|
49 |
#' FileBlock <- getFromNamespace("FileBlock", "teal.reporter") |
|
50 |
#' block <- FileBlock$new() |
|
51 |
#' block$to_list(tempdir()) |
|
52 |
#' |
|
53 |
to_list = function(output_dir) { |
|
54 | 21x |
base_name <- basename(super$get_content()) |
55 | 21x |
file.copy(super$get_content(), file.path(output_dir, base_name)) |
56 | 21x |
list(basename = base_name) |
57 |
} |
|
58 |
), |
|
59 |
lock_objects = TRUE, |
|
60 |
lock_class = TRUE |
|
61 |
) |
1 |
#' @title `NewpageBlock` |
|
2 |
#' @docType class |
|
3 |
#' @description |
|
4 |
#' A `ContentBlock` subclass that represents a page break in a report output. |
|
5 |
#' |
|
6 |
#' @keywords internal |
|
7 |
NewpageBlock <- R6::R6Class( # nolint: object_name_linter. |
|
8 |
classname = "NewpageBlock", |
|
9 |
inherit = ContentBlock, |
|
10 |
public = list( |
|
11 |
#' @description Initialize a `NewpageBlock` object. |
|
12 |
#' |
|
13 |
#' @details Returns a `NewpageBlock` object with no content and the default style. |
|
14 |
#' |
|
15 |
#' @return Object of class `NewpageBlock`, invisibly. |
|
16 |
#' @examples |
|
17 |
#' NewpageBlock <- getFromNamespace("NewpageBlock", "teal.reporter") |
|
18 |
#' block <- NewpageBlock$new() |
|
19 |
#' |
|
20 |
initialize = function() { |
|
21 | 14x |
super$set_content("\n\\newpage\n") |
22 | 14x |
invisible(self) |
23 |
} |
|
24 |
), |
|
25 |
lock_objects = TRUE, |
|
26 |
lock_class = TRUE |
|
27 |
) |