Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the number of rows printed in kable

Tags:

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?

like image 869
John Smith Avatar asked Aug 08 '18 23:08

John Smith


People also ask

What is Kable () in R?

kable() is a method in R designed to generate a table against the given input. It is a part of the knitr package, which should be installed in the R environment for the kable method to run.

How do I remove a column from a Kable in R?

You can also use kableExtra::remove_column() , to remove a selected column from your final kable table.

What R package is Kable in?

The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).


1 Answers

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

like image 102
eipi10 Avatar answered Sep 28 '22 17:09

eipi10