davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
554 stars 79 forks source link

Splitting up a flextable from a flextable object #590

Closed phargarten2 closed 6 months ago

phargarten2 commented 10 months ago

Thank you for creating the flextable package. I find it great to use in reports and presentations. Situations arise where a flextable that works in a report does not work on a slide because the table is too big. Consider this example:

library(flextable)
library(magrittr)
ft <- flextable::flextable(
  iris[c(1:8, 50:58), ]
)
ft

image

This table would be fine for a report, but on a slide it is way too big. I would need to split it on different slides.

ft %>%
  flextable::set_caption("") %>%   # No captions in presentation
  fontsize(size = 14, part = "all")   #Text needs to be bigger in presentation

image

Is there a way to split up the flextable (ft), say by Species? Or even specify that I would want the first 8 rows one slide and the last 8 rows on the next? Or, according to #95, I would need to do all this in the dataset, like iris, before producing a flextable.

flextable::flextable(
  iris[c(1:8), ]
)%>%
  flextable::set_caption("") %>%   # No captions in presentation
  fontsize(size = 14, part = "all")   #Text needs to be bigger in presentation

flextable::flextable(
  iris[c(50:58), ]
)%>%
  flextable::set_caption("") %>%   # No captions in presentation
  fontsize(size = 14, part = "all")   #Text needs to be bigger in presentation

image image

=============================================================================== Not a bug or feature request? If you are looking for help using the package correctly, please read the doc first at https://davidgohel.github.io/flextable/. You can also visit Stackoverflow and tag your question with [flextable]. We usually read them and answer WHEN possible.

davidgohel commented 6 months ago

For that, you can play with delete_rows() and maybe flextable_dim(ft) that return the whole width and height or dim(ft) that return the widths and the heights.

library(flextable)
x <- iris
rownum <- seq_len(nrow(x))
z <- flextable(x) |> autofit()

z1 <- delete_rows(z, i = setdiff(rownum, 1L:40L))
flextable_dim(z1)$heights
z2 <- delete_rows(z, i = setdiff(rownum, 41:80))
flextable_dim(z2)$heights
z3 <- delete_rows(z, i = setdiff(rownum, 81:120))
z4 <- delete_rows(z, i = setdiff(rownum, 121:150))
phargarten2 commented 6 months ago

Thank you, David! The inclusion of delete_rows() is a great addition to v. 0.9.4. Even better, I can use the row selectors to delete the rows not needed as well, which totally answers the question.

library(flextable)
x <- iris
rownum <- seq_len(nrow(x))
z <- flextable(x) |> autofit()
delete_rows(ft, ~ Species == "setosa", part = "body")
delete_rows(ft, ~ Species == "versicolor", part = "body")
github-actions[bot] commented 3 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.