Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit= argument to body_add_gg, ... . Closes #626 #628

Merged
merged 2 commits into from
Nov 19, 2024

Conversation

trekonom
Copy link
Contributor

@trekonom trekonom commented Nov 9, 2024

Hi David,

this PR adds a "unit=" argument to body_add_gg, ... thereby closing #626.

The reprex below shows that it (should) work:

library(officer)
library(ggplot2)

p <- ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point()

doc <- read_docx()

doc <- body_add_gg(doc, p) # Works when unit is not set -> #323
doc <- body_add_gg(doc, p, unit = "in")
doc <- body_add_gg(doc, p, unit = "cm")
doc <- body_add_gg(doc, p, unit = "mm")

# In all 4 cases a plot is added with a width of 6 and height of 5 in units
# expressed by the unit argument

doc <- cursor_end(doc)
node <- docx_current_block_xml(doc)

.in_to_emu <- 914400; .cm_to_emu <- 360000; .mm_to_emu <- 36000

all(identical(
  as.numeric(xml2::xml_attr(xml2::xml_find_all(node, "//wp:extent"), "cx")) /
    c(.in_to_emu, .in_to_emu, .cm_to_emu, .mm_to_emu),
  rep(6, 4)
))
#> [1] TRUE

all(identical(
  as.numeric(xml2::xml_attr(xml2::xml_find_all(node, "//wp:extent"), "cy")) /
    c(.in_to_emu, .in_to_emu, .cm_to_emu, .mm_to_emu),
  rep(5, 4)
))
#> [1] TRUE

# Uses cli_abort for nice error messages

doc <- body_add_gg(doc, p, unit = "px")
#> Error in `check_unit()`:
#> ! `unit` should be one of in, cm, or mm.
#> ✖ `unit` was px".
doc <- body_add_gg(doc, p, unit = c("cm", "mm"))
#> Error in `check_unit()`:
#> ! `unit` is not length 1.
#> ✖ `unit` must be a string.
doc <- body_add_gg(doc, p, units = "cm")
#> Error in `body_add_gg()`:
#> ! Found a `units` argument. Did you mean `unit`?

…ell as body_add.gg and body_add.plot_instr.

* Add tests to check that plot size matches the chosen units.

Closes davidgohel#626.
@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.90%. Comparing base (04c9dcb) to head (2b6d911).
Report is 12 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #628      +/-   ##
==========================================
+ Coverage   83.55%   85.90%   +2.34%     
==========================================
  Files          41       43       +2     
  Lines        5832     6967    +1135     
==========================================
+ Hits         4873     5985    +1112     
- Misses        959      982      +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@davidgohel davidgohel merged commit 469fc11 into davidgohel:master Nov 19, 2024
4 checks passed
@davidgohel
Copy link
Owner

thanks a lot @trekonom and sorry for the delay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants