Merck / r2rtf

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

bug fix #174 #175

Closed fb-elong closed 1 year ago

fb-elong commented 1 year ago

divided from 0 instead of 1 to allow first page to have same number of rows.

Root cause: divided by should start from 0 instead of 1 to get the same number of rows in the first page.

Example:

> 1:10 %/% 3
 [1] 0 0 1 1 1 2 2 2 3 3

Example code:

library(r2rtf)
#> Warning: package 'r2rtf' was built under R version 4.1.3
library(glue)
#> Warning: package 'glue' was built under R version 4.1.3
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.1.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
n_trt <- 100
n_placebo <- 50
# Create empty dataframe
df <- data.frame(matrix(ncol = 6, nrow = 200))

# Assign column names
colnames(df) <- c("Column1", "Column2", "Column3", "Column4", "Column5", "Column6")

# Generate random character data
set.seed(123)  # For reproducibility
for (i in 1:6) {
  df[, i] <- replicate(200, paste(sample(letters, 35, replace = TRUE), collapse = ""))
}

col_width <- c(5,3,3,3,3,3)

col_header <- c(
  "|Treatment 1| |Oddsd Ratio [1]|Relative Risk [1]|Risk Difference [1]",
  " |Only|Placebo|(95% CI)|(95% CI)|(95% CI)",
  glue('Preferred Term|(N={n_trt})|(N={n_placebo})|p-value|p-value|p-value')
)  

col_header <- purrr::map(col_header, function(x) strsplit(x, "|", fixed = TRUE)[[1]])
col_header <- do.call(cbind, col_header)
col_header <- paste(apply(col_header, 1, paste, collapse = "\n"), collapse = "|")

df %>%
  # rtf_page(orientation = "landscape") %>%
  rtf_page(orientation = "landscape",
           # width = ifelse(orientation == "portrait", 8.5, 11),
           # height = ifelse(orientation == "portrait", 11, 8.5),
           # nrow = ifelse(orientation == "portrait", 40, 24)
           width = 11,
           height = 8.5,
           nrow = 23
  ) %>%
  rtf_page_header(text = "[CONFIDENTIAL]                                                                                                                CSR \\line Protocol StudyID                                                                                                                   Page \\pagenumber of \\pagefield",
                  text_justification = "j",
                  text_font_size = 8,
                  text_convert = TRUE
  ) %>%
  rtf_footnote(
    footnote = c("Note: At each level of subject summarization, a subject is counted once for the most severe event if the subject reported one or more events. * Indicates p-value < 0.05. MedDRA 23.0 used for coding",
                 "[1] All odds ratios, relative risks and risk differences are for comparing the Cabozantinib only arm to the Placebo arm",
                 "All point estimates and confidence intervals are from Proc Freq, odds ratio p-values from Proc Logistic, Relative risk p-values from Proc Genmod and Risk difference CMH p-value from proc Freq. P-values for the odds ratio and relative risk are not generated unless the event rate is at least 5% in both the treatment arms.",
                 "Source Data: ADSL, ADAE",
                 "Data Cut Date: 19AUG2020 Data Extraction Date: 13NOV2020",
                 "Source: Data transfer: 20210316"
    ),
    as_table = FALSE, #If TRUE, the footnotes will be inside the table
    # text_space_before = 5,
    text_font_size = 7,
    text_convert = FALSE #Tell R don't interpret special characters
  ) %>%
  rtf_title(title = c(
    "Table 14.3.1.5.7.1",
    "Subject Incidence of Treatment-Emergent Adverse Events by Preferred Term with Odds Ratio, Relative Risk and Risk Difference",
    "(sorted by Descending Risk Difference)",
    "Population: Safety"
  ),
  text_font_size = 9
  ) %>%
  # rtf_subline(text= subline) %>%
  rtf_colheader(colheader = col_header,
                col_rel_width = col_width,
                border_left = "",
                border_right = "",
                border_bottom = "",
                text_font_size=8 ) %>%
  rtf_body(col_rel_width = col_width,
           text_justification = c('l',rep('c',5)),
           border_left = "",
           border_right = "",
           text_font_size=8) %>%
  rtf_encode(page_footnote = 'all') %>%
  write_rtf(paste0("test1",".rtf"))

Output:

image

elong0527 commented 1 year ago

I am not able to reproduce the issue. The test has all been passed in posit cloud.

The reference link mentioned a upload-snapshots parameter, would you think test-package CI can also enable it to help us get more informative message from the output?

Alternatively, let me try to remove the snaps and see if it only apply to the new change.