Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Produce a table spanning multiple pages using kable()

I would like to produce a table that spans over multiple pages using kable(). I know this is possible using xtable() with the "longtable" option, but I need kable() for other features.

Any ideas?

```{r cars, echo=TRUE, results='asis', warning=FALSE, message=FALSE}
    library(knitr)
    library(kableExtra)

# OUTPUT 1, fits on one page
output = rbind(mtcars[, 1:5])

kable(output, booktabs = T, format="latex", caption = "Small Output")


# OUTPUT 2, will not fit on one page 
output = rbind(mtcars[, 1:5], mtcars[, 1:5])

kable(output, booktabs = T, format="latex", caption = "Large Output")

```

Update: I am dumb! "longtable=TRUE," is an option. The problem is that this changes the order of my output and kinda messes things up.

like image 541
kpr62 Avatar asked Jun 11 '17 17:06

kpr62


People also ask

What does Kable do in R?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

How do I insert a table in R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.


1 Answers

You can try to use the kableExtra package. If you specify hold_position in kable_styling, you should be able to ping the table to the place you want.

Also, in the current dev version, I introduced a new feature called repeat_header for longtable to repeat the header row on every page. You can check it out.

kable(output, "latex", booktabs = TRUE, longtable = TRUE, caption = "Test") %>%
  kable_styling(latex_options = c("hold_position", "repeat_header"))
like image 194
Hao Avatar answered Sep 23 '22 13:09

Hao