Skip to content

Commit

Permalink
adding default options on functions efination.
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikeya committed Sep 29, 2023
1 parent 74cf1b9 commit 79bd779
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 21 deletions.
7 changes: 4 additions & 3 deletions R/DownloadModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ download_report_button_ui <- function(id) {
#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`.
#' @param id `character(1)` this `shiny` module's id.
#' @param reporter [`Reporter`] instance.
#' @param global_knitr `list` a global `knitr` parameters, like echo.
#' @param global_knitr `list` a global `knitr` parameters for customizing the rendering process.
#' Defaults to `list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE)`.
#' @inheritParams reporter_download_inputs
#' @return `shiny::moduleServer`
#' @export
download_report_button_srv <- function(id,
reporter,
global_knitr = list(),
global_knitr = list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation", "word" = "word_document"
Expand Down Expand Up @@ -136,7 +137,7 @@ download_report_button_srv <- function(id,
shiny::showNotification("Rendering and Downloading the document.")
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 <- append(global_knitr, list(echo = input$showrcode))
if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode
report_render_and_compress(reporter, input_list, global_knitr, file)
},
contentType = "application/zip"
Expand Down
7 changes: 4 additions & 3 deletions R/Previewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ reporter_previewer_ui <- function(id) {
#' For more details see the vignette: `vignette("previewerReporter", "teal.reporter")`.
#' @param id `character(1)` this `shiny` module's id.
#' @param reporter `Reporter` instance
#' @param global_knitr `list` a global `knitr` parameters, like echo.
#' @param global_knitr `list` a global `knitr` parameters for customizing the rendering process.
#' Defaults to `list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE)`.
#' @inheritParams reporter_download_inputs
#' @export
reporter_previewer_srv <- function(id,
reporter,
global_knitr = list(),
global_knitr = list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation",
Expand Down Expand Up @@ -181,7 +182,7 @@ reporter_previewer_srv <- function(id,
shiny::showNotification("Rendering and Downloading the document.")
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 <- append(global_knitr, list(echo = input$showrcode))
if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode
report_render_and_compress(reporter, input_list, global_knitr, file)
},
contentType = "application/zip"
Expand Down
20 changes: 12 additions & 8 deletions R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
#' Defaults to empty `list()`.
#'
#' @return `character` a `Rmd` text (`yaml` header + body), ready to be rendered.
renderRmd = function(blocks, yaml_header, global_knitr = list()) {
renderRmd = function(blocks, yaml_header, global_knitr = list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE)) {
checkmate::assert_list(blocks, c("TextBlock", "PictureBlock", "NewpageBlock", "TableBlock", "RcodeBlock"))
checkmate::assert_list(global_knitr)

valid_options <- names(knitr::opts_chunk$get())
global_knitr <- global_knitr[names(global_knitr) %in% valid_options]
if(!all(names(global_knitr) %in% valid_options)) {
message("Invalid options removed from global_knitr.")
}
if (missing(yaml_header)) {
yaml_header <- md_header(yaml::as.yaml(list(title = "Report")))
}
parsed_yaml <- yaml_header
if (requireNamespace("formatR", quietly = TRUE)) {
global_knitr <- append(global_knitr, list(tidy.opts = list(width.cutoff = 60), tidy = TRUE))
} else {
if (!requireNamespace("formatR", quietly = TRUE)) {
message("For better code formatting, consider installing the formatR package.")
}
parsed_global_knitr <- sprintf(
Expand Down Expand Up @@ -64,13 +69,12 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
#'
#' @param blocks `list` of `c("TextBlock", "PictureBlock", "NewpageBlock")` objects.
#' @param yaml_header `character` an `rmarkdown` `yaml` header.
#' @param global_knitr `list` a global `knitr` parameters, like echo.
#' But if local parameter is set it will have priority.
#' Defaults to empty `list()`.
#' @param global_knitr `list` a global `knitr` parameters for customizing the rendering process.
#' Defaults to `list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE)`.
#' @param ... `rmarkdown::render` arguments, `input` and `output_dir` should not be updated.z
#'
#' @return `character` path to the output
render = function(blocks, yaml_header, global_knitr = list(), ...) {
render = function(blocks, yaml_header, global_knitr = list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE), ...) {
args <- list(...)
input_path <- self$renderRmd(blocks, yaml_header, global_knitr)
args <- append(args, list(
Expand Down
5 changes: 3 additions & 2 deletions R/SimpleReporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ simple_reporter_ui <- function(id) {
#' @param reporter [`Reporter`] instance.
#' @param card_fun `function` which returns a [`ReportCard`] instance,
#' the function has a `card` argument and an optional `comment` argument.
#' @param global_knitr `list` a global `knitr` parameters, like echo.
#' @param global_knitr `list` a global `knitr` parameters for customizing the rendering process.
#' Defaults to `list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE)`.
#' @inheritParams reporter_download_inputs
#' @return `shiny::moduleServer`
#' @export
simple_reporter_srv <- function(id,
reporter,
card_fun,
global_knitr = list(),
global_knitr = list(echo = TRUE, tidy.opts = list(width.cutoff = 60), tidy = TRUE),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation", "word" = "word_document"
Expand Down
18 changes: 13 additions & 5 deletions man/Renderer.Rd

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

4 changes: 4 additions & 0 deletions man/download_report_button_srv.Rd

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

4 changes: 4 additions & 0 deletions man/reporter_previewer_srv.Rd

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

4 changes: 4 additions & 0 deletions man/simple_reporter_srv.Rd

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

0 comments on commit 79bd779

Please sign in to comment.