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()
andmd()
- flextable with ftextra and
colformat_md()
oras_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
Importing Multiple Excel Files with Filenames in R
Freezing Header and First Column Using Data.Table in Shiny
Plot Multiple Datasets with Ggplot
If Column Contains String Then Enter Value for That Row
Disabling/Enabling Sidebar from Server Side
R Cumulative Sum with a Condition and a Reset
Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R
Randomly Sample Data Frame into 3 Groups in R
Calculate Using Dplyr, Percentage of Na's in Each Column
Generate All Combinations, of All Lengths, in R, from a Vector
Ggplot2 and Geom_Density: How to Remove Baseline
How to Make Single Stacked Bar Chart in Ggplot2
Create Link to the Other Part of the Shiny App
Constructing a Named List Without Having to Type Each Object's Name Twice
How to Create a Histogram from Aggregated Data in R
Gap in Polar Time Plot - How to Connect Start and End Points in Geom_Line or Remove Blank Space
How to Move the Bibliography in Markdown/Pandoc
How to Convert a Character String Date to Date Class If Day Value Is Missing