The problem seems to be related to knitr::kable
incorrectly detecting the environment for the printing when it is embedded inside a function. This interferes with its ability to correctly figure out how to format. We can hack around this by placing the object to print in the top level environment before we print it.
---title: "Markdown example"output: html_document---```{r setup, include=FALSE}knitr::opts_chunk$set(echo = TRUE)print_kable = function(x) { print(kable_print_output <<- x) cat('\n')}```# Markdown example```{r mtcars, results='asis'}make_outputs <- function() { print_kable(knitr::kable(head(mtcars))) plot(mtcars$mpg, mtcars$cyl) print_kable(knitr::kable(tail(mtcars))) }make_outputs()```