# Read Word template into an onbrand object
obnd = read_template(
template = file.path(system.file(package="onbrand"), "templates", "report.docx"),
mapping = file.path(system.file(package="onbrand"), "templates", "report.yaml"))
# The examples below use the following packages
library(ggplot2)
library(flextable)
library(officer)
# Adding text
obnd = report_add_doc_content(obnd,
type = "text",
content = list(text="Text with no style specified will use the doc_def text format."))
# Text formatted with fpar
fpartext = fpar(
ftext("Formatted text can be created using the ", prop=NULL),
ftext("fpar ", prop=fp_text(color="green")),
ftext("command from the officer package.", prop=NULL))
obnd = report_add_doc_content(obnd,
type = "text",
content = list(text = fpartext,
format = "fpar",
style = "Normal"))
# Text formatted with markdown
mdtext = "Formatted text can be created using
**markdown** formatting"
obnd = report_add_doc_content(obnd,
type = "text",
content = list(text = mdtext,
format = "md",
style = "Normal"))
# Adding figures
p = ggplot() + annotate("text", x=0, y=0, label = "picture example")
imgfile = tempfile(pattern="image", fileext=".png")
ggsave(filename=imgfile, plot=p, height=5.15, width=9, units="in")
# From an image file:
obnd = report_add_doc_content(obnd,
type = "imagefile",
content = list(image = imgfile,
caption = "This is an example of an image from a file."))
# From a ggplot object
obnd = report_add_doc_content(obnd,
type = "imagefile",
content = list(image = imgfile,
caption = "This is an example of an image from a file."))
#Adding tables
tdf = data.frame(Parameters = c("Length", "Width", "Height"),
Values = 1:3,
Units = c("m", "m", "m") )
# Word table
tab_cont = list(table = tdf,
caption = "Word Table.")
obnd = report_add_doc_content(obnd,
type = "table",
content = tab_cont)
# onbrand flextable abstraction:
tab_cont = list(table = tdf,
caption = "Word Table.")
obnd = report_add_doc_content(obnd,
type = "table",
content = tab_cont)
# flextable object
tab_fto = flextable(tdf)
obnd = report_add_doc_content(obnd,
type = "flextable_object",
content = list(ft=tab_fto,
caption = "Flextable object created by the user."))
# Saving the report output
save_report(obnd, tempfile(fileext = ".docx"))
Run the code above in your browser using DataLab