Examples
This example shows how to create and embed figures into an RTF file as below.
Below is an example with adjusted page orientation, figure height and width.
Overall workflow
The package allow user to embed multiple figures into one RTF document. The supported format is listed as below.
r2rtf:::fig_format()
## type rtf_code
## 1 emf \\emfblip
## 2 png \\pngblip
## 3 jpeg \\jpegblip
By using png
file as an example, the workflow can be
summarized as:
- Save figures into PNG format. (e.g. using
png()
orggplot2::ggsave()
). - Read PNG files into R as binary file using
r2rtf::rtf_read_figure()
. - Add optional features using
r2rtf::rtf_title()
,r2rtf::rtf_footnote()
,r2rtf::rtf_source()
. - Set up page and figure options using
r2rtf::rtf_page()
,r2rtf::rtf_figure()
. - Encode rtf using
r2rtf::rtf_encode(doc_type = "figure")
. (Note: it is important to setdoc_type = "figure"
as the default isdoc_type = "table"
to handle tables). - Write rtf to a file using
r2rtf::write_rtf()
.
For
emf
format, one may use the R packagedevEMF
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:
- Page orientation:
orientation
argument inrtf_page
. - Figure height and width:
fig_height
andfig_width
arguments inrtf_figure
.
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