33

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.

3 Answers 3

44

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"))
3
  • You can find the full package vignette here haozhu233.github.io/kableExtra
    – Hao
    Commented Jun 12, 2017 at 2:16
  • I actually just recognized your id... 😂
    – Hao
    Commented Jun 12, 2017 at 2:23
  • Closing bracket is missing. Commented May 27, 2018 at 12:51
0

I would use a combination between the kable_styling: full_width and column_spec. Hope it helps.

 kable(myFields, "latex", longtable = F, booktabs = T) %>%
 kable_styling(full_width = T) %>%  
 column_spec(1, width = "5em" ) %>% 
 column_spec(2, width = "10em" ) %>%  
 column_spec(3, width = "15em")
0

I'm a little late to the party, but I thought I'd provide my two cents anyway. While we cannot use scale_down with longtable, I've had success using the out.width parameter. I believe this works because it sets sizes at a higher level (it works for figures and tables). Put something like this in the header:

```{r, echo=FALSE, results='asis', out.width="75%"

I hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.