I have a wide table that I want to print a two subtables so it fits on a printed page. The code below works nicely for HTML but it throws this error:
`data` must be a `gt_tbl` object, not a <gt_group> object.
when I try to render to PDF. Is there a fix or workaround that would let me print a wide tibble in a PDF using quarto:
---
title: "broken"
format: pdf
---
```{r}
library(tidyverse)
library(gt)
ten_wide <- tribble(
~a, ~b, ~c, ~d, ~e, ~f, ~g, ~h, ~i, ~j,
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "gulf", "hotel", "india", "juliet",
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "gulf", "hotel", "india", "juliet",
)
ten_wide |>
gt() |>
gt_split(col_slice_at = 5)
```
The desired output (give or take zebra stripes) is a single table that is generated if you change the format: pdf
to be be format: html
:
[updated answer] The issue you're experiencing is because the pdf output is created using latex. For PDF rendering, you'll need an alternative approach. You can use library(kableExtra)
for a custom table design like this.
Make sure to pre-Install "kableExtra" R-Package with install.packages("kableExtra")
.
---
title: "My Table"
format:
pdf:
geometry:
- margin=1cm
---
```{r}
#| echo: false
#| warning: false
# #| layout-ncol: 2 # remove # for rendering tables in 2 columns
library(tidyverse)
library(gt)
library(kableExtra)
ten_wide <- tribble(
~a, ~b, ~c, ~d, ~e, ~f, ~g, ~h, ~i, ~j,
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "gulf", "hotel", "india", "juliet",
"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "gulf", "hotel", "india", "juliet",
)
#gt(ten_wide[, 1:5])
#gt(ten_wide[, 6:10])
#gt(ten_wide)
# corrupt code
# ten_wide |>
# gt() |>
# gt_split(col_slice_at = 5)
# Split table into two halves
first_half <- ten_wide[, 1:5]
second_half <- ten_wide[, 6:10]
# Render two tables with reduced width
first_half %>%
kbl(booktabs = TRUE) %>%
kable_styling(
bootstrap_options = c("striped", "hover"),
latex_options = c("HOLD_position"),
full_width = FALSE,
position = "center",
htmltable_class = "table-condensed"
) %>%
row_spec(0, bold = TRUE)
second_half %>%
kbl(booktabs = TRUE) %>%
kable_styling(
bootstrap_options = c("striped", "hover"),
latex_options = c("HOLD_position"),
full_width = FALSE,
position = "center",
htmltable_class = "table-condensed"
) %>%
row_spec(0, bold = TRUE)
```
Which generates this required pdf: