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

Page size of the first page is different from the other pages #174

Closed Chuck1111 closed 1 year ago

Chuck1111 commented 1 year ago

Describe the bug In some of the tables I generated using r2rtf, the first page has a different nrow from the other pages. In the example below, when I set nrow = 25, the page 1 has more blank rows at the bottom while the other pages fit. But if you change the nrow from 25 to 26, the first page will fit while the other pages will split into two pages.

Expected behavior How can we have the nrow consistent among different pages?

To reproduce

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 = ""))
}

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 = 25
  ) %>%
  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 = " |Treatment 1| |Oddsd Ratio [1]|Relative Risk [1]|Risk Difference [1]",
                col_rel_width = c(5,3,3,3,3,3),
                border_left = "",
                border_right = "",
                border_bottom = "",
                text_font_size=8 ) %>%
  rtf_colheader(colheader = " |Only|Placebo|(95% CI)|(95% CI)|(95% CI)",
                col_rel_width = c(5,3,3,3,3,3),
                border_left = "",
                border_right = "",
                border_top = "",
                text_font_size=8 ) %>%
  rtf_colheader(colheader = glue('Preferred Term|(N={n_trt})|(N={n_placebo})|p-value|p-value|p-value'),
                col_rel_width = c(5,3,3,3,3,3),
                border_top = "",
                border_left = "",
                border_right = "",
                text_justification = c('l',rep('c',5)),
                text_font_size=8) %>%
  rtf_body(col_rel_width = c(5,3,3,3,3,3),
           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"))

Created on 2023-06-15 with reprex v2.0.2

fb-elong commented 1 year ago

Got it, yes it is a bug and be fixed by #175.

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

Note: there is no need to use multiple rtf_colheader. You can use \n to get multiple lines as shown in #175.