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")
remotes::install_github("rubenarslan/rmdpartials")
Define a function like this:
my_summary <- function(object) {
rmdpartials::partial("_my_summary.Rmd")
}
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.
knit_print.lm <- function(object) {
rmdpartials::partial("_my_summary.Rmd")
}
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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.