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

Stop assemble_rtf applying header/footer from last .rtf to entire document #199

Closed pgseye closed 9 months ago

pgseye commented 9 months ago

I am finding it easier to create separate tables by group. I then assign the sequence of that table in the header - e.g. 1/3, 2/3, 3/3. What I have noticed when taking the 3 resulting .rtf's and using assemble__rtf is that the header from the last .rtf in line is then applied to all pages (so 3/3 shows in the header of all pages for the assembled document). Is there something I'm doing wrong or can I change this? Example code:

file <- c("table_output/tbl_14.2.1.1a.rtf",
          "table_output/tbl_14.2.1.1b.rtf",
          "table_output/tbl_14.2.1.1c.rtf")
assemble_rtf(input = file,
             output = "table_output/merged.rtf")
elong0527 commented 9 months ago

Could you provide a reproducible example?

pgseye commented 9 months ago

Sorry this code is long - I didn't know how to make it shorter to illustrate the problem. Hopefully it reproduces for you as well (otherwise it's just at my end).

library(dplyr)
library(r2rtf)

file_path <- getwd()

dat <- structure(list(time = c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 
                               5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 
                               0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 
                               8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 0, 0.5, 1, 1.5, 2, 2.5, 
                               3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 
                               11, 11.5, 12, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 
                               6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 0, 0.5, 
                               1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 
                               9, 9.5, 10, 10.5, 11, 11.5, 12, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 
                               4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 
                               11.5, 12), dv = c(0, 0.18432616, 0.88526902, 1.2296906, 1.28501176, 
                                                 1.2139172, 1.09455226, 0.96353803, 0.83722931, 0.72220134, 0.62040715, 
                                                 0.53169995, 0.45505514, 0.38915199, 0.33264156, 0.2842622, 0.24288196, 
                                                 0.20750707, 0.1772753, 0.15144348, 0.12937353, 0.11051874, 0.09441129, 
                                                 0.08065113, 0.06889633, 0, 0.18646195, 0.9048901, 1.28471715, 
                                                 1.37742791, 1.33879388, 1.24474175, 1.13178462, 1.01703554, 0.90810807, 
                                                 0.80800831, 0.71754717, 0.63652547, 0.56431222, 0.50012324, 0.44315229, 
                                                 0.39262987, 0.34784693, 0.30816176, 0.27299918, 0.24184632, 0.21424719, 
                                                 0.18979702, 0.16813683, 0.14894842, 0, 0.18294322, 0.87292448, 
                                                 1.19567138, 1.22915435, 1.14027058, 1.00823429, 0.8693954, 0.73934779, 
                                                 0.62380257, 0.52390843, 0.43883218, 0.36699104, 0.30662467, 0.25604644, 
                                                 0.21374115, 0.1783911, 0.14887033, 0.12422626, 0.10365757, 0.08649245, 
                                                 0.07216874, 0.06021662, 0.05024367, 0.0419223, 0, 0.18697047, 
                                                 0.90966337, 1.29828203, 1.40059765, 1.37067756, 1.2838258, 1.17643544, 
                                                 1.06571995, 0.95947803, 0.86092175, 0.77106034, 0.68987364, 0.61688687, 
                                                 0.55144953, 0.49286823, 0.44046784, 0.39361759, 0.35174019, 0.31431303, 
                                                 0.28086579, 0.25097654, 0.22426743, 0.20040041, 0.17907321, 0, 
                                                 0.18531778, 0.89429381, 1.25485411, 1.32695875, 1.27013735, 1.16158786, 
                                                 1.03796132, 0.91603064, 0.80290267, 0.7010534, 0.61080177, 0.53151718, 
                                                 0.46220211, 0.40176719, 0.34915557, 0.30339446, 0.26361157, 0.22903568, 
                                                 0.19899009, 0.17288363, 0.15020104, 0.13049386, 0.11337208, 0.09849667, 
                                                 0, 0.18383905, 0.88088906, 1.21756741, 1.26499344, 1.18736204, 
                                                 1.0632291, 0.92915041, 0.80123546, 0.68577026, 0.58443313, 0.49683915, 
                                                 0.42176711, 0.35773895, 0.30328282, 0.25704292, 0.21781669, 0.18455866, 
                                                 0.15636983, 0.13248206, 0.11224132, 0.09509189, 0.0805622, 0.06825233, 
                                                 0.05782327)), class = "data.frame", row.names = c(NA, -150L))

# Summarise
dat_summarised <- dat |> 
  group_by(time) |> 
  summarise(mean = mean(dv, na.rm = T), 
            sd = sd(dv, na.rm = T))

# Footer text
dt_time <- as.character(Sys.time())
foot_string <- paste0(file_path," / ",dt_time)

