rfortherestofus / omni

RMarkdown template, ggplot2 theme, and table function for OMNI Institute
https://rfortherestofus.github.io/omni/
Other
21 stars 5 forks source link

How to deal with tables breaking across pages #87

Closed dgkeyes closed 4 months ago

dgkeyes commented 4 months ago

Video question

The issue here is that tables that go across multiple pages sometimes look odd. The code below gives a reproducible example of tables made with gt and flextable. The gt table does not work well across multiple pages but the flextable one does. @OskaratOmni I believe I have summarized this correctly, but let me know if I've missed anything.

I believe the best solution here is to use flextable because it just works across pages. However, if you want to use gt it looks like there is a function to split a gt table into multiple tables. For example:

library(tidyverse)
library(gt)

gtcars |>
  select(1:5) |> 
  gt() |>
  gt_split(row_every_n = 10)

Gives this:

image

I did try this function and was having trouble getting it to work with the omni_table() function. If you want me to look into this more, just let me know. But I wanted to check first because maybe you'll just want to go with flextable here.

Original code you shared is below.

---
title: "Document Title"
date: "01-01-2024"
output:
  omni::omni_pdf_report_new
knit: pagedown::chrome_print
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      fig.topcaption = TRUE,
                      fig.cap = TRUE,
                      dpi = 150,
                      warning = FALSE)
library(tidyverse)
library(flextable)
library(knitr)
library(omni)
library(scales)

First Level Header

:::wrap-columns :::column-70 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Et ligula ullamcorper malesuada proin libero nunc consequat. Proin libero nunc consequat interdum varius sit. Maecenas pharetra convallis posuere morbi leo. Ipsum dolor sit amet consectetur. Dolor sit amet consectetur adipiscing elit duis tristique. Sit amet cursus sit amet dictum sit. Non sodales neque sodales ut etiam sit amet. Fermentum odio eu feugiat pretium nibh ipsum. Neque aliquam vestibulum morbi blandit cursus risus at ultrices mi. Maecenas pharetra convallis posuere morbi. Bibendum ut tristique et egestas quis ipsum suspendisse. :::

:::column-30

Sidebar Header, 13pt Arial Bold

Text is inset from all edges by .125”, with the sidebar body copy having the exact same specs as the standard body copy. ::: :::

Second Level Header

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Et ligula ullamcorper malesuada proin libero nunc consequat. Proin libero nunc consequat interdum varius sit. Maecenas pharetra convallis posuere morbi leo. Ipsum dolor sit amet consectetur. Dolor sit amet consectetur adipiscing elit duis tristique. Sit amet cursus sit amet dictum sit. Non sodales neque sodales ut etiam sit amet. Fermentum odio eu feugiat pretium nibh ipsum. Neque aliquam vestibulum morbi blandit cursus risus at ultrices mi. Maecenas pharetra convallis posuere morbi. Bibendum ut tristique et egestas quis ipsum suspendisse.

:::stats-block :::stats-number 24 ::: :::stats-content pt. Arial Bold for stats numbers. 11 pt Arial Bold for stats content. ::: :::

Third Level Header

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Et ligula ullamcorper malesuada proin libero nunc consequat. Proin libero nunc consequat interdum varius sit. Maecenas pharetra convallis posuere morbi leo. Ipsum dolor sit amet consectetur. Dolor sit amet consectetur adipiscing elit duis tristique. Sit amet cursus sit amet dictum sit. Non sodales neque sodales ut etiam sit amet. Fermentum odio eu feugiat pretium nibh ipsum. Neque aliquam vestibulum morbi blandit cursus risus at ultrices mi. Maecenas pharetra convallis posuere morbi. Bibendum ut tristique et egestas quis ipsum suspendisse.

iris |>
  group_by(Species) |>
  summarise(sepal_length_mean = mean(Sepal.Length)) |>
  ggplot(aes(x = Species, y = sepal_length_mean, fill = Species)) +
  geom_col() +
  coord_flip() +
  scale_fill_omni_discrete() +
  theme_omni()

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Et ligula ullamcorper malesuada proin libero nunc consequat. Proin libero nunc consequat interdum varius sit.

knitr::include_graphics(system.file("assets/images/example_figure.png", package = "omni"))

Fourth Level Header

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi lacus sed viverra tellus. Et ligula ullamcorper malesuada proin libero nunc consequat. Proin libero nunc consequat interdum varius sit. Maecenas pharetra convallis posuere morbi leo.

Ipsum dolor sit amet consectetur. Dolor sit amet consectetur adipiscing elit duis tristique. Sit amet cursus sit amet dictum sit. Non sodales neque sodales ut etiam sit amet. Fermentum odio eu feugiat pretium nibh ipsum. Neque aliquam vestibulum morbi blandit cursus risus at ultrices mi. Maecenas pharetra convallis posuere morbi. Bibendum ut tristique et egestas quis ipsum suspendisse.

Ipsum dolor sit amet consectetur. Dolor sit amet consectetur adipiscing elit duis tristique. Sit amet cursus sit amet dictum sit. Non sodales neque sodales ut etiam sit amet. Fermentum odio eu feugiat pretium nibh ipsum. Neque aliquam vestibulum morbi blandit cursus risus at ultrices mi.

mtcars |> 
  slice(1:5)  |> 
  omni_table(caption = "Caption")

:::section-header

Section header

:::

With second level 1

Big table 1

btn = length(LETTERS)

big_table_data = 
  tibble( 
    Group = LETTERS, 
    Question1 = sample(c(1:5), size = btn, replace = TRUE),
    Question2 = sample(c(1:5), size = btn, replace = TRUE),
    followup = sample(c("yes","no"), size = btn, replace = TRUE),
    )
omni_table(big_table_data, option = 'gt', caption = 'how does this break across rows? the gt version')

And third level A

omni_table(big_table_data, caption = 'how does this break across rows? the flextable version')

And second level 2 with a very very very very long title

And third level A

And third level C

And second level 3

OskaratOmni commented 4 months ago

Hey @dgkeyes , Thanks. It doesn't work with either flextable or gt. If it worked with one of them, we would just use the one that works.

Attached is a pdf showing each version as it knits here.

The flextable version has all the extra tick mark stuff that we've covered before AND drops a row at the page break. The gt version also drops rows and then the second page of the table looks really odd.

pdf_test_mwe.pdf

OskaratOmni commented 4 months ago

PS: I also tried gt_split() with omni_table() and that only returned an error. I haven't worked on debugging it.

We could manually split into two tables as a work around.

dgkeyes commented 4 months ago

Oh yeah, you're right about flextable missing a row. I hadn't noticed that.

So there are two options here:

  1. You can just split tables (we usually just do this).
  2. We can dive into the code and make this work. That will take longer, but probably give you a better long-term solution.

Let us know what you'd like to do.

OskaratOmni commented 4 months ago

Thanks @dgkeyes , I think we just split the tables for now.

Maybe next month we return to this, and see if there's a way to modify the omni_table() function to work with gt_split(), which seems handy.

Hopefully new package updates and bug fixes will take care of this situation and we can go to using flextable(). Surely the issue with printing the tick marks and '= html' in knitted output gets resolved soon. (We still don't know why you are immune to this but Thomas and I aren't).

dgkeyes commented 4 months ago

Ok, I'll close this issue for now, but we can open it back up later.