5
votes

When using rmarkdown to render pdf document, we can use three options for printing data.frame: default, kable and tibble (see here)

With the default option, it is possible to limit the number of rows printed, with the option: max.print

For tibble, we can use: dplyr.print_max

I can't find a way to limit the number of rows for kable. Is it possible?

2

2 Answers

4
votes

kable renders the full data frame passed to it as a table in the output document. AFAIK there's no argument that will limit the number of rows. However, you can preselect the number of rows in the output table (for example, kable(head(dat)) or kable(dat[1:5, ])). If you want to avoid having to select the rows each time, you could write a helper function to limit the number of rows printed. For example:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
```

```{r}
my_kable = function(x, max.rows=6, ...) {
  kable(x[1:max.rows, ], ...)
}
```

```{r}
my_kable(mtcars, caption="My first caption")
```

```{r}
iris$Sepal.Length = 1000 * iris$Sepal.Length
my_kable(iris, 3, caption="My second caption", format.args=list(big.mark=","))
```

enter image description here

0
votes

A really simple solution is to wrap kable around head:

kable(head(mtcars, n = 5))