7  HTML, PDF, and Word (and more!)

For a scientific report to be completely credible, it must be reproducible. The full computational environment used to derive the results, including the data and code used for statistical analysis should be available for others to reproduce. quarto is a tool that allows you integrate your code, text and figures in a single file in order to make high quality, reproducible reports. A paper published with an included quarto file and data sets can be reproduced by anyone with a computer.

One of the really great, powerful things about rmarkdown is that we can convert it to many different output types. The top three that you might be most likely to use are HTML, PDF, and Microsoft Word. There are others that we can discuss later.

In this section, we are going to briefly discuss how to render to these output formats, and some things that you might want to do for each of them.

7.1 Overview

  • Teaching: 10 minutes
  • Exercises: 15 minutes

7.2 Questions

  • How do I convert to HTML, PDF, or Word?
  • How do I set options specific to each of these?
  • How can I include a screenshot of an interactive graphic in PDF or Word?

7.3 Objectives

7.4 How do I convert to HTML, PDF, or Word?

Here are three ways to do this:

  1. You can control this in the “knit” button

You might notice that depending on the option you select, this changes things in the YAML - which is another way to control which output you have:

  1. You can change the YAML option
title: "Exploring gapminder"
output: html_document
title: "Exploring gapminder"
output: pdf_document
title: "Exploring gapminder"
output: word_document

And if we remember from the previous lesson, the information in the YAML is passed into the render function:

  1. You can call the render function.
rmarkdown::render("example.Rmd", output_format = "html_document")
rmarkdown::render("example.Rmd", output_format = "pdf_document")
rmarkdown::render("example.Rmd", output_format = "word_document")

7.4.1 A note on workflow with rmarkdown: HTML first, PDF/word later

It can be easy to get caught up with how your document looks. I highly recommend avoiding compiling to PDF or word until you really need to. This is also recommended by the author of rmarkdown and knitr, Yihui Xie. Because HTML doesn’t have page breaks, this means that you can spend time working on generating content, and not trying to get figures to line up correctly.

7.5 Your Turn

  1. Generate three reports, one as HTML, one as PDF, and one as microsoft word. Remember, if you are having PDF problems, see the installation chapter note on installing  with the R package, tinytex.