forked from njtierney/rmd4sci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-problems.Rmd
265 lines (168 loc) · 7.36 KB
/
common-problems.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Common Problems with rmarkdown (and some solutions)
There are some things that I run into fairly frequently (and some not so much) when I'm rendering my rmarkdown documents. This section details some the common problems, and the solution that I have found works for me.
If you want to practice on fixing broken rmarkdown documents, check out some pathologically broken examples on github at [njtierney/rmd-errors](https://github.com/njtierney/rmd-errors).
## Avoiding problems
To avoid problems in the first place, I try and do the following:
- Develop code in chunks and execute the chunks until they work, then move on.
- knit the document regularly to check for errors.
Then, if there is an error:
- recreate the error in an interactive session:
- restart R
- run all chunks below
- find the chunk that did not work, fix until it does
- run all chunks below
- explore working directory issues
- remember that the rmarkdown directory is where the .Rmd file lives
## The errors
What follows from here are all the errors you might in an rmarkdown document, with the following structure:
- What they might look like
- What the error message might appear to be, and
- How to solve them
## "Duplication": Duplicated chunk names
**What it might look like**
Chunks like this:
````markdown
```{r title-one}`r ''`
```
```{r title-one}`r ''`
```
````
**The error message**
This is caught before the document compiles with a warning like:
```
processing file: common-problems.Rmd
Error in parse_block(g[-1], g[1], params.src) :
duplicate label 'title-one'
Calls: local ... process_file -> split_file -> lapply -> FUN -> parse_block
Execution halted
Error in Rscript_render(f, render_args, render_meta) :
Failed to compile common-problems.Rmd
Calls: <Anonymous> ... render_book -> render_new_session -> Rscript_render
Execution halted
```
The important part to note is the start:
```
.
.
.
Error in parse_block(g[-1], g[1], params.src) :
duplicate label 'title-one'
.
.
.
```
**How to solve**
* In our case we have the same chunk name twice: 'title-one'. Change the chunk name of one of them!
## "Not what I ordered": Objects not created in the right order
**What it might look like**
```r
plot(my_table)
my_table <- table(mtcars$cyl)
```
**The error message**
**How to solve**
## "Forgotten Trails I": Missing ",", or "(", "}", or "'"
**What it might look like**
**The error message**
**How to solve**
## "Forgotten Trails II": Chunk option with trailing ", or not input
**What it might look like**
**The error message**
```
processing file: rstudio.Rmd
(*) NOTE: I saw chunk options "fig-rstudio-workspace-options, fig.cap = "Setting the options right for rstudio, so you don't restore previous sessions work, and don't save it either."
please go to https://yihui.name/knitr/options
(it is likely that you forgot to quote "character" options)
Error in parse(text = code, keep.source = FALSE) :
<text>:1:51: unexpected INCOMPLETE_STRING
1: alist( 'fig-rstudio-workspace-options', fig.cap = "Setting the options right for rstudio, so you don't restore previous sessions work, and don't save it either. )
```
**How to solve it?**
* The easiest way is to do Cmd+Shift+F, which opens up a global search in your rstudio project, and then type in the offending string mentioned in the NOTE. In this case, I would search for the partial string `"fig.cap = "Setting the options right for rstudio, so you don't restore previous sessions work, and`. I search for the partial string because there might be parts at the end of the error message that aren't in the text.
## "Forgotten Trail III":
**What it might look like**
**The error message**
```
processing file: figures-tables-captions.Rmd
(*) NOTE: I saw chunk options "read-gapminder include = FALSE, echo = FALSe"
please go to https://yihui.name/knitr/options
(it is likely that you forgot to quote "character" options)
Error in parse(text = code, keep.source = FALSE) :
<text>:1:23: unexpected symbol
1: alist( read-gapminder include
^
```
**How to solve**
This error message is pretty good, I needed to add a comma after my chunk name.
So, go from:
````markdown
`r ''```{r read-gapminder include = FALSE, echo = FALSE}
gapminder <- readr::read_csv(here::here("data", "gapminder.csv"))
```
````
to
````markdown
`r ''```{r read-gapminder, include = FALSE, echo = FALSE}
gapminder <- readr::read_csv(here::here("data", "gapminder.csv"))
```
````
## "The Path Not Taken" File path incorrect
**What it might look like**
**The error message**
**How to solve**
## "Spolling I" Incorrectly spelled chunk options
These are often not an error, but you just won't get the behaviour that you expect.
**What it might look like**
* `fig.caption` instead of `fig.cap`. This once caused me to rewrite a lot of code and an entire section of a paper until I realised the problem.
**The error message**
**How to solve**
* There might be a switch you can flick to ask `knitr` to solve
## "Spolling II" Incorrectly spelled chunk option inputs
So this is when you provide the wrong input to your chunk options. Like something that requires `TRUE` gets "yes", or something that needs `"100%"` instead gets `100`
**What it might look like**
**The error message**
```
Quitting from lines 31-32 (figures-tables-captions.Rmd)
Error in eval(x, envir = envir) : object 'FALSe' not found
Calls: local ... process_group.block -> call_block -> eval_lang -> eval -> eval
Execution halted
Error in Rscript_render(f, render_args, render_meta) :
Failed to compile figures-tables-captions.Rmd
Calls: <Anonymous> ... render_book -> render_new_session -> Rscript_render
Execution halted
```
**How to solve**
What was the problem? Turns out I provided the option `FALSe` instead of `FALSE`.
Go from:
````markdown
`r ''```{r read-gapminder include = FALSE, echo = FALSe}
gapminder <- readr::read_csv(here::here("data", "gapminder.csv"))
```
````
to
````markdown
`r ''```{r read-gapminder, include = FALSE, echo = FALSE}
gapminder <- readr::read_csv(here::here("data", "gapminder.csv"))
```
````
## "The Legend of Link I": Your images in `![]()` don't work.
I often forget that it is `![](path/to/image)`, and not `![]("path/to/image")`. There are no quote marks!
## LaTeX errors
There is no panacea for LaTeX errors, but if you aren't familiar with "what that error message" might look like, here are some details.
**What it might look like**
**The error message**
**How to solve**
## I want to include inline R code verbatim to show an example
... Like for a book on using rmarkdown or something.
Check out this great [blog post by T. Hovorka from R Views](https://rviews.rstudio.com/2017/12/04/how-to-show-r-inline-code-blocks-in-r-markdown/)
It boils down to this:
<span style="color:blue">``` `r "\u0060\u0060 \u0060r \"\\u0060r expression\\u0060\"\u0060 \u0060\u0060"` ```</span>.
## My Figure or Table isn't being cited
**What it might look like**
You create a figure,
**The error message**
There isn't one - you just get `\@ref(fig:figure-chunk-name)` printed.
**How to solve**
You need to make sure that you actually print the table or plot. If you create the plot and save it, but do not print it in the document, then you will not be able to reference the plot or table.
## Your Turn {.exercise}
1. Go to this repo [njtierney/rmd-errors](https://github.com/njtierney/rmd-errors), and give debugging some of these common rmarkdown errors a go.