Chapter 12 FAQ

12.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.

12.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

12.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

12.4 How to keep images original height/width ratio?

Some users were in the following situation: they don’t want output graphics with the classical way, so they generate graphics in images. But these graphics need captions and should be cross-referencable.

Users should use the Markdown syntax to insert images in a markdown document. (note : this does not apply only to officedown format but to all Markdown format):

![caption](path/to/image)

Captions are simple paragraphs. Use officer::block_caption() or create your caption by hand in pure markdown.

With pandoc, you can associate a paragraph style with some text, if the style is associated with a auto-number, you have a caption style! That’s out of the scope of officedown but documented here: office-documents-generation.html#usage-with-rmarkdown.

12.5 Images rendering is different when in word

Some users want a similar rendering between the word output and what they are seeing in the R Markdown view (probably in RStudio).

Word is a paged support and HTML (the R Markdown editor) has different behavior.

You need to consider HTML and Word are 2 different formats. Word has a maximum width, this does not affect display in R Markdown viewer that is HTML. If your Word doc has a 7 inches’ width, figures’ width should not exceed that width because they won’t be resized as in a Web browser.

If you want an HTML ‘paged’ format with image resizing, I highly recommend package pagedown. If you need to stick to Word format, you’ll have to live with its characteristics.

12.6 How to create a report within Shiny application

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

library(shiny)
library(officer)
library(ggplot2)

ui <- fluidPage(

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

server <- function(input, output) {

    my_plot <- reactive({
        ggplot(faithful, aes(eruptions)) + geom_histogram(bins = input$bins)
    })
    output$distPlot <- renderPlot({
        my_plot()
    })
    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)