1

I want to include error messages in an R markdown pdf report. This works well:

---
output: pdf_document
---

This will be knitted and show the error message in the pdf.
```{r, error = TRUE}
stopifnot(2 == 3)
```

However, if I try the same approach with an error that comes from testthat, my document does not knit anymore.

---
output: pdf_document
---

This will not knit
```{r, error = TRUE}
library(testthat)
expect_equal(2, 3)
```

Why is that? And what can I do to include error messages from testthat's expect_something functions without wrapping them up in a test?

I think this must be possible since Hadley Wickham includes many error messages in his book R packages that come directly from expect_something-functions.

This is related, but not answered in Include errors in R markdown package vignette and How to skip error checking at Rmarkdown compiling?

symbolrush
  • 7,123
  • 1
  • 39
  • 67

2 Answers2

2

Create a test:

```{r, error = TRUE}
library(testthat)
test_that("Test A", {
  expect_equal(2, 3)
})
```
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
  • Thanks for your answer. This might be a workaround. But I still hope to be able to include errors from `expect_something` functions directly. See my edit of the question. – symbolrush Nov 16 '17 at 13:25
  • As far as I can see, all error messages come from comparisons contained in tests. If there are chunks with comparisons only, he has set `eval = F` (https://github.com/hadley/r-pkgs/blob/master/tests.rmd) – Martin Schmelzer Nov 16 '17 at 13:29
  • That's true. But he only recently changed that. https://github.com/hadley/r-pkgs/commit/649b73691b469dae68db63b9cd77e18e527dd808 So there might be a problem with that and including error messages from `expect_something` functions might not be possible at the moment.. – symbolrush Nov 16 '17 at 13:32
1

I don't understand the reason for the behavior (good question!), but this could be a workaround:

---
output: pdf_document
---

This will knit
```{r, error = TRUE}
library(testthat)
# expect_equal(2, 3)
# skip_if_not(2, 3)
assertthat::assert_that(2 == 3)
```
Christoph
  • 6,841
  • 4
  • 37
  • 89
  • Thanks for your answer. Sadly this does not really help since I want to include the more informative error messages from the `testthat` package. Notice that `expect_equal(2, 3)` and `skip_if_not(2, 3)` will not throw the same error messages. – symbolrush Nov 16 '17 at 13:21
  • Perhaps `assertthat` is better? From [here](https://stackoverflow.com/q/8343509/5784831). See edit... – Christoph Nov 16 '17 at 13:41