The solution was existing since a long time but was buried in a stackoverflow question instead of being provided in flextable as an option.
First, let’s create a simple flextable.
library(flextable) ft <- flextable( head( mtcars ) ) ft <- autofit(ft) ft
You can save it as a png:
save_as_image(ft, path = "name.png")
You can plot it:
Or combine the table with a ggplot object:
library(ggplot2) library(grid) library(cowplot) ft_raster <- as_raster(ft) anyplot <- qplot(speed, dist, data = cars, geom = "point") gflextable <- ggplot() + theme_void() + annotation_custom(rasterGrob(ft_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) plot_grid(anyplot, gflextable, nrow = 2, ncol = 1, rel_heights = c(4, 1) )
About width and height
When a flextable is printed, it may be useful to know the exact width and height
of the table to be sure to set up the correct aspect ratio. The function
flextable_dim() will provide these informations without the need to produce
dims <- flextable_dim(ft) dims
#> $widths #>  7.948045 #> #> $heights #>  2.035298 #> #> $aspect_ratio #>  0.2560753
You can reuse them as values for knitr chunk options