From fbaa3c2c1b490b97870ed48121a66cabbfd62b8d Mon Sep 17 00:00:00 2001 From: David Gohel Date: Sun, 27 Oct 2024 23:20:53 +0100 Subject: [PATCH] feat: plots now accept chunk options `out.width` and `out.height` - only if expressed in percent. fix #144 --- DESCRIPTION | 2 +- NEWS.md | 15 ++++++++++++++- R/hooks.R | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9d5bb93..cb12284 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: officedown Type: Package Title: Enhanced 'R Markdown' Format for 'Word' and 'PowerPoint' -Version: 0.4.0.001 +Version: 0.4.0.002 Authors@R: c( person("David", "Gohel", role = c("aut", "cre", "cph"), email = "david.gohel@ardata.fr"), diff --git a/NEWS.md b/NEWS.md index d120c03..c484107 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,17 @@ -# officedown 0.3.2 +# officedown 0.4.0 + +## new feature + +- plots now accept chunk options `out.width` and `out.height` +if expressed in percent. #144 + +# officedown 0.3.3 + +## issues + +- fix manual links for CRAN check + +# officedown 0.3.3 ## issues diff --git a/R/hooks.R b/R/hooks.R index 41106df..62019e3 100644 --- a/R/hooks.R +++ b/R/hooks.R @@ -35,11 +35,39 @@ plot_word_fig_caption <- function(x, options) { )) cap_str <- to_wml(bc, knitting = TRUE) + # physical size of plots fig.width <- opts_current$get("fig.width") if(is.null(fig.width)) fig.width <- 5 fig.height <- opts_current$get("fig.height") if(is.null(fig.height)) fig.height <- 5 + # out.width and out.height in percent + fig.out.width <- opts_current$get("out.width") + has_fig_out_width <- !is.null(fig.out.width) + is_pct_width <- has_fig_out_width && grepl("%", fig.out.width) + if (is_pct_width) { + fig.out.width <- gsub("%", "", fig.out.width, fixed = TRUE) + fig.out.width <- as.numeric(fig.out.width) / 100 + fig.out.height <- fig.out.width + } else { + fig.out.height <- opts_current$get("out.height") + has_fig_out_height <- !is.null(fig.out.height) + is_pct_height <- !is_pct_width && has_fig_out_height && grepl("%", fig.out.height) + if (is_pct_height) { + fig.out.height <- gsub("%", "", fig.out.height, fixed = TRUE) + fig.out.height <- as.numeric(fig.out.height) / 100 + fig.out.width <- fig.out.height + } + } + + if (!has_fig_out_width && !has_fig_out_height) { + fig.out.width <- 1 + fig.out.height <- 1 + } + + fig.width <- fig.width * fig.out.width + fig.height <- fig.height * fig.out.height + img <- external_img(src = x[1], width = fig.width, height = fig.height, alt = options$fig.alt) doc <- get_reference_rdocx()