Skip to content

Commit

Permalink
reversing code split logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikeya committed Sep 8, 2023
1 parent d0029ab commit 861ccc1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 84 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ export(reporter_previewer_srv)
export(reporter_previewer_ui)
export(reset_report_button_srv)
export(reset_report_button_ui)
export(reverse_yaml_field)
export(rmd_output_arguments)
export(rmd_outputs)
export(simple_reporter_srv)
export(simple_reporter_ui)
export(to_flextable)
importFrom(R6,R6Class)
importFrom(checkmate,assert_string)
importFrom(grid,grid.newpage)
Expand Down
24 changes: 12 additions & 12 deletions R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
capture.output(dput(global_knitr))
)

report_type <- reverse_yaml_field(yaml_header, "output")
parsed_blocks <- paste(
unlist(
lapply(blocks, function(b) private$block2md(b, report_type))
lapply(blocks, function(b) private$block2md(b))
),
collapse = "\n\n"
)
Expand Down Expand Up @@ -153,23 +152,23 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
private = list(
output_dir = character(0),
# factory method
block2md = function(block, report_type) {
block2md = function(block) {
if (inherits(block, "TextBlock")) {
private$textBlock2md(block, report_type)
private$textBlock2md(block)
} else if (inherits(block, "RcodeBlock")) {
private$rcodeBlock2md(block, report_type)
private$rcodeBlock2md(block)
} else if (inherits(block, "PictureBlock")) {
private$pictureBlock2md(block, report_type)
private$pictureBlock2md(block)
} else if (inherits(block, "TableBlock")) {
private$tableBlock2md(block, report_type)
private$tableBlock2md(block)
} else if (inherits(block, "NewpageBlock")) {
block$get_content()
} else {
stop("Unknown block class")
}
},
# card specific methods
textBlock2md = function(block, report_type) {
textBlock2md = function(block) {
text_style <- block$get_style()
block_content <- block$get_content()
switch(text_style,
Expand All @@ -180,17 +179,18 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
block_content
)
},
rcodeBlock2md = function(block, report_type) {
rcodeBlock2md = function(block) {
params <- block$get_params()
params <- lapply(params, function(l) if (is.character(l)) shQuote(l) else l)
block_content <- block$get_content()
paste("### ",
sprintf(
"\n```{r, %s}\n%s\n```\n",
paste(names(params), params, sep = "=", collapse = ", "),
block_content
)
), collapse = "\n")
},
pictureBlock2md = function(block, report_type) {
pictureBlock2md = function(block) {
basename_pic <- basename(block$get_content())
file.copy(block$get_content(), file.path(private$output_dir, basename_pic))
params <- c(
Expand All @@ -205,7 +205,7 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
basename_pic
)
},
tableBlock2md = function(block, report_type) {
tableBlock2md = function(block) {
basename_table <- basename(block$get_content())
file.copy(block$get_content(), file.path(private$output_dir, basename_table))
sprintf("```{r echo = FALSE}\nreadRDS('%s')\n```", basename_table)
Expand Down
25 changes: 20 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ panel_item <- function(title, ..., collapsed = TRUE, input_id = NULL) {
#' Indent the row names by 10 times indentation.
#'
#' @param content Supported formats: "data.frame", "rtables", "TableTree", "ElementaryTable"

#' @return (`flextable`)
#'
#' @export
#'
#' @keywords internal
to_flextable <- function(content) {
if (inherits(content, "data.frame")) {
ft <- flextable::flextable(content)
Expand All @@ -145,7 +146,7 @@ to_flextable <- function(content) {
flextable::align_text_col(align = "center", header = TRUE) %>%
flextable::align(i = seq_len(nrow(content)), j = 1, align = "left") %>% # row names align to left
padding_lst(mf$row_info$indent) %>%
flextable::padding(padding.top = 3, padding.bottom = 3, part = "all") %>%
flextable::padding(padding.top = 1, padding.bottom = 1, part = "all") %>%
flextable::autofit(add_h = 0)

ft <- ft %>%
Expand All @@ -160,9 +161,17 @@ to_flextable <- function(content) {
flextable::width(width = dim(ft)$widths * pgwidth / flextable::flextable_dim(ft)$widths) # adjust width of each column as percentage of total width

Check warning on line 161 in R/utils.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / SuperLinter 🦸‍♂️

file=/github/workspace/R/utils.R,line=161,col=121,[line_length_linter] Lines should not be more than 120 characters.
}
} else {
ft <- content # update logic for ElementaryTable class
ft <- flextable::flextable(content)
}
return(ft)

# adding theme_booktabs theme and styling.
ft <- ft %>%
flextable::theme_booktabs() %>%
flextable::font(fontname = "arial", part = "all") %>%
flextable::fontsize(size = 8, part = "body") %>%
flextable::bold(part = "header")

ft
}

#' @noRd
Expand All @@ -179,6 +188,8 @@ get_merge_index_single <- function(span) {
}

#' @noRd
#'
#' @keywords internal
get_merge_index <- function(spans) {
ret <- lapply(seq_len(nrow(spans)), function(i) {
ri <- spans[i, ]
Expand All @@ -191,13 +202,17 @@ get_merge_index <- function(spans) {
}

#' @noRd
#'
#' @keywords internal
merge_at_indice <- function(ft, lst, part) {
Reduce(function(ft, ij) {
flextable::merge_at(ft, i = ij$i, j = ij$j, part = part)
}, lst, ft)
}

#' @noRd
#'
#' @keywords internal
padding_lst <- function(ft, indents) {
Reduce(function(ft, s) {
flextable::padding(ft, s, 1, padding.left = (indents[s] + 1) * 10)
Expand Down
36 changes: 0 additions & 36 deletions R/yaml_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,42 +246,6 @@ as_yaml_auto <- function(input_list,
structure(result, class = "rmd_yaml_header")
}

#' Extract a Field from YAML and Optionally Retrieve Names from a List
#'
#' This function parses a YAML text and extracts the specified field. It provides
#' the option to retrieve the names of elements from a list if the field contains a list.
#'
#' @param yaml_text A character vector containing the YAML text.
#' @param field_name The name of the field to extract.
#' @param check_list Logical, indicating whether to check if the result is a list
#' and retrieve the names of list elements. Default is TRUE.
#'
#' @return If `check_list` is TRUE and the result is a list, it returns the names of
#' elements in the list; otherwise, it returns the extracted field.
#'
#' @examples
#' yaml_text <- "\nauthor: NEST\ntitle: Report\noutput:\n powerpoint_presentation:\n toc: yes\n"
#' reverse_yaml_field(yaml_text, "output") # Returns a character vector with "Reading" and "Cooking"

#'
#' @export
reverse_yaml_field <- function(yaml_text, field_name, check_list = TRUE) {
checkmate::assert_multi_class(yaml_text, c("rmd_yaml_header", "character"))
checkmate::assert_string(field_name)
checkmate::assert_logical(check_list)
# Parse the YAML text
yaml_obj <- yaml::yaml.load(yaml_text)

# Extract the specified field
if (field_name %in% names(yaml_obj)) {
result <- yaml_obj[[field_name]]
if (check_list && is.list(result)) {
return(names(result))
}
return(result)
}
}

#' @title Print method for the `yaml_header` class
#'
#' @description `r lifecycle::badge("experimental")`
Expand Down
29 changes: 0 additions & 29 deletions man/reverse_yaml_field.Rd

This file was deleted.

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

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

0 comments on commit 861ccc1

Please sign in to comment.