### Install r2rtf from GitHub Source: https://merck.github.io/r2rtf/index.html Install the r2rtf package directly from its GitHub repository. ```r remotes::install_github("Merck/r2rtf") ``` -------------------------------- ### Minimal RTF File Creation Example Source: https://merck.github.io/r2rtf/articles/design-pattern.html Combines various RTF components to create a minimal RTF file. This example demonstrates initializing RTF, setting fonts, pages, margins, and tables. ```R tbl <- iris[1:2, ] %>% rtf_body() paste( r2rtf:::as_rtf_init(), r2rtf:::as_rtf_font(), r2rtf:::as_rtf_page(tbl), r2rtf:::as_rtf_margin(tbl), r2rtf:::as_rtf_table(tbl), "}", sep = "\n" ) %>% cat() #> { tf1\ansi #> \deff0\deflang1033 #> {\fonttbl{\f0\froman\fcharset1\fprq2 Times New Roman;} #> {\f1\froman\fcharset161\fprq2 Times New Roman Greek;} #> {\f2\fswiss\fcharset161\fprq2 Arial Greek;} #> {\f3\fswiss\fcharset0\fprq2 Arial;} #> {\f4\fswiss\fcharset1\fprq2 Helvetica;} #> {\f5\fswiss\fcharset1\fprq2 Calibri;} #> {\f6\froman\fcharset1\fprq2 Georgia;} #> {\f7\ffroman\fcharset1\fprq2 Cambria;} #> {\f8\fmodern\fcharset0\fprq2 Courier New;} #> {\f9\ftech\fcharset2\fprq2 Symbol;} #> } #> #> \paperw12240\paperh15840 #> #> \margl1800\margr1440\margt2520\margb1800\headery2520\footery1449 #> #> \trowd\trgaph108\trleft0\trqc #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrw15\clvertalt\cellx1800 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx3600 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx5400 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx7200 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrr\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx9000 #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 5.1}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 3.5}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 1.4}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 0.2}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 setosa}\cell #> \intbl\row\pard #> } { tf1\ansi #> \deff0\deflang1033 #> {\fonttbl{\f0\froman\fcharset1\fprq2 Times New Roman;} #> {\f1\froman\fcharset161\fprq2 Times New Roman Greek;} #> {\f2\fswiss\fcharset161\fprq2 Arial Greek;} #> {\f3\fswiss\fcharset0\fprq2 Arial;} #> {\f4\fswiss\fcharset1\fprq2 Helvetica;} #> {\f5\fswiss\fcharset1\fprq2 Calibri;} #> {\f6\froman\fcharset1\fprq2 Georgia;} #> {\f7\ffroman\fcharset1\fprq2 Cambria;} #> {\f8\fmodern\fcharset0\fprq2 Courier New;} #> {\f9\ftech\fcharset2\fprq2 Symbol;} #> } #> #> \paperw12240\paperh15840 #> #> \margl1800\margr1440\margt2520\margb1800\headery2520\footery1449 #> #> \trowd\trgaph108\trleft0\trqc #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx1800 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx3600 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx5400 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx7200 #> \clbrdrl\brdrs\brdrw15\clbrdrt\brdrs\brdrw15\clbrdrr\brdrs\brdrw15\clbrdrb\brdrs\brdrw15\clvertalt\cellx9000 #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 4.9}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 3}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 1.4}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 0.2}\cell #> \pard\hyphpar0\sb15\sa15\fi0\li0\ri0\qc\fs18{\f0 setosa}\cell #> \intbl\row\pard #> } ``` -------------------------------- ### Create Sample Text for RTF Source: https://merck.github.io/r2rtf/articles/rtf-text.html Generates a long string of repeated text for use in RTF examples. This is a setup step for demonstrating text formatting. ```R text <- paste(rep("Sample Text", 20), collapse = " ") ``` ```R text <- rep(text, 5) ``` -------------------------------- ### Install r2rtf from CRAN Source: https://merck.github.io/r2rtf/index.html Install the r2rtf package from the CRAN repository. ```r install.packages("r2rtf") ``` -------------------------------- ### Example: Embedding and Attributing a PNG Figure Source: https://merck.github.io/r2rtf/reference/rtf_figure.html Demonstrates how to create a PNG file, read it using rtf_read_figure, and then apply figure attributes using rtf_figure. This example requires the dplyr package and is designed to be run in a non-interactive session. ```R if (FALSE) { # \dontrun{ library(dplyr) # required to run examples file <- file.path(tempdir(), "figure1.png") png(file) plot(1:10) dev.off() # Read in PNG file in binary format rtf_read_figure(file) %>% rtf_figure() %>% attributes() } # } ``` -------------------------------- ### Example of rtf_page with Data Source: https://merck.github.io/r2rtf/reference/rtf_page.html An example demonstrating how to use the rtf_page function with a sample data frame and retrieve the page attributes. Requires the dplyr library. ```r library(dplyr) # required to run examples data(r2rtf_tbl1) r2rtf_tbl1 %>% rtf_page() %>% attr("page") #> $width #> [1] 8.5 #> #> $height #> [1] 11 #> #> $orientation #> [1] "portrait" #> #> $margin #> [1] 1.25000 1.00000 1.75000 1.25000 1.75000 1.00625 #> #> $nrow #> [1] 40 #> #> $col_width #> [1] 6.25 #> #> $border_first #> [1] "double" #> #> $border_last #> [1] "double" #> #> $page_title #> [1] "all" #> #> $page_footnote #> [1] "last" #> #> $page_source #> [1] "last" #> #> $use_color #> [1] FALSE #> #> $use_i18n #> [1] FALSE #> ``` -------------------------------- ### Example Usage of rtf_encode Source: https://merck.github.io/r2rtf/reference/rtf_encode.html Demonstrates how to use rtf_encode with tables and figures, including writing the output to RTF files. ```APIDOC ## Examples ### Example 1: Encoding a table ```R library(dplyr) # required to run examples head(iris) %>% rtf_body() %>% rtf_encode() %>% write_rtf(file = file.path(tempdir(), "table1.rtf")) ``` ### Example 2: Encoding a figure ```R if (FALSE) { # \dontrun{ library(dplyr) # required to run examples file <- file.path(tempdir(), "figure1.png") png(file) plot(1:10) dev.off() # Read in PNG file in binary format rtf_read_figure(file) %>% rtf_figure() %>% rtf_encode(doc_type = "figure") %>% write_rtf(file = file.path(tempdir(), "figure1.rtf")) } # } ``` ### Example 3: Encoding a table with column headers and footnotes ```R # convert tbl_1 to the table body. Add title, subtitle, two table ## headers, and footnotes to the table body. data(r2rtf_tbl2) ## convert r2rtf_tbl2 to the table body. Add a table column header to table body. t2 <- r2rtf_tbl2 %>% rtf_colheader( colheader = "Pairwise Comparison |\n Difference in LS Mean(95% CI)\\dagger | p-Value", text_justification = c("l", "c", "c") ) %>% rtf_body( col_rel_width = c(8, 7, 5), text_justification = c("l", "c", "c"), last_row = FALSE ) # concatenate a list of table and save to an RTF file t2 %>% rtf_encode() %>% write_rtf(file.path(tempdir(), "table2.rtf")) ``` ``` -------------------------------- ### RTF Encoding Initialization with as_rtf_init() Source: https://merck.github.io/r2rtf/articles/design-pattern.html Use `r2rtf:::as_rtf_init()` to start the RTF encoding process. It sets up the initial RTF document structure, defaulting to English. ```R r2rtf:::as_rtf_init() %>% cat() ``` -------------------------------- ### Example Data Source: https://merck.github.io/r2rtf/reference/index.html Provides access to several example datasets commonly used in clinical trial analysis, including efficacy, subject demographics, adverse events, and ANCOVA model results. ```APIDOC ## Example Data ### `r2rtf_HAMD17` An Efficacy Clinical Trial Data to Evaluate a Drug to Reduce Lower Back Pain. ### `r2rtf_adsl` A Subject Level Demographic Dataset. ### `r2rtf_adae` An Adverse Event Dataset. ### `r2rtf_tbl1` Within Group Results from an ANCOVA Model. ### `r2rtf_tbl2` Between Group Results from an ANCOVA Model. ### `r2rtf_tbl3` Root Mean Square Error from an ANCOVA model. ``` -------------------------------- ### Basic Table Creation with r2rtf Source: https://merck.github.io/r2rtf/index.html A simple example demonstrating the core workflow of r2rtf: adding attributes with `rtf_body`, converting to RTF encode with `rtf_encode`, and writing to a file with `write_rtf`. This requires the dplyr and r2rtf packages. ```r library(dplyr) library(r2rtf) head(iris) %>% rtf_body() %>% rtf_encode() %>% write_rtf(file = "ex-tbl.rtf") ``` -------------------------------- ### Create and Encode RTF Table Source: https://merck.github.io/r2rtf/articles/r2rtf.html A minimal example to create an RTF table from a data frame, encode it, and save it to a file. This involves piping the data through `rtf_body`, `rtf_encode`, and `write_rtf` functions. ```r head(tbl) %>% rtf_body() %>% # Step 1 Add table attributes rtf_encode() %>% # Step 2 Convert attributes to RTF encode write_rtf("rtf/intro-ae1.rtf") # Step 3 Write to a .rtf file ``` -------------------------------- ### Get Line Spacing Options Source: https://merck.github.io/r2rtf/articles/rtf-text.html Displays available line spacing options and their associated RTF codes. This is useful for understanding how to control vertical spacing between lines of text. ```R r2rtf:::spacing()[, 1:2] ``` -------------------------------- ### Basic r2rtf Data Conversion Source: https://merck.github.io/r2rtf/articles/r2rtf-cran.html This snippet demonstrates the core workflow of the r2rtf package: loading libraries, preparing data with dplyr, converting it to RTF format using rtf_body and rtf_encode, and finally writing the RTF output to a file. Ensure the dplyr and r2rtf libraries are installed and loaded. ```r library(dplyr) library(r2rtf) head(iris) %>% rtf_body() %>% # Step 1 Add attributes rtf_encode() %>% # Step 2 Convert attributes to RTF encode write_rtf(file = "ex-tbl.rtf") # Step 3 Write to a .rtf file ``` -------------------------------- ### Get supported figure formats Source: https://merck.github.io/r2rtf/articles/example-figure.html Display the supported figure formats and their corresponding RTF codes. ```r r2rtf:::fig_format() ``` -------------------------------- ### Load r2rtf_adsl Dataset Source: https://merck.github.io/r2rtf/reference/r2rtf_adsl.html This R code snippet shows how to access the r2rtf_adsl dataset. Ensure the r2rtf package is installed and loaded. ```r r2rtf_adsl ``` -------------------------------- ### Get Paragraph Justification Options Source: https://merck.github.io/r2rtf/articles/rtf-text.html Retrieves available paragraph justification types (left, center, right, justified) and their corresponding RTF codes. Use this to understand available alignment options. ```R r2rtf:::justification() %>% subset(type %in% c("l", "c", "r", "j")) ``` -------------------------------- ### Read PNG Figure into Binary Data Source: https://merck.github.io/r2rtf/reference/rtf_read_figure.html Reads a PNG image file into a binary data vector. Ensure the devEMF package is installed if you need to create EMF files. ```R if (FALSE) { # \dontrun{ # Read in PNG file in binary format file <- tempfile("figure", fileext = ".png") png(file) plot(1:10) dev.off() rtf_read_figure(file) # Read in EMF file in binary format library(devEMF) file <- tempfile("figure", fileext = ".emf") emf(file) plot(1:10) dev.off() rtf_read_figure(file) } # } ``` -------------------------------- ### Output RTF File Source: https://merck.github.io/r2rtf/articles/example-basechar.html This snippet demonstrates how to encode the rtf object and write it to an RTF file named 'bs_example.rtf' in the 'rtf/' directory. ```R # Output .rtf file bs_rtf %>% rtf_encode() %>% write_rtf("rtf/bs_example.rtf") ``` -------------------------------- ### Outputting Table to RTF File Source: https://merck.github.io/r2rtf/articles/example-efficacy.html This code demonstrates how to encode the combined table list into RTF format using rtf_encode() and then write it to a specified RTF file named 'efficacy_example.rtf'. ```r # Output .rtf file tbl %>% rtf_encode() %>% write_rtf("rtf/efficacy_example.rtf") ``` -------------------------------- ### Group by Feature in rtf_body Source: https://merck.github.io/r2rtf/articles/r2rtf.html Use the `group_by` argument in `rtf_body()` to display data once per group variable. This example groups by treatment group. ```r aegt5 %> rtf_title(title = "Top Adverse Events by Treatment Group") %> rtf_colheader(colheader = "Treatment|Adverse Events|Count") %> rtf_body( group_by = "TRTA", text_justification = c("l", "l", "c") ) %> rtf_encode() %> write_rtf("rtf/intro-ae18.rtf") ``` -------------------------------- ### Write RTF to HTML File Source: https://merck.github.io/r2rtf/reference/write_html.html Use this function to save an RTF encoding string as an HTML file. It requires LibreOffice to be installed and is only supported on Unix/Linux/macOS. ```R write_html(rtf, file) ``` -------------------------------- ### Encode and write a figure to RTF Source: https://merck.github.io/r2rtf/reference/rtf_encode.html Encodes a figure (read from a PNG file) into RTF format and writes it to a file. This example is wrapped in a \dontrun{} block. ```R if (FALSE) { # \dontrun{ library(dplyr) # required to run examples file <- file.path(tempdir(), "figure1.png") png(file) plot(1:10) dev.off() # Read in PNG file in binary format rtf_read_figure(file) %>% rtf_figure() %>% rtf_encode(doc_type = "figure") %>% write_rtf(file = file.path(tempdir(), "figure1.rtf")) } # } ``` -------------------------------- ### Display Sample of Prepared Data Source: https://merck.github.io/r2rtf/articles/example-sublineby-pageby-groupby.html Uses `knitr::kable` to display the first two rows of the prepared adverse event data (`ae_t1`), showing the structure of the data before RTF table creation. ```R knitr::kable(head(ae_t1, 2)) ``` -------------------------------- ### Add Page Break Before Paragraphs Source: https://merck.github.io/r2rtf/articles/rtf-text.html Inserts a page break before the specified paragraphs by setting `new_page = TRUE`. This is useful for starting new sections on a fresh page. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text)[1:2], new_page = TRUE ) r2rtf:::write_rtf_para(res, "rtf/para-page.rtf") ``` -------------------------------- ### Basic Table Creation with rtf_body() Source: https://merck.github.io/r2rtf/articles/design-pattern.html Use `rtf_body()` to create a table from a data frame. This function allows extensive customization of table appearance through parameters. ```R head(iris) %>% rtf_body() #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa ``` -------------------------------- ### Write RTF to DOCX using write_docx Source: https://merck.github.io/r2rtf/reference/write_docx.html Use this function to save an RTF encoding string directly to a .docx file. It requires LibreOffice to be installed and is supported on Unix/Linux/macOS systems. ```R write_docx(rtf, file) ``` -------------------------------- ### Prepare Text Formatting Matrix Source: https://merck.github.io/r2rtf/articles/rtf-row.html Creates a matrix to prepare text formatting for RTF output, combining empty strings with font format types. ```r fmt <- cbind("", r2rtf:::font_format()$type) fmt ``` -------------------------------- ### Convert RTF to DOCX using write_docx() Source: https://merck.github.io/r2rtf/articles/rtf-convert-format.html Use write_docx() to convert RTF encoding to a DOCX file. Ensure RTF encoding is created first using rtf_body() and rtf_encode(). ```r rtf <- head(iris) | rtf_body() | rtf_encode() write_docx(rtf, "output/table.docx") ``` -------------------------------- ### Example: Adding a Title to a Table Source: https://merck.github.io/r2rtf/reference/rtf_title.html Demonstrates how to use the rtf_title function to add a title to a data frame and retrieve the added attribute. Requires the dplyr package and the r2rtf_tbl1 dataset. ```R library(dplyr) # required to run examples data(r2rtf_tbl1) r2rtf_tbl1 %>% rtf_title(title = "ANCOVA of Change from Baseline at Week 8") %>% attr("rtf_title") #> [1] "ANCOVA of Change from Baseline at Week 8" #> attr(,"text_font") #> [1] 1 #> attr(,"text_font_size") #> [1] 12 #> attr(,"text_justification") #> [1] "c" #> attr(,"text_space") #> [1] 1 #> attr(,"text_space_before") #> [1] 180 #> attr(,"text_space_after") #> [1] 180 #> attr(,"text_indent_first") #> [1] 0 #> attr(,"text_indent_left") #> [1] 0 #> attr(,"text_indent_right") #> [1] 0 #> attr(,"text_new_page") #> [1] FALSE #> attr(,"text_hyphenation") #> [1] TRUE #> attr(,"text_convert") #> [1] TRUE #> attr(,"use_i18n") #> [1] FALSE #> attr(,"strwidth") #> [1] 3.07921 #> attr(,"use_color") #> [1] FALSE #> attr(,"class") #> [1] "character" "rtf_text" ``` -------------------------------- ### Customize Multi-line Column Headers in R2RTF Source: https://merck.github.io/r2rtf/articles/r2rtf.html Demonstrates how to create multi-line column headers and handle empty columns using `rtf_colheader`. It also shows how to apply different relative column widths for complex header structures. ```r head(tbl, 50) %>% rtf_colheader( colheader = " | Treatment", col_rel_width = c(3, 6) ) %>% rtf_colheader( colheader = "Adverse Events | Placebo | Xanomeline High Dose | Xanomeline Low Dose", col_rel_width = c(3, 2, 2, 2) ) %>% rtf_body(col_rel_width = c(3, 2, 2, 2)) %>% rtf_encode() %>% write_rtf("rtf/intro-ae4.rtf") ``` -------------------------------- ### Using rtf_source and Inspecting Attributes Source: https://merck.github.io/r2rtf/reference/rtf_source.html Demonstrates how to use the rtf_source function to add source information to a data frame and then how to access the attributes of the resulting object. This is useful for verifying that the source information and associated text formatting options have been correctly applied. ```R library(dplyr) # required to run examples data(r2rtf_tbl1) r2rtf_tbl1 %>% rtf_source("Source: [study999:adam-adeff]") %>% attr("rtf_source") #> [1] "Source: [study999:adam-adeff]" #> attr(,"text_font") #> [1] 1 #> attr(,"text_font_size") #> [1] 9 #> attr(,"text_justification") #> [1] "c" #> attr(,"text_space") #> [1] 1 #> attr(,"text_space_before") #> [1] 15 #> attr(,"text_space_after") #> [1] 15 #> attr(,"text_indent_first") #> [1] 0 #> attr(,"text_indent_left") #> [1] 0 #> attr(,"text_indent_right") #> [1] 0 #> attr(,"text_new_page") #> [1] FALSE #> attr(,"text_hyphenation") #> [1] TRUE #> attr(,"text_convert") #> [1] TRUE #> attr(,"use_i18n") #> [1] FALSE #> attr(,"strwidth") #> [1] 1.544922 #> attr(,"use_color") #> [1] FALSE #> attr(,"class") #> [1] "character" "rtf_text" #> attr(,"as_table") #> [1] FALSE #> attr(,"col_rel_width") #> [1] 1 ``` -------------------------------- ### Convert RTF to HTML using write_html() Source: https://merck.github.io/r2rtf/articles/rtf-convert-format.html Use write_html() to convert RTF encoding to an HTML file. Ensure RTF encoding is created first using rtf_body() and rtf_encode(). ```r rtf <- head(cars) | rtf_body() | rtf_encode() write_html(rtf, "output/table.html") ``` -------------------------------- ### Convert RTF to HTML using rtf_convert_format() Source: https://merck.github.io/r2rtf/articles/rtf-convert-format.html Specify the output format as 'html' using the format argument in rtf_convert_format() to convert RTF files to HTML. ```r r2rtf:::rtf_convert_format(input = "rtf/ae_example.rtf", format = "html") ``` -------------------------------- ### Apply Specific Cell Borders Source: https://merck.github.io/r2rtf/articles/rtf-row.html Applies different border styles to the top, left, and right of cells in an RTF table. The bottom border is applied to the last row. This example demonstrates granular control over cell borders. ```R iris %>% head() %>% rtf_body( as_colheader = FALSE, border_top = "dash", border_left = "double", border_right = "single" ) %>% rtf_encode() %>% write_rtf("rtf/border-order.rtf") ``` -------------------------------- ### Initiating a New Page with as_rtf_new_page() Source: https://merck.github.io/r2rtf/articles/design-pattern.html Use `r2rtf::as_rtf_new_page()` to insert RTF code that forces a new page in the document. This is useful for controlling document flow. ```R r2rtf::as_rtf_new_page() %>% cat() ``` -------------------------------- ### Define RTF Table Structure and Formatting Source: https://merck.github.io/r2rtf/articles/example-basechar.html This code defines the RTF table structure using r2rtf functions, including page setup, title, column headers, body, footnotes, and source information. The 'page_by' feature is used to separate sections by variable label. ```R bs_rtf <- bs_tb %>% rtf_page(width = 9.5) %>% rtf_title("Demographic and Anthropometric Characteristics", "ITT Subjects") %>% rtf_colheader(" | Placebo | Drug Low Dose | Drug High Dose | Total ", col_rel_width = c(3, rep(2, 4)) ) %>% rtf_colheader(" | n | (%) | n | (%) | n | (%) | n | (%) ", col_rel_width = c(3, rep(c(1.2, 0.8), 4)), border_top = c("", rep("single", 8)), border_left = c("single", rep(c("single", ""), 4)) ) %>% rtf_body( page_by = "var_label", col_rel_width = c(3, rep(c(1.2, 0.8), 4), 3), text_justification = c("l", rep("c", 8), "l"), text_format = c(rep("", 9), "b"), border_left = c("single", rep(c("single", ""), 4), "single"), border_top = c(rep("", 9), "single"), border_bottom = c(rep("", 9), "single") ) %>% rtf_footnote("This is a footnote") %>% rtf_source("Source: xxx") ``` -------------------------------- ### Load r2rtf and dplyr Libraries Source: https://merck.github.io/r2rtf/articles/example-ae-summary.html Loads the necessary libraries for data manipulation and RTF table creation. ```r library(r2rtf) library(dplyr) library(tidyr) ``` -------------------------------- ### Create RTF Table with r2rtf Source: https://merck.github.io/r2rtf/articles/design-pattern.html Summarizes the major steps to create a table from a data frame using the r2rtf package. Requires data frame preparation before piping into rtf functions. ```R head(iris) %> rtf_body() %> # Step 1 Add attributes rtf_encode() %> # Step 2 Convert attributes to RTF encode write_rtf(file = "rtf/ex-tbl.rtf") # Step 3 Write to a .rtf file ``` -------------------------------- ### Separate RTF Table Pages by Group Source: https://merck.github.io/r2rtf/articles/r2rtf.html Generates an RTF table with a title and column headers, using the `page_by` argument to separate rows into different pages based on the 'TRTA' variable. `new_page = TRUE` ensures each 'TRTA' value starts on a new page. Saves the output to an RTF file. ```R aegt5 %>% rtf_title(title = "Top Adverse Events by Treatment Group") %>% rtf_colheader(colheader = "Adverse Events|Count") %>% rtf_body( page_by = "TRTA", new_page = TRUE ) %>% rtf_encode() %>% write_rtf("rtf/intro-ae17.rtf") ``` -------------------------------- ### assemble_rtf Source: https://merck.github.io/r2rtf/reference/assemble_rtf.html Assembles multiple RTF table, listing, and figure files into a single RTF document. ```APIDOC ## assemble_rtf ### Description Assembles multiple RTF table, listing, and figure files into a single RTF document. ### Usage ``` assemble_rtf(input, output, landscape = FALSE) ``` ### Arguments * **input** (Character vector) - Character vector of file paths to the RTF files to be assembled. * **output** (Character string) - The file path for the output RTF document. * **landscape** (Logical) - Determines if the page orientation should be landscape. Defaults to FALSE (portrait). ### Examples ```R library(magrittr) file <- replicate(2, tempfile(fileext = ".rtf")) file1 <- head(iris) %>% rtf_body() %>% rtf_encode() %>% write_rtf(file[1]) file2 <- head(cars) %>% rtf_page(orientation = "landscape") %>% rtf_body() %>% rtf_encode() %>% write_rtf(file[2]) output <- tempfile(fileext = ".rtf") assemble_rtf( input = file, output = output ) ``` ``` -------------------------------- ### Set Line Spacing in Paragraphs Source: https://merck.github.io/r2rtf/articles/rtf-text.html Demonstrates how to set different line spacing values (single, double, 1.5) within a paragraph. The `space` argument accepts a numeric vector. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text), space = c(1, 2, 1.5) ) r2rtf:::write_rtf_para(res, "rtf/para-line-space.rtf") ``` -------------------------------- ### Utility Functions Source: https://merck.github.io/r2rtf/reference/index.html Utility functions for text encoding and image file handling, including converting text to formatted RTF, reading PNG files, and converting UTF-8 strings to RTF encoded strings. ```APIDOC ## Utility ### `rtf_rich_text()` Text to Formatted RTF Encode. ### `rtf_read_png()` Read PNG Figures into Binary Files. ### `utf8Tortf()` Convert a UTF-8 Encoded Character String to a RTF Encoded String. ``` -------------------------------- ### Load Required Libraries Source: https://merck.github.io/r2rtf/articles/example-efficacy.html Loads the necessary R libraries for data manipulation, analysis, and table generation. ```r library(r2rtf) library(dplyr) library(tidyr) library(emmeans) ``` -------------------------------- ### Batch Convert Multiple RTF files to PDF Source: https://merck.github.io/r2rtf/articles/rtf-convert-format.html Process multiple RTF files for conversion to PDF by providing a list of filenames to the input argument of rtf_convert_format(). ```r r2rtf:::rtf_convert_format(input = list.files("rtf", pattern = "*.rtf")) ``` -------------------------------- ### Load HAMD17 Dataset Source: https://merck.github.io/r2rtf/reference/r2rtf_HAMD17.html This snippet shows how to access the HAMD17 dataset. No specific imports are required as it's a built-in dataset. ```r r2rtf_HAMD17 ``` -------------------------------- ### Combine Text Formatting (Single Call) Source: https://merck.github.io/r2rtf/articles/rtf-text.html Combines text elements with varied formatting in a single `rtf_text` call by providing vectors for `text`, `format`, `color`, and `background_color`. This is efficient for applying consistent styles across multiple text parts. ```R text <- c(3.5, "\dagger", "\line red ", "highlight") format <- c("", "^", "", "") color <- c("black", "black", "red", "black") background_color <- c("white", "white", "white", "yellow") res <- r2rtf:::rtf_paragraph( paste( r2rtf:::rtf_text(text, format = format, color = color, background_color = background_color ), collapse = "" ) ) r2rtf:::write_rtf_para(res, "rtf/text-combine2.rtf") ``` -------------------------------- ### Apply Text Formatting Source: https://merck.github.io/r2rtf/articles/rtf-text.html Applies various text formats like bold, italic, underline, strikethrough, and superscript/subscript. The `format` argument takes a character vector of format codes. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text, format = c("b", "i", "bi", "^", "_", "u", "s") )) ``` ```R r2rtf:::write_rtf_para(res, "rtf/text-format-1.rtf") ``` -------------------------------- ### Set Text Background Color Source: https://merck.github.io/r2rtf/articles/rtf-text.html Applies background colors to the text. The `background_color` argument works similarly to the `color` argument. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text, color = "white", background_color = c("red", "gold", "black", "orange", "blue") )) r2rtf:::write_rtf_para(res, "rtf/text-background-color.rtf") ``` -------------------------------- ### assemble_docx Source: https://merck.github.io/r2rtf/reference/assemble_docx.html Assembles multiple RTF table listings and figures into a single Word document (docx). ```APIDOC ## assemble_docx ### Description Assembles multiple RTF table, listing, and figures into one document as Microsoft Word (i.e., docx). ### Usage ``` assemble_docx(input, output, landscape = FALSE) ``` ### Arguments * **input** (Character vector) - Vector of file paths for the RTF files to be assembled. * **output** (Character string) - The file path for the output DOCX file. * **landscape** (Logical vector) - Determines the page orientation. Defaults to FALSE (portrait). ### Examples ```R library(officer) library(magrittr) file <- replicate(2, tempfile(fileext = ".rtf")) file1 <- head(iris) %>% rtf_body() %>% rtf_encode() %>% write_rtf(file[1]) file2 <- head(cars) %>% rtf_page(orientation = "landscape") %>% rtf_body() %>% rtf_encode() %>% write_rtf(file[2]) output <- tempfile(fileext = ".docx") assemble_docx( input = file, output = output ) ``` ``` -------------------------------- ### Paginate Table by Variable Source: https://merck.github.io/r2rtf/articles/design-pattern.html Demonstrates how to paginate a table by a specific variable (e.g., 'Species') using `rtf_body()` with the `page_by` argument. This pattern is useful for grouping and presenting data in distinct sections within an RTF document. Ensure the input data frame is sorted by the `page_by` variable before applying this pattern. ```r iris %>% arrange(Species) %>% rtf_colheader("Sepal Length | Sepal Width | Petal Length | PetalWidth", ) %>% rtf_body( page_by = c("Species"), border_top = c(rep("", 4), c("single")), border_bottom = c(rep("", 4), c("single")), text_justification = c(rep("c", 4), c("l")), new_page = TRUE ) %>% rtf_encode() %>% write_rtf("pageby.rtf") ``` -------------------------------- ### write_docx Source: https://merck.github.io/r2rtf/reference/write_docx.html Writes an RTF encoding string to a .docx file by first creating a temporary RTF file and then converting it to DOCX using LibreOffice. ```APIDOC ## write_docx ### Description Writes an RTF encoding string to a .docx file by first writing to a temporary RTF file and then converting it to DOCX using LibreOffice. ### Usage ``` write_docx(rtf, file) ``` ### Arguments * **rtf** (character) - A character rtf encoding string rendered by `rtf_encode()`. * **file** (character) - A character string naming a file to save docx file. ### Details This function requires LibreOffice to be installed on the system. The function uses the internal `rtf_convert_format()` function to perform the conversion from RTF to DOCX format. Currently only Unix/Linux/macOS systems are supported. ``` -------------------------------- ### Apply Paragraph Justification Source: https://merck.github.io/r2rtf/articles/rtf-text.html Applies different text justifications (left, center, right, justified) to sample text and saves the result to an RTF file. This demonstrates how to control paragraph alignment. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text), justification = c("l", "c", "r", "j") ) r2rtf:::write_rtf_para(res, "rtf/para-justification.rtf") ``` -------------------------------- ### Displaying a Table with knitr Source: https://merck.github.io/r2rtf/articles/example-efficacy.html This snippet shows how to display a table object using the knitr::kable function, likely for previewing or debugging. ```r knitr::kable(t3) ``` -------------------------------- ### Convert RTF to PDF using rtf_convert_format() Source: https://merck.github.io/r2rtf/articles/rtf-convert-format.html Use the internal rtf_convert_format() function for batch RTF to PDF conversion. The output PDF will be saved in the same directory with the same filename by default. ```r r2rtf:::rtf_convert_format(input = "rtf/ae_example.rtf") ``` -------------------------------- ### Generate RTF Table with Column Headers and Body Source: https://merck.github.io/r2rtf/articles/r2rtf.html Use this function to create an RTF table with specified column headers and body content. It allows for custom column widths and border styles. The output is written to an RTF file. ```R head(tbl) %>% rtf_colheader( colheader = " | Treatment", col_rel_width = c(3, 6) ) %>% rtf_colheader( colheader = "Adverse Events | Placebo | Xanomeline High Dose | Xanomeline Low Dose", border_top = c("", "single", "single", "single"), col_rel_width = c(3, 2, 2, 2) ) %>% rtf_body(col_rel_width = c(3, 2, 2, 2)) %>% rtf_encode() %>% write_rtf("rtf/intro-ae7.rtf") ``` -------------------------------- ### View Text Justification Options in R2RTF Source: https://merck.github.io/r2rtf/articles/r2rtf.html This snippet displays the available text justification options provided by the `r2rtf` package. Use these options with `text_justification` argument in `rtf_*()` functions. ```r r2rtf:::justification() ``` -------------------------------- ### Create Table with Double Line Borders Source: https://merck.github.io/r2rtf/articles/rtf-row.html Generates an RTF table with specific border styles, using double lines for the first and last rows and blank borders for the middle rows. The output is saved as an RTF file. ```R rtf_db <- iris %>% head() %>% rtf_title("Border Using Double Line at First/Last Line") %>% rtf_colheader("Sepal Length | Sepal Width | Petal Length | Petal Width | Species") %>% rtf_body() rtf_db %>% rtf_encode() %>% write_rtf("rtf/border-example.rtf") ``` -------------------------------- ### Set Paragraph Spacing Source: https://merck.github.io/r2rtf/articles/rtf-text.html Controls the space before and after paragraphs using `space_before` and `space_after` arguments. Values are in points. ```R res <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(text), space_before = c(50, 180), space_after = c(180, 50) ) r2rtf:::write_rtf_para(res, "rtf/para-space.rtf") ``` -------------------------------- ### Define RTF Table Format with Pageby Source: https://merck.github.io/r2rtf/articles/example-sublineby-pageby-groupby.html Formats the prepared data into an RTF table using rtf_page, rtf_title, rtf_colheader, rtf_body, rtf_footnote, and rtf_source. It configures page orientation, titles, column headers, body text justification, and page-by functionality. ```R apr0ae_rtf <- apr0ae %>% rtf_page(orientation = "landscape") %>% rtf_title(c("Participants with Severe Adverse Events", "(Incidence \\geq 0% in One or More Treatment Groups)"), "(ASaT Population)") %>% rtf_colheader(" | Placebo | Xanomeline Low Dose| Xanomeline High Dose ", col_rel_width = c(5, 2, 2, 2), border_bottom = c("", rep("single", 3)) ) %>% rtf_colheader(" | n | n | n ", border_top = c("", rep("single", 3)), border_bottom = "single", col_rel_width = c(5, rep(2, 3)) ) %>% rtf_body( col_rel_width = c(5, rep(2, 3), 1), text_justification = c("l", "c", "c", "c", "l"), border_first = "", page_by = "AEBODSYS", pageby_row = "first_row", ) %>% rtf_footnote(c("This is footnote")) %>% rtf_source("Source: [Study CDISCPILOT01: adam-adae]") ``` -------------------------------- ### write_html Source: https://merck.github.io/r2rtf/reference/write_html.html Writes an RTF encoding string to an HTML file by converting a temporary RTF file to HTML using LibreOffice. ```APIDOC ## write_html ### Description Writes an RTF encoding string to an HTML file by first writing to a temporary RTF file and then converting it to HTML using LibreOffice. This function requires LibreOffice to be installed on the system and currently only supports Unix/Linux/macOS systems. ### Arguments * **rtf** (character) - A character rtf encoding string rendered by `rtf_encode()`. * **file** (character) - A character string naming a file to save the HTML file. ```