You can format correctly following the FAQ: How to format your code
Compare to interactive running directly in the document, when you use rmarkdown::render
, the output of chunk is captured to be printed in the resulting document. Errors are not capture by default so that the rendering stop in the R console. This is not perfect, but when you have an error, here are some tricks you can use to see the chunk content, including the error:
You can add error = TRUE
only on the chunk that you want to "debug":
```{r chunk2, error = TRUE}
print('This worked')
# Code will stop here, but no information is printed to console when run headless using rmarkdown::render()
stop('error')
print('This will not run')
```
This will capture the error in the document, so rmarkdown::render
will not error and you can see the chunk content, including the error in the resulting document.
You can also add at the top of the document before any other chunk to apply on all chunk
```{r}
knitr::opts_chunk$set(error = TRUE)
```
See
If you want to stop rendering, right after the chunk with issue to be quicker at rendering when debuging, you can add knitr::knit_exit()
call at the end of the chunk, in an inline chunk or another chunk.
See
This is not ideal but that what exist for now.
Improving the error message at render for an Rmd is in our backlog. Thank you for your feedback !