There are two other chunks in the default document. If we run them we do get output to show within the document by
default. However, running or not running chunks interactively will not affect our output document. We won’t run these
chunks right now so we can see what happens in the output.
Note that you can stop output from showing within the Rmd document by going to
Global Options. . . > R Markdown
and unclick the box next to Show output inline for all R Markdown documents
R Markdown with PDF output
Instead of a Preview button at the top of the document, we now have a Knit button. This is what we will use to knit the
Rmd file to the output type we’ve chosen. In this case we’ll be knitting this document to a PDF, as we chose a PDF document
when we created the new R Markdown file.
This process is called knitting because the knitr package is doing the work behind the scenes.
Saving the R Markdown document
If you hit Knit, you will be prompted to save the document. Let’s save this as a document called pdf_test.Rmd.
Once you hit Save the document will immediately begin to knit. We can see the process in the R Markdown pane in RStudio,
which shows in the same pane as the Console. This is RStudio using various R packages and software to do the heavy lifting
of converting our Rmd to a PDF.
You’ll see RStudio keeps track of how much of the document has been knit. For small documents it seems almost instanta-
neous, but for larger documents this can help you keep track of the progress that has been made.
Knitting to PDF requires LaTeX
RStudio does this knitting in a way that looks pretty magical. However, we need a LaTeX program installed if we want
to knit a Markdown document to a non-HTML format, which is why we installed TinyTeX earlier. Other common LaTeX
distributions are MikTeX (Windows) and MacTeX (Mac OS).
If you do not have some version of LaTeX installed or RStudio can’t find the one you do have, you will get errors when trying
to knit to non-HTML formats and you will need to troubleshoot.
The output PDF document
Once the PDF document has been created, we can take a closer look at it. You’ll see all the same elements as our R Notebook,
including information from the YAML header, plain text, and code with output.
Code chunks always create output by default
One thing I want you to see is that our two non-setup R code chunks both ran and the R output shows in the PDF document.
This is one big difference between interactive R Markdown Notebooks and non-interactive (or batch) R Markdown documents.
In the former, we run chunks interactively and they show up in the final document. The document is updated each time you
save, but doesn’t run through any chunks that have already been run and saved previously.
In the latter, all code chunks are run every time you knit. The order the chunks are in is very important, as the chunks are
run in the order they show up in the document. This means you must have a code chunk that loads a package before a code
chunk that uses functions from that package.
For large R Markdown documents with a lot of slow code, running all chunks every time you knit can get tiresome. The
chunk option cache = TRUE can help with this, allowing the output to be cached after it is run the first time to save on
processing time. See the caching page on the knitr author’s website for more info.
26