Skip to content

Commit

Permalink
169 wrapping up the long code in the Report (#218)
Browse files Browse the repository at this point in the history
this Fixes
#169

I've implemented a solution that relies on the formatR package. I've
ensured that this solution checks for the package in the namespace; if
it's not accessible, the user will be notified. To maintain flexibility,
I've included the formatR package in the suggested dependencies, as
there are no strict dependencies on it.

In response to the feedback provided by @m7pr in [this
comment](#169 (comment)),
I've updated the default value of global_knitr to list(echo = TRUE,
tidy.opts = list(width.cutoff = 60), tidy = FALSE).

Please note that there was an alternative option to set tidy = "styler",
but I opted against it due to the additional dependency it would
introduce (the styler package). Feel free to review the changes and let
me know if any further adjustments are needed.

----
This branch utilizes the functionality provided by the 'formatR' package
to automatically add spaces and indentations to the code. However, it
does create a strong dependency on the 'formatR' package. If the package
is not installed, the output will be generated without any formatting.

Additionally, this branch introduces the following changes:

- It exposes Knitr options to users through the functions
'download_report_button_srv', 'reporter_previewer_srv,'
'simple_reporter_srv,' and the R6 renderer.
- The Roxygen documentation has been updated to reflect these
modifications.

changes in reports are observed as follows.

branch: main
<img width="613" alt="image"
src="https://github.com/insightsengineering/teal.reporter/assets/6700955/84998f72-1676-46bc-aff1-e376b68e2c22">

branch: 169_wrap_rcode@main
<img width="582" alt="image"
src="https://github.com/insightsengineering/teal.reporter/assets/6700955/be027ece-c2b8-45c0-85cc-719c7ee306f6">

---------

Signed-off-by: kartikeya kirar <[email protected]>
Co-authored-by: kartikeya <[email protected]>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Marcin <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored Oct 4, 2023
1 parent b82c2ac commit 6a30a56
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters: linters_with_defaults(
line_length_linter = line_length_linter(120),
cyclocomp_linter = NULL,
object_usage_linter = NULL
object_usage_linter = NULL,
indentation_linter = NULL
)
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
zip (>= 1.1.0)
Suggests:
DT (>= 0.13),
formatR (>= 1.5),
ggplot2 (>= 3.4.0),
lattice (>= 0.18-4),
png,
Expand All @@ -44,8 +45,8 @@ RdMacros:
Config/Needs/verdepcheck: rstudio/bslib, mllg/checkmate,
rstudio/htmltools, yihui/knitr, r-lib/lifecycle, r-lib/R6,
rstudio/rmarkdown, rstudio/shiny, dreamRs/shinyWidgets,
yaml=vubiostat/r-yaml, r-lib/zip, davidgohel/flextable, rstudio/DT,
tidyverse/ggplot2, deepayan/lattice, cran/png,
yaml=vubiostat/r-yaml, r-lib/zip, davidgohel/flextable, rstudio/DT,
yihui/formatR, tidyverse/ggplot2, deepayan/lattice, cran/png,
insightsengineering/rtables, r-lib/testthat, rstudio/tinytex
Config/Needs/website: insightsengineering/nesttemplate
Encoding: UTF-8
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* `add_card_button_srv` allows to specify `card_fun` with `label` parameter for card's title & content customization.

* Supports automatic `Rcode` formatting using the suggested `formatR` package in reports.

# teal.reporter 0.2.1

### Miscellaneous
Expand Down
9 changes: 7 additions & 2 deletions R/DownloadModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ 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 of `knitr` parameters (passed to `knitr::opts_chunk$set`)
#' for customizing the rendering process.
#' @inheritParams reporter_download_inputs
#' @return `shiny::moduleServer`
#' @details `r global_knitr_details()`
#'
#' @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"
Expand All @@ -48,6 +53,7 @@ download_report_button_srv <- function(id,
toc = FALSE
)) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get()))
checkmate::assert_subset(
rmd_output,
c(
Expand Down Expand Up @@ -134,8 +140,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)
global_knitr <- list()
if (is.logical(input$showrcode)) 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
9 changes: 7 additions & 2 deletions R/Previewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ 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 of `knitr` parameters (passed to `knitr::opts_chunk$set`)
#' for customizing the rendering process.
#' @inheritParams reporter_download_inputs
#' @details `r global_knitr_details()`
#'
#' @export
reporter_previewer_srv <- function(id,
reporter,
global_knitr = getOption("teal.reporter.global_knitr"),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation",
Expand All @@ -45,6 +50,7 @@ reporter_previewer_srv <- function(id,
toc = FALSE
)) {
checkmate::assert_class(reporter, "Reporter")
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get()))
checkmate::assert_subset(
rmd_output,
c(
Expand Down Expand Up @@ -179,8 +185,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)
global_knitr <- list()
if (is.logical(input$showrcode)) 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
22 changes: 12 additions & 10 deletions R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
#' @description getting the `Rmd` text which could be easily rendered later.
#'
#' @param blocks `list` of `c("TextBlock", "PictureBlock", "NewpageBlock")` objects.
#' @param yaml_header `character` a `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 yaml_header `character` an `rmarkdown` `yaml` header.
#' @param global_knitr `list` a of `knitr` parameters (passed to `knitr::opts_chunk$set`)
#' for customizing the rendering process.
#' @details `r global_knitr_details()`
#'
#' @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 = getOption("teal.reporter.global_knitr")) {
checkmate::assert_list(blocks, c("TextBlock", "PictureBlock", "NewpageBlock", "TableBlock", "RcodeBlock"))
checkmate::assert_subset(names(global_knitr), names(knitr::opts_chunk$get()))

if (missing(yaml_header)) {
yaml_header <- md_header(yaml::as.yaml(list(title = "Report")))
}
Expand Down Expand Up @@ -84,13 +86,13 @@ 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 ... `rmarkdown::render` arguments, `input` and `output_dir` should not be updated.z
#' @param global_knitr `list` a of `knitr` parameters (passed to `knitr::opts_chunk$set`)
#' for customizing the rendering process.
#' @param ... `rmarkdown::render` arguments, `input` and `output_dir` should not be updated.
#' @details `r global_knitr_details()`
#'
#' @return `character` path to the output
render = function(blocks, yaml_header, global_knitr = list(), ...) {
render = function(blocks, yaml_header, global_knitr = getOption("teal.reporter.global_knitr"), ...) {
args <- list(...)
input_path <- self$renderRmd(blocks, yaml_header, global_knitr)
args <- append(args, list(
Expand Down
2 changes: 1 addition & 1 deletion R/ReportCard.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ReportCard <- R6::R6Class( # nolint: object_name_linter.
self$append_content(TextBlock$new(text, style))
invisible(self)
},
#' @description Appends a `rmarkdown` R chunk to this `ReportCard`.
#' @description Appends an `rmarkdown` R chunk to this `ReportCard`.
#'
#' @param text (`character(0)` or `character(1)`) the text
#' @param ... any `rmarkdown` R chunk parameter and its value.
Expand Down
5 changes: 5 additions & 0 deletions R/SimpleReporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ 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 for customizing the rendering process.
#' @inheritParams reporter_download_inputs
#' @details `r global_knitr_details()`
#'
#' @return `shiny::moduleServer`
#' @export
simple_reporter_srv <- function(id,
reporter,
card_fun,
global_knitr = getOption("teal.reporter.global_knitr"),
rmd_output = c(
"html" = "html_document", "pdf" = "pdf_document",
"powerpoint" = "powerpoint_presentation", "word" = "word_document"
Expand All @@ -68,6 +72,7 @@ simple_reporter_srv <- function(id,
download_report_button_srv(
"download_button_simple",
reporter = reporter,
global_knitr = global_knitr,
rmd_output = rmd_output,
rmd_yaml_args = rmd_yaml_args
)
Expand Down
18 changes: 18 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,21 @@ split_text_block <- function(x, n) {
ind <- rep(1:nblocks, each = n)[seq_along(lines)]
unname(lapply(split(lines, ind), paste, collapse = "\n"))
}

#' Retrieve text details for global_knitr options
#' This function returns a character string describing the default settings for the global_knitr options.
#' @noRd
#'
#' @keywords internal
global_knitr_details <- function() {
paste0(
c(
" To access the default values for the `global_knitr` parameter,",
" use `getOption('teal.reporter.global_knitr')`. These defaults include:",
" - `echo = TRUE`",
" - `tidy.opts = list(width.cutoff = 60)`",
" - `tidy = TRUE` if `formatR` package is installed, `FALSE` otherwise"
),
collapse = "\n"
)
}
22 changes: 22 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.onLoad <- function(libname, pkgname) {
op <- options()
default_global_knitr <- list(teal.reporter.global_knitr = list(
echo = TRUE,
tidy.opts = list(width.cutoff = 60),
tidy = requireNamespace("formatR", quietly = TRUE)
))

if (!("teal.reporter.global_knitr" %in% names(op))) {
options(default_global_knitr)
}

invisible()
}

.onAttach <- function(libname, pkgname) {
packageStartupMessage(
if (!requireNamespace("formatR", quietly = TRUE)) {
"For better code formatting, consider installing the formatR package."
}
)
}
47 changes: 37 additions & 10 deletions man/Renderer.Rd

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

2 changes: 1 addition & 1 deletion man/ReportCard.Rd

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

13 changes: 13 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.

13 changes: 13 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.

Loading

0 comments on commit 6a30a56

Please sign in to comment.