Capture mid-chunk output from rmarkdown::render() on error?

When running chunks of an rmarkdown interactively using RStudio, the console output is visible. Therefore if an error happens, it can be debugged. When run headlessly, the only console output is the progress meter that tells which chunks have run and finished. That is helpful to a point, but it doesnt show where in the crunk the error happened. Are there any mechanism to capture the output from the partially executed chunk, when rmarkdown::render() is used? I realize that it is possible to debug a markdown by splitting apart chunks, etc.; however, that's not always an option.

Below is an example markdown to illustrate this (i am not sure how to escape the backtics . If you run this file interactively in RStudio, you see the console output, which shows exactly where it died. if you run on the command line or via "rmarkdown::render()", you only get the message "Quitting from lines 17-26", which is not always useful to diagnose a problem.

---

title: "RMarkdown Error Repro"

---

# NOTE: backticks "escaped" so they will render in this codeblock.

\```{r chunk1}

print('I worked')

\```

\```{r chunk2}

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')

\```

Again, if you run the above rmd file using "rmarkdown::render('myFile.rmd')", the only information is "Quitting from lines 17-26". Seeing the fact that the line "print('This worked')" printed would be helpful to diagnose which line failed, as would the actual error message.

Thanks for any help.

2 Likes

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 !

1 Like

Thanks for the reply. So "knitr::opts_chunk$set(error = TRUE)" will print the error itself, but what would really be useful is to see the last non-error code. If the actual error happens within some other package/method, the error message by itself is frequently not especially helpful.

When rmarkdown runs, it caches the images to a folder. Does the text portion of the output get captured somewhere? It must, since rmarkdown supports resume, doesnt it? Can that be viewed or dumped to a file?

I am not sure to follow. Using error = TRUE will capture the error in the resulting document. You will get the same output as when run interactively in the IDE as you asked in the first post.

In the IDE

In the Rmd output

  • error = FALSE will stop on error so rmarkdown::render will stop and you won't get the error capture in the output. It is the default for Rmarkdown
  • error = TRUE will capture the error as any other R code result, and print the error in the resulting file. This is the default for knitr knit() function. when use on its own.

What else do you want to be captured to a file that this does not capture ?

Can you give me a more detail example to understand better ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.