Produce a Table Spanning Multiple Pages Using Kable()

Produce a table spanning multiple pages using kable()

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"))

how to split table into 2 pages in R markdown using datasummary?

@manro put you on the right track here. The trick is to use the longtable=TRUE argument.

Behind the scenes, modelsummary calls the kbl function from the kableExtra package to produce a table. All the arguments which are not explicitly used by datasummary() are automatically pushed forward to the kbl function via the ... ellipsis. This means that you can define the longtable argument directly in the datasummary() call. For example:

datasummary(mpg + hp ~ Mean + SD, data = mtcars, longtable = TRUE)

The above is a minimal example. I tried passing the longtable=TRUE argument in the same way in your more complex example, and your Rmarkdown document compiled without problems on my machine.

How to split row from table if column exceeds the page capacity in R Markdown?

Here's a workaround by splitting the cell with long text. It works by splitting the text into two chunks based on word count so could easily be adjusted by trial and error.

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE)

library(kableExtra)
library(wakefield) # for generating long text
library(dplyr)
library(tidyr)
library(stringr)

```
```{r df, include=FALSE}

set.seed(123)

#sample dataset

df <- data.frame(a = 1:6,
b = month.name[1:6],
c = names(mtcars)[1:6],
d = names(islands)[1:6],
e = c(paragraph(2), paste(paragraph(6), collapse = "; "), paragraph(3)))

#create new data frame, cells with long text split into to
df_new <-
df %>%
mutate(f = ifelse(str_length(e)>2000, word(e, 301, -1), NA_character_),
e = ifelse(str_length(e)>2000, word(e, 1, 300), e)) %>%
pivot_longer(cols = c(f, e), values_to = "e") %>%
na.omit() %>%
arrange(a, name) %>%
select(-name)

```
```{r long-table, results='asis'}

df_new %>%
kbl(booktabs = TRUE,
longtable = TRUE) %>%
row_spec(0, background = "#F6F6F6", color = "black") %>%
landscape() %>%
kable_styling(bootstrap_options = "striped",
font_size = 9,
latex_options = c("hold_position","repeat_header"),position = "left") %>%
column_spec(1, width = "2.0cm") %>%
column_spec(2, width = "2.5cm") %>%
column_spec(3, width = "2.5cm") %>%
column_spec(4, width = "4.5cm") %>%
column_spec(5, width = "10.0cm")

```

How to prevent a kable from splitting between pages?

If you're only exporting to PDF, then try this:

knitr::kable(
my_data,
format = "latex",
longtable = FALSE
)

A longtable table allows page breaks between rows. Looking at the code for knitr:::kable_latex, which kable calls, the default should be longtable = FALSE. But explicitly setting this argument makes sure you're not making longtables.

Using Markdown formatting in table using kable in quarto

When creating a table in Quarto, you can't mix Markdown with HTML - the Markdown syntax won't be processed within the HTML table.

This R code would work

data.frame(Function = "`read_delim()`",
Formula = "$\\leftarrow$",
Break = "this continues on a<br>new line",
Link = "[Google](www.google.com)") |>
kbl(format = "markdown")

So if you can, output only Markdown table which knitr::kable() should do by default.

If you need to output a HTML table (e.g for specific HTML features), you need to use a framework that will render the markdown for you while creating the HTML table.

  • gt with fmt_markdown() and md()
  • flextable with ftextra and colformat_md() or as_paragraph_md

This is possible that this limitation of note being able to include raw Markdown inside HTML table will be improve in the future (https://github.com/quarto-dev/quarto-cli/discussions/957#discussioncomment-2807907)

Kable of a list of dataframes prints as comments instead of table

according to the documentation, the first argument to kable_styling is your kable, you piped it in, which is what I believe is your error

x_html <- knitr::kable(head(mtcars), "html")
kable_styling(x_html, "striped", position = "left", font_size = 7)

kable_styling_example



Related Topics



Leave a reply



Submit