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.
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.')
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:
caption
: Table caption. See examples in demonstration.align
: Vector of column alignments with 'l'
, 'c'
, and 'r'
.
Or, a single element, i.e. c('c','l','r') = 'clr')
.row.names
: A logical value indicating whether to include row names.col.names
: A character vector of column names to be used in the table.The second code block, when edited in RStudio, would look like this: 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:
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))
library(dplyr) knit_print.tbl <- lemon_print
mtcars %>% group_by(cyl) %>% summarise(mean(disp))
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.