Learn R Programming

⚠️There's a newer version (1.1.3) of this package.Take me there.

r2rtf

Overview

r2rtf is an R package to create production-ready tables and figures in RTF format. The package is designed with these principles:

  • Provide simple “verb” functions that correspond to each component of a table, to help you translate data frame(s) to a table in RTF file.
  • Functions are chainable with pipes (%>%).
  • Only focus on table format.
    • Data manipulation and analysis should be handled by other R packages, for example, tidyverse.
  • Minimize package dependency.

The R for clinical study reports and submission book provides tutorials by using real world examples.

Installation

You can install the package via CRAN:

install.packages("r2rtf")

Or, install from GitHub:

remotes::install_github("Merck/r2rtf")

Highlighted features

The R package r2rtf provides flexibility to enable features below:

  • Create highly customized RTF table and figure ready for production.
  • Simple to use parameters and data structure.
    • Customized column header: split by "|".
    • Three required parameters for the output tables (data, filename, column relative width).
    • Flexible and detail control of table structure.
  • Format control in cell, row, column and table level for:
    • Border Type: single, double, dash, dot, etc.
    • Alignment: left, right, center, decimal.
    • Column width.
    • Text appearance: bold, italics, strikethrough, underline and any combinations.
    • Font size.
    • Text and border color (657 different colors named in color() function).
    • Special characters: any character in UTF-8 encoding (e.g., Greek, Symbol, Chinese, Japanese, Korean).
  • Append several tables into one file.
  • Pagination.
  • Built-in raw data for validation.

Simple example

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

Example efficacy table

Example safety table

Citation

If you use this software, please cite it as below.

Wang, S., Ye, S., Anderson, K., & Zhang, Y. (2020). r2rtf—an R Package to Produce Rich Text Format (RTF) Tables and Figures. PharmaSUG. https://www.pharmasug.org/proceedings/2020/DV/PharmaSUG-2020-DV-198.pdf

A BibTeX entry for LaTeX users is

@inproceedings{wang2020r2rtf,
  title     = {{r2rtf}---an {R} Package to Produce {Rich Text Format} ({RTF}) Tables and Figures},
  author    = {Wang, Siruo and Ye, Simiao and Anderson, Keaven M and Zhang, Yilong},
  booktitle = {PharmaSUG},
  year      = {2020},
  url       = {https://www.pharmasug.org/proceedings/2020/DV/PharmaSUG-2020-DV-198.pdf}
}

Copy Link

Version

Install

install.packages('r2rtf')

Monthly Downloads

4,520

Version

1.0.2

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Yilong Zhang

Last Published

May 1st, 2023

Functions in r2rtf (1.0.2)

fig_format

RTF Text Format Dictionary
inch_to_twip

Convert Inches to Twips
as_rtf_subline

Create Table Subline RTF Encode
match_arg

Argument Verification Using Partial Matching
color_table

RTF Text Color Dictionary
nrow_table

Calculate Number of Lines Broken to for Each Table Row
r2rtf_tbl1

Within Group Results from an ANCOVA Model
obj_rtf_border

Create an RTF Table Border Object
nrow_paragraph

Calculate Number of Rows for a Paragraph
border_type

RTF Border Type Dictionary
assemble_rtf

Assemble Multiple RTF Table Listing and Figure Into One RTF Document
convert

Convert Symbol to ANSI and Unicode Encoding
rtf_body

Add Table Body Attributes to the Table
r2rtf_tbl2

Between Group Results from an ANCOVA Model
rtf_encode_figure

Render Figure to RTF Encoding
as_rtf_table

Combine RTF Table Encoding
r2rtf_HAMD17

An Efficacy Clinical Trial Data to Evaluate a Drug to Reduce Lower Back Pain
r2rtf_tbl3

Root Mean Square Error from an ANCOVA model
rtf_figure

Add Figure Attributes
font_format

RTF Text Format Dictionary
obj_rtf_text

Create an RTF Text Object
rtf_encode_list

Render List to RTF Encoding
rtf_encode_table

Render Table to RTF Encoding
rtf_pageby

Add Pageby Attributes to Table
justification

RTF Text Justification Dictionary
rtf_by_subline

Add Sublineby Attributes to Table
r2rtf_adsl

A Subject Level Demographic Dataset
rtf_read_figure

Read Figures into Binary Files
rtf_group_by_enhance

Remove Duplicate Records
r2rtf_adae

An Adverse Event Dataset
rtf_strwidth

Calculate String Width in Inches
rtf_source

Add Data Source Attributes to the Table
rtf_colheader

Add Column Header Attributes to Table
rtf_footnote

Add Footnote Attributes to Table
rtf_convert_format

Convert RTF file to Other Format
rtf_encode

Render to RTF Encoding
update_border_first

Update First Border Line Based on Page Information
rtf_read_png

Read PNG Figures into Binary Files
spacing

RTF Paragraph Spacing Dictionary
rtf_nline_matrix

Calculate Number of Lines of a String Matrix
rtf_page_footer

Add RTF Page Footer Information
rtf_page_header

Add RTF Page Header Information
rtf_nline_vector

Calculate Number of Lines of a String Vector
rtf_paragraph

Paragraph to RTF Encode
rtf_table_content

Create RTF Table Body Encode
write_rtf

Write an RTF Table or Figure to an RTF File
rtf_text

Text to RTF Encode
update_cellx

Update RTF border twips
write_rtf_para

Write a Paragraph to an RTF File
update_border_last

Update Last Border Line Based on Page Information
rtf_nrow

Add Number of Row Attributes for a Table
rtf_title

Add Title Attributes to Table
set_margin

Define Margin Type
rtf_page

Add RTF File Page Information
rtf_subline

Add Subline Attributes to Table
utf8Tortf

Convert a UTF-8 Encoded Character String to a RTF Encoded String
rtf_subset

Pass Table Attributes to Subset Table
vertical_justification

RTF Text Vertical Justification Dictionary
as_rtf_end

End RTF Encode
as_rtf_color

Create RTF Color Encode
as_rtf_colheader

Create Column Header RTF Encode
as_rtf_font

Create RTF Font Encode
as_rtf_pageby

RTF Table Page By Encoding
as_rtf_margin

Create RTF Page Margin Encode
as_rtf_init

Create RTF Header Encode
as_rtf_new_page

Create RTF New Page Encode
as_rtf_page

Create RTF Page Size Encode
as_rtf_footnote

Create Footnote RTF Encode
assemble_docx

Assemble Multiple RTF Table Listing and Figure Into One Word Document
as_rtf_title

Create Table Title RTF Encode
cell_size

Calculate Cell Size in Twips
as_rtf_source

Create Data Source RTF Encode
as_rtf_paragraph

Create Paragraph RTF Encode
check_args

Check Argument Types, Length or Dimension
font_type

RTF Text Font Dictionary
footnote_source_space

Derive Space Adjustment