From a24947d6db0e037ce8324d2d1e25345debb5e274 Mon Sep 17 00:00:00 2001 From: David Gohel Date: Tue, 5 Mar 2024 22:27:18 +0100 Subject: [PATCH] doc: improve gen_grob manual --- DESCRIPTION | 2 +- R/grid_grobs.R | 38 +++++++++++++++++++++++--------------- man/gen_grob.Rd | 40 ++++++++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 16835ca5..80a41add 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,7 @@ License: GPL-3 Imports: stats, utils, grDevices, graphics, grid, rmarkdown, knitr, htmltools, rlang, ragg, - officer (>= 0.6.5), gdtools (>= 0.3.3), + officer (>= 0.6.5), gdtools (>= 0.3.6), xml2, data.table (>= 1.13.0), uuid (>= 0.1-4) RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE) diff --git a/R/grid_grobs.R b/R/grid_grobs.R index a96e04a7..d4586c7c 100644 --- a/R/grid_grobs.R +++ b/R/grid_grobs.R @@ -83,25 +83,33 @@ #' #' @return a grob (gTree) object made with package `grid` #' @examples +#' library(ragg) +#' library(gdtools) +#' register_liberationsans() +#' +#' set_flextable_defaults(font.family = "Liberation Sans") +#' #' ft <- flextable(head(mtcars)) -#' ft <- autofit(ft) +#' #' gr <- gen_grob(ft) #' -#' used_family <- get_flextable_defaults()$font.family -#' if (gdtools::font_family_exists(used_family) && -#' require("ragg")) { -#' png_f <- tempfile(fileext = ".png") -#' # get the size -#' dims <- dim(gr) -#' dims -#' ragg::agg_png( -#' filename = png_f, width = dims$width + .1, -#' height = dims$height + .1, units = "in", res = 150 -#' ) -#' plot(gr) -#' dev.off() -#' } +#' png_f_1 <- tempfile(fileext = ".png") +#' ragg::agg_png( +#' filename = png_f_1, width = 4, height = 2, +#' units = "in", res = 150) +#' plot(gr) +#' dev.off() #' +#' png_f_2 <- tempfile(fileext = ".png") +#' # get the size +#' dims <- dim(gr) +#' dims +#' ragg::agg_png( +#' filename = png_f_2, width = dims$width + .1, +#' height = dims$height + .1, units = "in", res = 150 +#' ) +#' plot(gr) +#' dev.off() #' @family flextable print function #' @importFrom grid gTree gen_grob <- function(x, diff --git a/man/gen_grob.Rd b/man/gen_grob.Rd index 7a376d2e..3b1b0889 100644 --- a/man/gen_grob.Rd +++ b/man/gen_grob.Rd @@ -101,25 +101,33 @@ The size of the flextable can be known by using the method } \examples{ +library(ragg) +library(gdtools) +register_liberationsans() + +set_flextable_defaults(font.family = "Liberation Sans") + ft <- flextable(head(mtcars)) -ft <- autofit(ft) -gr <- gen_grob(ft) -used_family <- get_flextable_defaults()$font.family -if (gdtools::font_family_exists(used_family) && - require("ragg")) { - png_f <- tempfile(fileext = ".png") - # get the size - dims <- dim(gr) - dims - ragg::agg_png( - filename = png_f, width = dims$width + .1, - height = dims$height + .1, units = "in", res = 150 - ) - plot(gr) - dev.off() -} +gr <- gen_grob(ft) +png_f_1 <- tempfile(fileext = ".png") +ragg::agg_png( + filename = png_f_1, width = 4, height = 2, + units = "in", res = 150) +plot(gr) +dev.off() + +png_f_2 <- tempfile(fileext = ".png") +# get the size +dims <- dim(gr) +dims +ragg::agg_png( + filename = png_f_2, width = dims$width + .1, + height = dims$height + .1, units = "in", res = 150 +) +plot(gr) +dev.off() } \seealso{ Other flextable print function: