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?
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.
You can also use kableExtra::remove_column() , to remove a selected column from your final kable table.
The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).
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=","))
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With