Chapter 11 FAQ

11.1 Is it possible to edit or create Word styles from R

No. This may be implemented in the future as it’ll be a useful feature to have.

11.2 Do you want to update the fields in this document?

You may see this message when editing the document with Word:

This document contains fields that may refer to other files. Do you want to update the fields in this document?

Click “Yes”

This is a deliberate design decision for security reasons. Some kinds of Word fields can access external data. Microsoft’s policy on this point is that responsibility for opening a document (and taking a risk) lies with the user - the user needs to decide whether the document comes from a trusted source.

For this reason, if the fields are set to automatically update, a message will be displayed asking the user whether to allow the update.

It’s possible to insert fields and not set the automatic update. In this case, the user will need to manually update the fields or there could be an add-in that takes care of this when any document is opened. Since the user will have made the choice to install the add-in, that’s again the user’s responsibility.

The only other way to suppress the message is to have opened the document and updated the fields before passing it to the user. Programmatically, this could be done either using Word automation (not server-side) or in an on-premises version of SharePoint that has the Word Automation Services installed.

The following explanation is written by Cindy Meister on stackoverflow

11.3 Add a bullet list on a Word document with officer

Creating ordered and unordered lists with officer is a difficult point and will probably be implemented, but for now the only way to do it is to create a paragraph style containing a bullet and reuse it with the body_add_par() function; see The following example as an illustration.

The used template is: template.docx


doc <- read_docx(path = "template.docx") %>%
  body_add_par("item 1", style = "bullet") %>%
  body_add_par("item 2", style = "bullet") %>%
  body_add_par("item 3", style = "bullet")

print(doc, target = "test.docx" )

bullet list on a Word document

11.4 How to create a report within Shiny application

This is a minimal example that shows how to use officer from a shiny application:


ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),
            sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
            downloadButton("downloadReport", "Download report")

server <- function(input, output) {

    my_plot <- reactive({
        ggplot(faithful, aes(eruptions)) + geom_histogram(bins = input$bins)
    output$distPlot <- renderPlot({
    output$downloadReport <- downloadHandler(
        filename = function() {
            paste("report-", Sys.Date(), ".docx", sep="")
        content = function(file) {
            doc <- read_docx()
            doc <- body_add_gg(doc, value = my_plot())
            print(doc, target = file)

shinyApp(ui = ui, server = server)