knitr::opts_chunk$set(echo = TRUE)
library(r2rtf) library(dplyr) library(tidyr)
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")
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:
png()
or ggplot2::ggsave()
).r2rtf::rtf_read_figure()
.r2rtf::rtf_title()
, r2rtf::rtf_footnote()
, r2rtf::rtf_source()
.r2rtf::rtf_page()
, r2rtf::rtf_figure()
.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).r2rtf::write_rtf()
.For
emf
format, one may use the R packagedevEMF
to create the figure.
# 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
Features of page and figure can be set up in rtf_page
and rtf_figure
respectively:
orientation
argument in rtf_page
. fig_height
and fig_width
arguments in rtf_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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.