Skip to content

Commit

Permalink
feat: plots now accept chunk options out.width and out.height
Browse files Browse the repository at this point in the history
- only if expressed in percent.

fix #144
  • Loading branch information
davidgohel committed Oct 27, 2024
1 parent 040e39c commit fbaa3c2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
Expand Down
15 changes: 14 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
28 changes: 28 additions & 0 deletions R/hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fbaa3c2

Please sign in to comment.