Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

212 fixing r-code formating #216

Merged
merged 96 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 90 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
0c6c097
docs: add minimal version of deps
averissimo Jul 25, 2023
1877d51
fix: ggplot2 needs to be bumped higher due to a dependency
averissimo Jul 26, 2023
1e6f3fa
fix: zip and shinyWidgets need to be bumped higher
averissimo Jul 26, 2023
ea981fe
doc: corrects NEWS after rebase
averissimo Sep 1, 2023
b78f92f
docs: remove old working branch
averissimo Sep 1, 2023
49fb444
ci: remove push on branch to prepare merge
averissimo Sep 4, 2023
b3fbfce
updating utils function to convert it into flextable.
Sep 7, 2023
c0b3a4a
updating utils function for flextable.
Sep 7, 2023
602a568
updating description.
Sep 7, 2023
4bdf620
merge back from main
Sep 7, 2023
65e395b
resolved conflict with description
Sep 7, 2023
c204d8e
Merge 65e395b655ca6ed6f5b11b8a5898c10afa15813c into 55e890abb7ba165f6…
kartikeyakirar Sep 7, 2023
1e1f3fb
[skip actions] Restyle files
github-actions[bot] Sep 7, 2023
2ad9352
add reverse_yaml_field function to extract report type
Sep 7, 2023
d0029ab
Merge branch '185_fixing_formating@main' of https://github.com/insigh…
Sep 7, 2023
861ccc1
reversing code split logic
Sep 8, 2023
ec29177
Merge 861ccc1a0f947aedfd37ad9e36085cf66076f260 into 55e890abb7ba165f6…
kartikeyakirar Sep 8, 2023
19bf333
[skip actions] Restyle files
github-actions[bot] Sep 8, 2023
4f04939
updating test and fixing linter
Sep 8, 2023
f9fea46
removing duplicate news due to merge issue.
Sep 8, 2023
92df034
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 8, 2023
ca455be
updating Rd and fixing test
Sep 8, 2023
c9f5e69
blank commit
Sep 8, 2023
a1d97b2
removing magritter dependencies
Sep 8, 2023
a6d2e2a
added custom theme
Sep 8, 2023
95d5a49
Merge a6d2e2a2ae9a423a1fdd4566e4da1661287ec0b9 into 55e890abb7ba165f6…
kartikeyakirar Sep 8, 2023
945c377
[skip actions] Restyle files
github-actions[bot] Sep 8, 2023
221abdd
Empty-Commit
Sep 8, 2023
7d935b0
merge back from main
Sep 11, 2023
5f6fc66
fixing preview
Sep 11, 2023
2a7f44f
Merge branch 'main' into 185_fixing_formating@main
kartikeyakirar Sep 11, 2023
b3aab20
updating description for verdepcheck checka
Sep 12, 2023
7a8ccd5
Merge branch 'main' into 185_fixing_formating@main
kartikeyakirar Sep 14, 2023
4a7cfa9
Update R/Renderer.R
kartikeyakirar Sep 15, 2023
9c2c397
Merge 4a7cfa9bcfec912c276b664440246a94edf84eba into 89546d6a806f56fa5…
kartikeyakirar Sep 15, 2023
1705ffd
[skip actions] Restyle files
github-actions[bot] Sep 15, 2023
d550252
Update R/utils.R
kartikeyakirar Sep 15, 2023
6fb45ef
Update R/utils.R
kartikeyakirar Sep 15, 2023
e78f6bd
Update R/utils.R
kartikeyakirar Sep 15, 2023
a2f6613
Update R/utils.R
kartikeyakirar Sep 15, 2023
52a773c
Update R/utils.R
kartikeyakirar Sep 15, 2023
4ca61b2
adding tests
Sep 15, 2023
1bf2cee
Update tests/testthat/test-utils.R
kartikeyakirar Sep 15, 2023
f71c42f
Update tests/testthat/test-utils.R
kartikeyakirar Sep 15, 2023
0e9765b
updating name
Sep 15, 2023
8e4d546
adding utils functions to split code.
kartikeyakirar Sep 18, 2023
e972b8f
adding utility functions to get report type
Sep 19, 2023
bac9567
merge back from main
Sep 19, 2023
6174853
fixing width
Sep 19, 2023
a7180f0
updating Rd files and linter fixes
Sep 19, 2023
f5fa68f
fixing unit-test
Sep 19, 2023
4fdd520
removing duplicate import
Sep 19, 2023
e2fb8e0
fixing font
Sep 20, 2023
2df5749
fixing spell issue.
Sep 20, 2023
245fb44
Update R/utils.R
kartikeyakirar Sep 20, 2023
e94d5af
Update R/utils.R
kartikeyakirar Sep 20, 2023
95a2ad7
Update R/utils.R
kartikeyakirar Sep 20, 2023
f7e32ee
Update R/utils.R
kartikeyakirar Sep 20, 2023
151c658
Merge f7e32eedbb6000d1ebe9f7018745a666146e9bbd into 4fa942ec6c8c72f46…
kartikeyakirar Sep 20, 2023
159b70f
[skip actions] Restyle files
github-actions[bot] Sep 20, 2023
f9f72a7
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 20, 2023
4a42480
adding unit-tests
Sep 20, 2023
8abe363
Merge branch '212_rocde_formating@185_fixing_formating@main' of https…
Sep 20, 2023
9541307
updating unit-test
Sep 20, 2023
be7c1ad
Merge 9541307abe52b53d412ec5e2d162b018510f64b4 into 4fa942ec6c8c72f46…
kartikeyakirar Sep 20, 2023
967fc1e
[skip actions] Restyle files
github-actions[bot] Sep 20, 2023
45b88fd
optimizing the sprintf statements and adding unit-test
Sep 21, 2023
8011bbf
empty commit
Sep 21, 2023
6a0b8ac
Merge 8011bbfb10463909778ec8d02a15215e13b83cc7 into 4fa942ec6c8c72f46…
kartikeyakirar Sep 21, 2023
778300e
[skip actions] Restyle files
github-actions[bot] Sep 21, 2023
35933a9
Empty-Commit
Sep 21, 2023
94a40af
removing check_list arg
Sep 21, 2023
be1183f
fixing spell
Sep 21, 2023
6a24d44
removing browser()
Sep 21, 2023
4653580
fixing issue with quoting the string.
Sep 21, 2023
8f40958
fixing quote issue.
Sep 21, 2023
c2a5e25
refactoring testname
Sep 21, 2023
82390bc
Update R/yaml_utils.R
kartikeyakirar Sep 21, 2023
ce74b54
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 21, 2023
3f19c4c
fixing tests and split_text_block
Sep 21, 2023
008dce7
Merge branch '212_rocde_formating@185_fixing_formating@main' of https…
Sep 21, 2023
97f82ab
updating .github workflow for testing example
Sep 21, 2023
18992e5
fixing var naming
Sep 26, 2023
694df8d
updating description.
Sep 26, 2023
6076746
merge back from main
Sep 28, 2023
5cce98b
refactoring the function name.
Sep 28, 2023
72ebfaa
updating format_code_block_for_slide()
Sep 28, 2023
d980bdd
Merge 72ebfaad2e2a595bbca2ff2d4599286faba52933 into 6a5664f51fd9ea5c1…
kartikeyakirar Sep 28, 2023
a26dd64
[skip actions] Restyle files
github-actions[bot] Sep 28, 2023
bd94647
fixing styling.
Sep 28, 2023
5922b26
changed fun to string.
Sep 28, 2023
939db82
updating unit-test
Sep 29, 2023
60a3ec6
refactoring variable name.
Sep 29, 2023
226ef9e
prefixing the package name.
Sep 29, 2023
60972d3
removing line_break parameter comments
Sep 29, 2023
525c4a1
amend unit tests
Sep 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
with:
additional-env-vars: |
_R_CHECK_CRAN_INCOMING_REMOTE_=false
_R_CHECK_EXAMPLE_TIMING_THRESHOLD_=10
additional-r-cmd-check-params: --as-cran
enforce-note-blocklist: true
note-blocklist: |
Expand Down
61 changes: 46 additions & 15 deletions R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,36 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
if (missing(yaml_header)) {
yaml_header <- md_header(yaml::as.yaml(list(title = "Report")))
}
parsed_yaml <- yaml_header

