docx
objects can generate Word documents using bookmarks as placeholders to insert contents.
Use the function docx
with argument template to create a document based on an Word document where bookmarks are located.
Functions addFlexTable
, addPlot
, addParagraph
and addImage
can send respective outputs into these bookmarks. All of them have an optional argument named bookmark
corresponding the the bookmarked content to replace.
Content (plots, paragraphs or images) will replace the whole paragraph containing the bookmark.
When used with addFlexTable
, content (table) will be added after the paragraph containing the bookmark.
In a Word document, paragraphs and tables are different containers; a Word table contains paragraphs but is not a paragraph object. Because Word bookmarks are located in paragraphs, it is not possible to bookmark a table.
We will use a specific file as template bookmark_example.docx
containing various styles and bookmarks located in the package.
library( ReporteRs )
target_file <- "bookmark_example.docx" # file to produce
template <- system.file(package = "ReporteRs",
"templates/bookmark_example.docx" ) # template example
styles(doc)
## Normal heading 1 heading 2 header
## "Normal" "Titre1" "Titre2" "En-tte"
## footer Title rcode figurereference
## "Pieddepage" "Titre" "rcode" "figurereference"
## tablereference small
## "tablereference" "small"
```
Let use iris
to illustrate tables and graphics.
library( ggplot2 )
alpha = I(0.7) )
Use list_bookmarks
to check available bookmarks in the template document:
list_bookmarks(doc)
## [1] "PLOT" "DATA" "AUTHOR" "REVIEWER"
Finally, add all elements into the report and write it into file target_file
.
library( magrittr )
doc %>%
Download file bookmark_example.docx - view with office web viewer
To be used with a docx
object, bookmark must be placed into a single paragraph, you have to add the bookmark on a portion of a text in a paragraph.
If placed along 1 or more paragraphs, produced document will be corrupted.
To add a bookmark that ReporteRs will use later, do not add the bookmark on the whole paragraph** but rather in the paragraph.
This operation is to be done in Word, by editing the template of the document.
Below an example that will not work (bookmark brackets are on the whole paragraph):
Below an example that will work (bookmark brackets are not on the whole paragraph):