Lemon print -- pretty printing data frames and tables"

This vignette demonstrates how load the lemon package automatically enables pretty printing of data frames with knitr's kable.

The beauty of defining the function knit_print.data.frame is that when working with R Notebooks in R, the editor can output the data frame dynamically, while ensuring that the knitted document also has the

library(knitr)
library(stringr)

knitr::opts_chunk$set(fig.height=4, fig.width=6, verbatim=FALSE,
                      cache=FALSE, autodep = TRUE, cache.path='lemon_print-cache/')

# Show chunk verbatim
# Source: https://stackoverflow.com/questions/19908158/show-an-r-markdown-chunk-in-the-final-output
# Set verbatim option as last and it will not be printed. ;)
hook_source_def = knit_hooks$get('source')
knit_hooks$set(source = function(x, options){
  if (!is.null(options$verbatim) && options$verbatim){
    opts = gsub(",\\s*verbatim\\s*=\\s*TRUE\\s*", "", options$params.src)
    bef = sprintf('\n\n    ```r\n', opts, "\n")
    stringr::str_c(bef, paste(knitr:::indent_block(x, "    "), collapse = '\n'), "\n    ```\n\n")
  } else {
     hook_source_def(x, options)
  }
})

First we load some data and show the top.

data(USArrests)
head(USArrests)

Now load lemon and set the lemon_print functions.

library(lemon)
knit_print.data.frame <- lemon_print

The same view is now nicely printed!

head(USArrests)

Notice how we specified kable arguments directly as chunk-options.

How it works

knitr uses a S3 generic function, knit_print, to print objects. Method dispatching in R means that by defining a function, say knit_print.data.frame, calling knit_print(df) will call our function if df was a data frame.

To have knitr use our function when outputting a data frame, we define the function knit_print.data.frame (and similar for data frames passed through dplyr functions, i.e tbl_df and grouped_df).

Disabling the function is by usual chunk options, render = normal_print. check ?knit_print.

Disabling the functions.

head(USArrests)

You can still ask for kable.

kable(head(USArrests),caption='Normal `kable` usage.')

It uses kable

We have mentioned kable a couple of times. That is because the lemon_print for data frames uses kable of the knitr package.

Some common arguments:

Why does it matter

The second code block, when edited in RStudio, would look like this: Viewing data frames in R Notebooks in RStudio And, as demonstrated, the rendered document has the data frame printed nicely.

If we do not wish this behaviour, but still use kable,

kable(head(USArrests))

RStudio would look like this: RStudio renders kable'd data frames rather eh

Other objects covered

Summaries are nicely formatted, but these require setting the S3 method for table.

knit_print.table <- lemon_print
summary(USArrests)

Cross-tabulations are however not nicely formatted.

head(mtcars)
with(mtcars, table(cyl, gear))

dplyr

library(dplyr)
knit_print.tbl <- lemon_print
mtcars %>% group_by(cyl) %>% summarise(mean(disp))


Try the lemon package in your browser

Any scripts or data that you put into this service are public.

lemon documentation built on Nov. 7, 2023, 5:06 p.m.