/
DownloadModule.R
331 lines (313 loc) · 10.3 KB
/
DownloadModule.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#' Download report button module
#'
#' @description `r lifecycle::badge("experimental")`
#'
#' Provides a button that triggers downloading a report.
#'
#' For more information, refer to the vignette: `vignette("simpleReporter", "teal.reporter")`.
#'
#' @details `r global_knitr_details()`
#'
#' @name download_report_button
#'
#' @param id (`character(1)`) this `shiny` module's id.
#' @param reporter (`Reporter`) instance.
#' @param global_knitr (`list`) of `knitr` parameters (passed to `knitr::opts_chunk$set`)
#' for customizing the rendering process.
#' @inheritParams reporter_download_inputs
#'
#' @return `NULL`.
NULL
#' @rdname download_report_button
#' @export
download_report_button_ui <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
shiny::singleton(
shiny::tags$head(shiny::includeCSS(system.file("css/custom.css", package = "teal.reporter")))
),
shiny::tags$button(
id = ns("download_button"),
type = "button",
class = "simple_report_button btn btn-primary action-button",
title = "Download",
`data-val` = shiny::restoreInput(id = ns("download_button"), default = NULL),
NULL,
shiny::tags$span(
shiny::icon("download")
)
)
)
}
#' @rdname download_report_button
#' @export
download_report_button_srv <- function(id,
reporter,
global_knitr = getOption("teal.reporter.global_knitr"),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation", "word" = "word_document"
),
rmd_yaml_args = list(
author = "NEST", title = "Report",
date = as.character(Sys.Date()), output = "html_document",
toc = FALSE
)) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get()))
checkmate::assert_subset(
rmd_output,
c(
"html_document", "pdf_document",
"powerpoint_presentation", "word_document"
),
empty.ok = FALSE
)
checkmate::assert_list(rmd_yaml_args, names = "named")
checkmate::assert_names(
names(rmd_yaml_args),
subset.of = c("author", "title", "date", "output", "toc"),
must.include = "output"
)
checkmate::assert_true(rmd_yaml_args[["output"]] %in% rmd_output)
shiny::moduleServer(id, function(input, output, session) {
shiny::setBookmarkExclude(c("download_button"))
ns <- session$ns
download_modal <- function() {
nr_cards <- length(reporter$get_cards())
downb <- shiny::tags$a(
id = ns("download_data"),
class = paste("btn btn-primary shiny-download-link", if (nr_cards) NULL else "disabled"),
style = if (nr_cards) NULL else "pointer-events: none;",
href = "",
target = "_blank",
download = NA,
shiny::icon("download"),
"Download"
)
shiny::modalDialog(
easyClose = TRUE,
shiny::tags$h3("Download the Report"),
shiny::tags$hr(),
if (length(reporter$get_cards()) == 0) {
shiny::tags$div(
class = "mb-4",
shiny::tags$p(
class = "text-danger",
shiny::tags$strong("No Cards Added")
)
)
} else {
shiny::tags$div(
class = "mb-4",
shiny::tags$p(
class = "text-success",
shiny::tags$strong(paste("Number of cards: ", nr_cards))
),
)
},
reporter_download_inputs(
rmd_yaml_args = rmd_yaml_args,
rmd_output = rmd_output,
showrcode = any_rcode_block(reporter),
session = session
),
footer = shiny::tagList(
shiny::tags$button(
type = "button",
class = "btn btn-secondary",
`data-dismiss` = "modal",
`data-bs-dismiss` = "modal",
NULL,
"Cancel"
),
downb
)
)
}
shiny::observeEvent(input$download_button, {
shiny::showModal(download_modal())
})
output$download_data <- shiny::downloadHandler(
filename = function() {
paste0(
"report_",
if (reporter$get_id() == "") NULL else paste0(reporter$get_id(), "_"),
format(Sys.time(), "%y%m%d%H%M%S"),
".zip"
)
},
content = function(file) {
shiny::showNotification("Rendering and Downloading the document.")
shinybusy::block(id = ns("download_data"), text = "", type = "dots")
input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]])
names(input_list) <- names(rmd_yaml_args)
if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode
report_render_and_compress(reporter, input_list, global_knitr, file)
shinybusy::unblock(id = ns("download_data"))
},
contentType = "application/zip"
)
})
}
#' Render the report
#'
#' Render the report and zip the created directory.
#'
#' @param reporter (`Reporter`) instance.
#' @param input_list (`list`) like `shiny` input converted to a regular named list.
#' @param global_knitr (`list`) a global `knitr` parameters, like echo.
#' But if local parameter is set it will have priority.
#' @param file (`character(1)`) where to copy the returned directory.
#'
#' @return `file` argument, invisibly.
#'
#' @keywords internal
report_render_and_compress <- function(reporter, input_list, global_knitr, file = tempdir()) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_list(input_list, names = "named")
checkmate::assert_string(file)
if (
identical("pdf_document", input_list$output) &&
inherits(try(system2("pdflatex", "--version", stdout = TRUE), silent = TRUE), "try-error")
) {
shiny::showNotification(
ui = "pdflatex is not available so the pdf_document could not be rendered. Please use other output type.",
action = "Please contact app developer",
type = "error"
)
stop("pdflatex is not available so the pdf_document could not be rendered.")
}
yaml_header <- as_yaml_auto(input_list)
renderer <- Renderer$new()
tryCatch(
renderer$render(reporter$get_blocks(), yaml_header, global_knitr),
warning = function(cond) {
print(cond)
shiny::showNotification(
ui = "Render document warning!",
action = "Please contact app developer",
type = "warning"
)
},
error = function(cond) {
print(cond)
shiny::showNotification(
ui = "Render document error!",
action = "Please contact app developer",
type = "error"
)
}
)
output_dir <- renderer$get_output_dir()
tryCatch(
archiver_dir <- reporter$to_jsondir(output_dir),
warning = function(cond) {
print(cond)
shiny::showNotification(
ui = "Archive document warning!",
action = "Please contact app developer",
type = "warning"
)
},
error = function(cond) {
print(cond)
shiny::showNotification(
ui = "Archive document error!",
action = "Please contact app developer",
type = "error"
)
}
)
temp_zip_file <- tempfile(fileext = ".zip")
tryCatch(
expr = zip::zipr(temp_zip_file, output_dir),
warning = function(cond) {
print(cond)
shiny::showNotification(
ui = "Zipping folder warning!",
action = "Please contact app developer",
type = "warning"
)
},
error = function(cond) {
print(cond)
shiny::showNotification(
ui = "Zipping folder error!",
action = "Please contact app developer",
type = "error"
)
}
)
tryCatch(
expr = file.copy(temp_zip_file, file),
warning = function(cond) {
print(cond)
shiny::showNotification(
ui = "Copying file warning!",
action = "Please contact app developer",
type = "warning"
)
},
error = function(cond) {
print(cond)
shiny::showNotification(
ui = "Copying file error!",
action = "Please contact app developer",
type = "error"
)
}
)
rm(renderer)
invisible(file)
}
#' Get the custom list of UI inputs
#'
#' @param rmd_output (`character`) vector with `rmarkdown` output types,
#' by default all possible `pdf_document`, `html_document`, `powerpoint_presentation`, and `word_document`.
#' If vector is named then those names will appear in the `UI`.
#' @param rmd_yaml_args (`named list`) with `Rmd` `yaml` header fields and their default values.
#' This `list` will result in the custom subset of UI inputs for the download reporter functionality.
#' Default `list(author = "NEST", title = "Report", date = Sys.Date(), output = "html_document", toc = FALSE)`.
#' The `list` must include at least `"output"` field.
#' The default value for `"output"` has to be in the `rmd_output` argument.
#'
#' @keywords internal
reporter_download_inputs <- function(rmd_yaml_args, rmd_output, showrcode, session) {
shiny::tagList(
lapply(names(rmd_yaml_args), function(e) {
switch(e,
author = shiny::textInput(session$ns("author"), label = "Author:", value = rmd_yaml_args$author),
title = shiny::textInput(session$ns("title"), label = "Title:", value = rmd_yaml_args$title),
date = shiny::dateInput(session$ns("date"), "Date:", value = rmd_yaml_args$date),
output = shiny::tags$div(
shinyWidgets::pickerInput(
inputId = session$ns("output"),
label = "Choose a document type: ",
choices = rmd_output,
selected = rmd_yaml_args$output
)
),
toc = shiny::checkboxInput(session$ns("toc"), label = "Include Table of Contents", value = rmd_yaml_args$toc)
)
}),
if (showrcode) {
shiny::checkboxInput(
session$ns("showrcode"),
label = "Include R Code",
value = FALSE
)
}
)
}
#' @noRd
#' @keywords internal
any_rcode_block <- function(reporter) {
any(
vapply(
reporter$get_blocks(),
function(e) inherits(e, "RcodeBlock"),
logical(1)
)
)
}