Search code examples
rr-markdownr-s4

Using the `validate` package inside an Rmarkdown document


I wish to include same validations inside my Rmarkdown report. Here is a minimal example:

---
title: "My Report"
output:
  html_document:
    mode: selfcontained
    theme: null
---

```{r, echo=FALSE}
library(validate)
rules <- validator(
    "Species" %in% names(.),
    is.character(Species),
    !is.na(Species)
)
chk <- confront(dat=iris, x=rules)
summary(chk)
```

I would expect a data.frame as the result, but I get (when I call rmarkdown::render on a file with the content above):

##     Length      Class       Mode 
##          3 validation         S4

It seems that the S4 summary method is not called correctly. I tried to call validate::summary.validations but that did not help.

Any ideas how to call summary from inside an rmarkdown document?


Solution

  • The summary.validations() function isn't exported from the namespace, so you'd have to use validate:::summary.validations(chk), but when I tried that, I got NULL as the output. However, when I used validate::summary(chk), I got the intended answer. (R v4.3.2, validate v1.1.3, rmarkdown v2.25, macOS 13.6)