[This article was first published on R on ArData, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. Export table as png
* Function gen_grob
+ Simple usage
+ The resizing options
+ Content wrapping
One of the great features of the flextable package is the ability to workwith ‘Grid graphics’ format. The gen_grob() function generates a grob*(Graphical Object) from a flextable, which can then be added to a ggplot graphicusing the ‘patchwork’ package or with ggplot2::annotation_custom().
These features require the use of a graphical output using‘systemfonts’: svglite::svglite(), ragg::agg_png() or ggiraph::dsvg().These devices must be used to ensure all fonts you are using will be recognizedby the R graphical device.
The graph shown above is produced with the function gen_grob().
the associated code is available in this articleof the ‘flextable gallery’.
Export table as pngPast versions of ‘flextable’ used packages ‘webshot’ or ‘webshot2’ to get an imageversion of the HTML table. This mechanism has been removed and replaced by theuse of ‘Grid graphics’ output and the ‘ragg’ package. This allows for a fasterprocess and makes maintenance easier.
First, we define some global parameters.
library(flextable)library(gdtools)set\_flextable\_defaults( font.family = "Open Sans", font.color = "#333333", theme\_fun = theme\_vanilla, digits = 2, big.mark = "", na\_str = "")register\_gfont("Open Sans") # register Open Sans
To export a flextable to a png file, we need to call the save_as_image() function.Let’s illustrate with an flextable made with the dataset ‘palmerpenguins::penguins’.
library(palmerpenguins)filename <- "head-penguins.png"ft <- as\_flextable(palmerpenguins::penguins)save\_as\_image(ft, path = filename)knitr::include\_graphics(filename)
Function gen_grobLet’s illustrate with the previous ‘flextable’ and a new ‘ggplot’.
library(tidyverse)g1 <- ggplot(penguins, aes(x = bill\_length\_mm, y = bill\_depth\_mm, color = species)) + geom\_point() + scale\_color\_viridis\_d() + theme\_minimal() + theme(plot.margin = unit(c(0, 0, 30, 0), "pt"))
Simple usageThe function gen_grob() will by default adapt to the space available for the table.
library(patchwork)g1 / gen\_grob(ft, just = "bottom")
It should work with any function that support grid objects:
g1 + inset\_element(gen\_grob(ft |> bg(bg = "#EFEFEFAA", part = "all"), just = "bottom"), 0.1, 0.25, .9, .75) + theme( plot.background = element\_rect(fill = "transparent"), panel.background = element\_rect(fill = "transparent") )
The resizing optionsOf course, it is possible to take control over this automatic resizing.
For this we will use a small table that will help to demo some options.
set\_flextable\_defaults(font.family = "Fira Sans Condensed")register\_gfont("Fira Sans Condensed")dummy\_ft <- data.frame(zzzz = ";)") |> flextable() |> color(color = "white", part = "all") |> mk\_par( value = as\_paragraph( as\_chunk("made", props = fp\_text\_default(font.size = 30, color = "#f2af00")), as\_chunk(" with\n", props = fp\_text\_default(color = "gray", font.size = 15)), as\_chunk("flextable", props = fp\_text\_default(color = "#c32900", font.size = 45)) ), part = "header") |> autofit() |> align(align = "center", part = "all") |> border\_outer(border = fp\_border\_default(width = 0))
HTML version of the table is the following:
dummy\_ft
| made withflextable | | --- | | |
The graphic below shows some possible resizing options.
fit = "width") andthe fonts will be resized to the maximum possible (scaling = "full").fit = "fixed").fit = "auto").design <- "AAA BBC"wrap\_plots( dummy\_ft |> bg(bg = "#ff006e", part = "all") |> gen\_grob(fit = "width", scaling = "full", just = "bottom"), dummy\_ft |> bg(bg = "#3a86ff", part = "all") |> gen\_grob(fit = "fixed"), dummy\_ft |> bg(bg = "#ffbe0b", part = "all") |> gen\_grob(), design = design)
Content wrappingAn algorithm is used to wrap content within the available space of the cell.
library(safetyData)adsl <- adam\_adsl[, c("AGE", "SEX", "ARM")]dat <- summarizor(adsl, by = "ARM")ft <- as\_flextable(dat, spread\_first\_col = TRUE)ft |> bg(bg = "#EFEFEF", part = "all") |> plot(fit = "width", wrapping = TRUE, scaling = FALSE)
It can be disabled with the wrapping = FALSE argument.
ft |> bg(bg = "#EFEFEF", part = "all") |> plot(fit = "width", wrapping = FALSE, scaling = FALSE)
To leave a comment for the author, please follow the link and comment on their blog: R on ArData.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: flextable for ‘Grid graphics’