Simplify using rmarkdown partials to document objects in R
Ever want to make a standardized way to report a summary of something like a regression, but did not want to limit yourself to one unformatted block of text, one table, or one figure?
Rmarkdown partials allow you to define standardised knitr code chunks that become part of your rmarkdown report. They can then be turned into HTML, PDFs, or Word files. You could, for example, create an rmarkdown partial to summarise regression models in a flexible way.
You can also define your partials as methods of
knit_print
, so that a rich rmarkdown partial is made by
default.
Confer the help or: https://rubenarslan.github.io/rmdpartials. See the vignette
for a quick example of an HTML document generated with
rmdpartials
.
To get the latest development version:
install.packages("remotes")
::install_github("rubenarslan/rmdpartials") remotes
Define a function like this:
<- function(object) {
my_summary ::partial("_my_summary.Rmd")
rmdpartials }
And create a file called _my_summary.Rmd
with contents
like this:
## My special summary
```{r}
summary(object)
```
## My special plot
```{r}
plot(object)
```
To use the partial in an rmarkdown report
```{r}
m1 <- lm(y ~ x, data = data)
my_summary(m1)
```
To make your partial the default printing option for objects of a certain class when they are echoed in a knitr document, give it the name of a knit_print method.
<- function(object) {
knit_print.lm ::partial("_my_summary.Rmd")
rmdpartials }
Now, all you need to is let the lm object be echoed.
```{r}
m1 <- lm(y ~ x, data = data)
m1
```
You can also preview what the result from the partial would look like by calling it in an interactive session.