private$report_type <- get_yaml_field(yaml_header, "output")
format_code_block_for_slide <- function() {
paste0(
c(
"code_block <- function (code_text) {",
" df <- data.frame(code_text)",
" ft <- flextable::flextable(df)",
" ft <- flextable::delete_part(ft, part = 'header')",
" ft <- flextable::autofit(ft, add_h = 0)",
" ft <- flextable::fontsize(ft, size = 7, part = 'body')",
" ft <- flextable::bg(x = ft, bg = 'lightgrey')",
" ft <- flextable::border_outer(ft)",
" if (flextable::flextable_dim(ft)$widths > 8) {",
" ft <- flextable::width(ft, width = 8)",
" }",
" ft",
"}"
),
collapse = "\n"
)
}
parsed_global_knitr <- sprintf(
"\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(%s)\n```\n",
capture.output(dput(global_knitr))
"\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(%s)\n%s\n```\n",
capture.output(dput(global_knitr)),
if (identical(private$report_type, "powerpoint_presentation")) {
format_code_block_for_slide()
} else {
character(0)
}
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved
)

parsed_blocks <- paste(
Expand All @@ -46,7 +72,7 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
collapse = "\n\n"
)

rmd_text <- paste0(parsed_yaml, "\n", parsed_global_knitr, "\n", parsed_blocks, "\n")
rmd_text <- paste0(yaml_header, "\n", parsed_global_knitr, "\n", parsed_blocks, "\n")
tmp <- tempfile(fileext = ".Rmd")
input_path <- file.path(
private$output_dir,
Expand Down Expand Up @@ -88,6 +114,7 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
),
private = list(
output_dir = character(0),
report_type = NULL,
# factory method
block2md = function(block) {
if (inherits(block, "TextBlock")) {
Expand Down Expand Up @@ -119,18 +146,22 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
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(
sep = "\n",
collapse = "\n",
"### ",
if (identical(private$report_type, "powerpoint_presentation")) {
block_content_list <- split_text_block(block$get_content(), 30)
paste(
sprintf(
"---\n\n```{r, echo=FALSE}\ncode_block(\n%s)\n```\n",
shQuote(block_content_list, type = "cmd")
),
collapse = "\n\n"
)
} else {
sprintf(
"```{r, %s}", paste(names(params), params, sep = "=", collapse = ", ")
),
block_content,
"```",
""
)
"--- \n\n```{r, %s}\n%s\n```\n",
paste(names(params), params, sep = "=", collapse = ", "),
block$get_content()
)
}
},
pictureBlock2md = function(block) {
basename_pic <- basename(block$get_content())
Expand Down
32 changes: 31 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ 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`, `listing_df`

#'
#' @return (`flextable`)
#'
#' @keywords internal
Expand Down Expand Up @@ -185,6 +185,7 @@ to_flextable <- function(content) {
#'
#' @keywords internal
custom_theme <- function(ft) {
checkmate::assert_class(ft, "flextable")
ft <- flextable::fontsize(ft, size = 8, part = "body")
ft <- flextable::bold(ft, part = "header")
ft <- flextable::theme_booktabs(ft)
Expand Down Expand Up @@ -247,3 +248,32 @@ padding_lst <- function(ft, indents) {
flextable::padding(ft, s, 1, padding.left = (indents[s] + 1) * 10)
}, seq_len(length(indents)), ft)
}

#' Split a text block into smaller blocks with a specified number of lines.
#'
#' Divide text block into smaller blocks.
#'
#' A single character string containing a text block of multiple lines (separated by `\n`)
#' is split into multiple strings with n or less lines each.
#'
#' @param block_text `character` string containing the input block of text
#' @param n `integer` number of lines per block
#'
#' @return
#' List of character strings with up to `n` lines in each element.
#'
#' @keywords internal
split_text_block <- function(x, n) {
checkmate::assert_string(x)
checkmate::assert_integerish(n, lower = 1L, len = 1L)

lines <- strsplit(x, "\n")[[1]]

if (length(lines) <= n) {
return(x)
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved
}

nblocks <- ceiling(length(lines) / n)
ind <- rep(1:nblocks, each = n)[seq_along(lines)]
unname(lapply(split(lines, ind), paste, collapse = "\n"))
}
23 changes: 23 additions & 0 deletions R/yaml_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,26 @@ as_yaml_auto <- function(input_list,
print.rmd_yaml_header <- function(x, ...) {
cat(x, ...)
}

#' Parses `yaml` text, extracting the specified field. Returns list names if it's a list;
#' otherwise, the field itself.
#'
#' @param yaml_text A character vector containing the `yaml` text.
#' @param field_name The name of the field to extract.
#'
#' @return if the field is a list, it returns the names of elements in the list; otherwise,
#' it returns the extracted field.
#'
#' @keywords internal
get_yaml_field <- function(yaml_text, field_name) {
checkmate::assert_multi_class(yaml_text, c("rmd_yaml_header", "character"))
checkmate::assert_string(field_name)

yaml_obj <- yaml::yaml.load(yaml_text)

result <- yaml_obj[[field_name]]
if (is.list(result)) {
result <- names(result)
}
result
}
23 changes: 23 additions & 0 deletions man/get_yaml_field.Rd

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

24 changes: 24 additions & 0 deletions man/split_text_block.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ test_that("padding_lst applies padding to a flextable based on indentation level
padded_ft <- padding_lst(sample_ft, sample_indents)
expect_is(padded_ft, "flextable")
})

block_text <- "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6"

test_that("split_text_block - splits text block into equal parts", {
n <- 2
result <- split_text_block(block_text, n)
expected_result <- list("Line 1\nLine 2", "Line 3\nLine 4", "Line 5\nLine 6")
expect_equal(result, expected_result)
})
kartikeyakirar marked this conversation as resolved.
Show resolved Hide resolved

test_that("split_text_block - n greater than the number of line breaks", {
n <- 7
result <- split_text_block(block_text, n)
expect_equal(result, block_text)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-yaml_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,10 @@ testthat::test_that("as_yaml_auto - accept multi outputs with the multi_output a
NA
)
})

test_that("get_yaml_field returns the correct result", {
yaml_text <- "---\nauthor: ''\ndate: '2022-04-29'\noutput:\n pdf_document:\n toc: yes\n keep_tex: yes\n---\n"
field_name <- "output"
result <- get_yaml_field(yaml_text, field_name)
expect_equal(result, "pdf_document")
})