Add section on troubleshooting R Markdown

Fixes #324
This commit is contained in:
hadley 2016-10-03 16:53:11 -05:00
parent c8b586514b
commit 7f915fad06
1 changed files with 9 additions and 0 deletions

View File

@ -297,6 +297,7 @@ comma <- function(x) format(x, digits = 2, big.mark = ",")
comma(3452345)
comma(.12358124331)
```
### Exercises
1. Add a section that explores how diamond sizes vary by cut, colour,
@ -317,6 +318,14 @@ comma(.12358124331)
both `b` and `c` depend on `a`. Have each chunk print `lubridate::now()`,
set `cache = TRUE`, then verify your understanding of caching.
## Troubleshooting
Troubleshooting R Markdown documents can be challenging because you are no longer in an interactive R environment, and you will need to learn some new tricks. The first thing you should always try is to recreate the problem in an interactive session. Restart R, then "Run all chunks" (either from Code menu, under Run region), or with the keyboard shortcut Ctrl + Alt + R. If you're lucky, that will recreate the problem, and you can figure out what's going on interactively.
If that doesn't help, there must be something different between your interactive environment and the R markdown environment. You're going to need to systematically explore the options. The most common difference is the working directory: the working directory of an R Markdown is the directory in which it lives. Check the working directory is what you expect by including `getwd()` in a chunk.
Next, brainstorm all the things that might cause the bug. You'll need to systematically check that they're the same in your R session and your R markdown session. The easiest way to do that is to set `error = TRUE` on the chunk causing the problem, then use `print()` and `str()` to check that settings are as you expect.
## YAML header
You can control many other "whole document" settings by tweaking the parameters of the YAML header. You might wonder what YAML stands for: it's "yet another markup language", which is designed for representing hierarchical data in a way that's easy for humans to read and write. R Markdown uses it to control many details of the output. Here we'll discuss two: document parameters and bibliographies.