# Table 1
dat_summarised |> 
  rtf_page(orientation = "landscape",
           width = 11.7,
           height = 8.3,
           nrow = 60,
           margin = c(1,1,0.8,0.8,0.4,0.4),
           border_first = "single",
           border_last = "single") |> 
  rtf_page_header(text = c("Sponsor:                                                                                                                 Data Cut-Off: 2022-02-22",
                           "Protocol:                                                                                                                                   1 / 2"),
                  text_justification = c("l","l"),
                  text_indent_left = 400,
                  text_font = 9,
                  text_font_size = 7,
                  text_format = "b") |> 
  rtf_title(title = "Table 1",
            text_justification = "c",
            text_font = 9,
            text_font_size = 8,
            text_format = "b") |> 
  rtf_colheader(colheader = "Time | Mean | SD",
                col_rel_width = c(4,3, 3),
                text_justification = c("l","l", "l"),
                text_font = 9,
                text_font_size = 7,
                text_format = "b",
                border_top = rep("", 3),
                border_right = rep("", 3),
                border_left = rep("", 3),
                border_width = 25) |> 
  rtf_body(as_colheader = F,
           col_rel_width = c(4,3,3),
           text_justification = c("l","l","l"),
           text_font = 9,
           text_font_size = 7,
           border_first = rep("single", 3),
           border_last = rep("single", 3),
           border_left = rep("", 3),
           border_right = rep("", 3),
           border_width = 25,
           last_row = F) |> 
  rtf_footnote(footnote = c("",
                            ""),
               text_font = 9,
               text_font_size = 7,
               border_left = "",
               border_right = "",
               border_bottom = "",
               border_color_bottom = "white") |> # can't seem to remove bottom border without coloring it out
  rtf_page_footer(text = foot_string,
                  text_justification = "c",
                  text_font = 9,
                  text_font_size = 7,
                  text_format = "b") |> 
  rtf_encode() |> 
  write_rtf("table_1.rtf")

# Table 2
dat_summarised |> 
  rtf_page(orientation = "landscape",
           width = 11.7,
           height = 8.3,
           nrow = 60,
           margin = c(1,1,0.8,0.8,0.4,0.4),
           border_first = "single",
           border_last = "single") |> 
  rtf_page_header(text = c("Sponsor:                                                                                                                 Data Cut-Off: 2022-02-22",
                           "Protocol:                                                                                                                                   2 / 2"),
                  text_justification = c("l","l"),
                  text_indent_left = 400,
                  text_font = 9,
                  text_font_size = 7,
                  text_format = "b") |> 
  rtf_title(title = "Table 2",
            text_justification = "c",
            text_font = 9,
            text_font_size = 8,
            text_format = "b") |> 
  rtf_colheader(colheader = "Time | Mean | SD",
                col_rel_width = c(4,3, 3),
                text_justification = c("l","l", "l"),
                text_font = 9,
                text_font_size = 7,
                text_format = "b",
                border_top = rep("", 3),
                border_right = rep("", 3),
                border_left = rep("", 3),
                border_width = 25) |> 
  rtf_body(as_colheader = F,
           col_rel_width = c(4,3,3),
           text_justification = c("l","l","l"),
           text_font = 9,
           text_font_size = 7,
           border_first = rep("single", 3),
           border_last = rep("single", 3),
           border_left = rep("", 3),
           border_right = rep("", 3),
           border_width = 25,
           last_row = F) |> 
  rtf_footnote(footnote = c("",
                            ""),
               text_font = 9,
               text_font_size = 7,
               border_left = "",
               border_right = "",
               border_bottom = "",
               border_color_bottom = "white") |> # can't seem to remove bottom border without coloring it out
  rtf_page_footer(text = foot_string,
                  text_justification = "c",
                  text_font = 9,
                  text_font_size = 7,
                  text_format = "b") |> 
  rtf_encode() |> 
  write_rtf("table_2.rtf")

# Merge
file <- c("table_1.rtf",
          "table_2.rtf")
assemble_rtf(input = file,
             output = "merged.rtf")
elong0527 commented 9 months ago

Please use two special variable \\pagenumber and \\pagefield in the page header to indicate page number and total page.

You can find example from the default value of the rtf_page_header function.

pgseye commented 9 months ago

Thanks - that works.

pgseye commented 9 months ago

Actually, this doesn't do quite what I was hoping. I was wanting the page numbering to reflect a continuation of a single table split over multiple pages. I can only seem to achieve that if I use assemble_rtf just on those pages belonging to each table, then join them manually in Word. Otherwise, \\pagenumber and \\pagefield will continue the numbering (which I guess makes sense for its intended purpose).

elong0527 commented 9 months ago

I am not sure RTF file actually support this feature of "section breaker" in Word to allow you to have different page header or page footer.

If you have a RTF file that enable this feature, I can take a look the source code how it is achieved.

Otherwise, it might be better to handle it in Word as you described. 'assemble_docx' may help you in this case with details described in https://r4csr.org/tlf-assemble.html#using-toggle-fields

pgseye commented 9 months ago

Thank you