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

Incorrect RTF output using both page_by & group_by args. of rtf_body() #168

Closed GlicketySplit closed 1 year ago

GlicketySplit commented 1 year ago

Description

When page_by & group_by are both specified, the table is ordered by the group_by variable(s) throughout the table instead of within the page_by variable. Rows within each page_by section of the table do not correspond with the page_by value of the original data.

Expected behavior

My assumption is that the table would be arranged by the page_by variable then by the group_by variable(s) within each page_by section.

Reproducible code

library(r2rtf)

dat <- structure(list(inline_text = c("Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 78381 ; Age (Years): 80 ; Sex: Female ; Race: Black", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 92121 ; Age (Years): 18 ; Sex: Female ; Race: White", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other", 
                                      "Participant ID: 68154 ; Age (Years): 3 ; Sex: Female ; Race: Other"), 
                      Date = structure(c(19490, 19493, 19489, 19493, 19491, 19491, 
                                         19491, 19493, 19491, 19491, 19493, 19492, 
                                         19489, 19488, 19491, 19490, 19491, 19490), 
                                       class = "Date"), 
                      Location = c("Hospital", "Clinic", "Hospital", "Hospital", "Hospital", "Clinic", 
                                   "Clinic", "Hospital", "Hospital", "Hospital", "Clinic", "Hospital", 
                                   "Clinic", "Hospital", "Hospital", "Hospital", "Hospital", "Clinic"), 
                      VisitID = c(95267L, 14049L, 9595L, 74009L, 7846L, 25473L, 
                                  83169L, 61604L, 80932L, 86098L, 81671L, 18930L, 
                                  18377L, 33893L, 48587L, 31230L, 29311L, 37127L), 
                      System = c("Muscular", "Digestive", "Nervous", "Nervous", "Urinary", "Digestive", 
                                 "Nervous", "Urinary", "Respiratory", "Circulatory", "Nervous", "Circulatory", 
                                 "Respiratory", "Circulatory", "Respiratory", "Urinary", "Respiratory", "Digestive"), 
                      Symptoms = c("Asymptomatic", "Symptomatic", "Unknown", "Unknown", "Symptomatic", "Not applicable", 
                                   "Asymptomatic", "Symptomatic", "Symptomatic", "Symptomatic", "Unknown", "Symptoms resolved", 
                                   "Not applicable", "Not applicable", "Unknown", "Not applicable", "Asymptomatic", "Unknown")), 
                 row.names = c(NA, -18L), 
                 class = "data.frame")

x <- dat %>%
  arrange(inline_text, Date) %>% 
  mutate(inline_text = paste(" \\line",inline_text)) %>% 
  rtf_page(orientation = "landscape",
           margin = c(1,1,1,1,1,1),
           nrow = 30,
           border_first = 'single',
           border_last =  'single') %>% 
  rtf_colheader(paste(names(dat[,-1]), collapse = " | "),
                col_rel_width = rep(3,5),
                text_justification = "c",
                text_font = 9,
                border_top = "single",
                border_bottom = 'single',
                border_left = NULL,
                border_right = NULL
  )  %>%
  rtf_body(
    col_rel_width = rep(3, ncol(dat)),
    text_font = 9,
    border_left = NULL,
    border_right = NULL,
    border_first = "",
    border_last = "single",
    page_by = 'inline_text',
    pageby_header = FALSE,
    group_by = 'Date',
    border_bottom = "",
    border_top = "",
    as_colheader = FALSE,
    text_convert = FALSE
  ) 

x %>% rtf_encode() %>%
  write_rtf("example2.rtf")

Screenshots

Current output:

current_marked

^ rows are highlighted corresponding to their respective Participant ID / inline_text

Expected output:

image

Session Info ``` R version 4.2.2 (2022-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044) ```
elong0527 commented 1 year ago

Fixed in #170