knitr::opts_chunk$set(echo = TRUE)
library(r2rtf)
library(dplyr)
library(tidyr)

Examples

This example shows how to create and embed figures into an RTF file as below.

knitr::include_graphics("pdf/fig-simple.pdf")

Below is an example with adjusted page orientation, figure height and width.

knitr::include_graphics("pdf/fig-landscape.pdf")

Overall workflow

The package allow user to embed multiple figures into one RTF document. The supported format is listed as below.

r2rtf:::fig_format()

By using png file as an example, the workflow can be summarized as:

  1. Save figures into PNG format. (e.g. using png() or ggplot2::ggsave()).
  2. Read PNG files into R as binary file using r2rtf::rtf_read_figure().
  3. Add optional features using r2rtf::rtf_title(), r2rtf::rtf_footnote(), r2rtf::rtf_source().
  4. Set up page and figure options using r2rtf::rtf_page(), r2rtf::rtf_figure().
  5. Encode rtf using r2rtf::rtf_encode(doc_type = "figure"). (Note: it is important to set doc_type = "figure" as the default is doc_type = "table" to handle tables).
  6. Write rtf to a file using r2rtf::write_rtf().

For emf format, one may use the R package devEMF to create the figure.

Simple Example

# Define the path of figure
filename <- c("fig/fig1.png", "fig/fig2.png", "fig/fig3.png")

filename %>%
  rtf_read_figure() %>% # read PNG files from the file path
  rtf_title("title", "subtitle") %>% # add title or subtitle
  rtf_footnote("footnote") %>% # add footnote
  rtf_source("[datasource: mk0999]") %>% # add data source
  rtf_figure() %>% # default setting of page and figure
  rtf_encode(doc_type = "figure") %>% # encode rtf as figure
  write_rtf(file = "rtf/fig-simple.rtf") # write RTF to a file

Example with features

Features of page and figure can be set up in rtf_page and rtf_figure respectively:

The figure height and width can be set up for each figure in a vector. The code below provides an example for these features.

filename %>%
  rtf_read_figure() %>% # read PNG files from the file path
  rtf_page(orientation = "landscape") %>% # set page orientation
  rtf_title("title", "subtitle") %>% # add title or subtitle
  rtf_footnote("footnote") %>% # add footnote
  rtf_source("[datasource: mk0999]") %>% # add data source
  rtf_figure(
    fig_height = 3.5, # set figure height
    fig_width = c(6, 7, 8) # set figure width individually.
  ) %>%
  rtf_encode(doc_type = "figure") %>% # encode rtf as figure
  write_rtf(file = "rtf/fig-landscape.rtf") # write RTF to a file


Merck/r2rtf documentation built on April 18, 2024, 11:51 a.m.