Merck / r2rtf

Easily Create Production-Ready Rich Text Format (RTF) Table and Figure
https://merck.github.io/r2rtf
GNU General Public License v3.0
76 stars 19 forks source link

rtf_subline & page_by #197

Closed Amex-AZ closed 11 months ago

Amex-AZ commented 11 months ago

Hello! Can the rtf_subline text be adjusted dynamically dependent on page_by?

Context I have a few of pages of rft output. Each page will be based on a distinct criteria (groupn = page_by).
How can I effectively update rtf_subline(text = "condition") for each condition.

The only workable approach was to avoid using page_by and instead break and join the pages.

#The what I have tried  bc_condition1, bc_condition2, and bc_condition3 data frames(works for 2 rtfs) 
bc_final1 <- convert_summary_table(bc_condition1)
bc_final2 <- convert_summary_table(bc_condition2)
bc_final3 <- convert_summary_table(bc_condition3)

convert_to_rtf <- function(bc_final, condition_name) {
  bc_rtf <- bc_final %>%
  rtf_page(orientation = "portrait", border_first ="single", border_last = "single") %>%
  rtf_title(title = "Table 14.x.x", subtitle = c("BC", "Full Analysis Set"), text_justification = "c", text_font_size = 9) %>%
  rtf_subline(text = condition_name, text_indent_left = 0, text_font_size = 9) %>%
  rtf_colheader(colheader = " |Statistics | Drug1 | Drug2  | Placebo  | Total | " ,
                col_rel_width = c(6, 4, 3, 3, 3, 3),
                text_justification = c("c", "c", "c", "c", "c", "c"),
                border_top = rep("single", 6),
                border_right = rep("", 6),
                border_left = rep("", 6)) %>%
  rtf_body(as_colheader = TRUE,
           col_rel_width = c(6, 3, 3, 3, 3, 3),
           border_first = rep("single", 6),
           border_last = rep("single", 6),
           border_left = rep("", 6),
           border_right = rep("", 6),
           text_justification = c("c", "c", "c", "c", "c", "c"),
           text_font_size = 6,
          # page_by = "groupn",
          # new_page = TRUE,
           last_row = FALSE)  %>%
rtf_page_header()

  return(bc_rtf)
}

#rtf_subline(text = condition_name) for each page. 
bc_rtf1 <- convert_to_rtf(bc_final1, "Condition1 from page 1")
bc_rtf2 <- convert_to_rtf(bc_final2, "Condition2 from page 2")
bc_rtf3 <- convert_to_rtf(bc_final3, "Condition3 from page 3")

rtf_append <- list(bc_rtf1, bc_rtf2, bc_rtf3)

rtf_append %>% 
  rtf_encode() %>%
  write_rtf("dummy.rtf")
elong0527 commented 11 months ago

The rtf_subline is designed to be a constant for a table. The content in each page depends on multiple factors and only determined after rtf_encode is executed. So r2rtf did not directly support this feature.

If you do not want to generate multiple tables and combine them, you may post-processing the information generated by rtf_code.

Here is an example to achieve the goal.

x <- iris %>% 
  rtf_subline("place---holder") %>%
  rtf_colheader("a|b|c|d") %>%
  rtf_body(
    page_by = "Species", 
    new_page = TRUE) %>%
  rtf_encode(verbose = FALSE)

pattern <- "place---holder"

i <- 0
while(grepl(pattern,x$body, fixed = TRUE)){
  i <- i + 1
  x$body <- sub(pattern, glue::glue("condition in page {i}"), x$body)
}

x %>% 
  write_rtf("tmp.rtf")
Amex-AZ commented 11 months ago

Your logic produced the output I was looking for, thanks!