Skip to content

Add a check on value in body_add_flextable() #357

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

Merged
merged 1 commit into from
Nov 14, 2021
Merged

Add a check on value in body_add_flextable() #357

merged 1 commit into from
Nov 14, 2021

Conversation

DanChaltiel
Copy link
Contributor

Hi David,

Just a very minor PR here.

I find myself wanting to insert dataframes directly into officer documents more often than I'd like to admit, and the error message that I get then is not very clear:

library(tidyverse)
officer::read_docx() %>% flextable::body_add_flextable(iris)
#> Error: $ operator is invalid for atomic vectors

With that stopifnot() addition, it will be clearer that the problem is that value is not a flextable.

In fact, I think that body_add_flextable() might even accept dataframes by design.

For instance you could consider something like this:

body_add_flextable <- function( x, value, align = "center", pos = "after", split = FALSE,
                                topcaption = TRUE, ...) {
  if(is.data.frame(value)) value <- flextable(value, ...) # pass on the ellipsis

  stopifnot(inherits(x, "rdocx"))
  stopifnot(inherits(value, "flextable"))
  # rest of the function
}

@codecov-commenter
Copy link

Codecov Report

Merging #357 (f848567) into master (748de09) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #357   +/-   ##
=======================================
  Coverage   63.31%   63.32%           
=======================================
  Files          37       37           
  Lines        4430     4431    +1     
=======================================
+ Hits         2805     2806    +1     
  Misses       1625     1625           
Impacted Files Coverage Δ
R/body_add_flextable.R 19.44% <100.00%> (+2.30%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 748de09...f848567. Read the comment docs.

@davidgohel
Copy link
Owner

Thanks @DanChaltiel

@davidgohel davidgohel merged commit e9f6f65 into davidgohel:master Nov 14, 2021
@DanChaltiel
Copy link
Contributor Author

:-)
But what about if(is.data.frame(value)) value <- flextable(value, ...)?

@davidgohel
Copy link
Owner

this would overcomplicate things, users would ask 10000 new options.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants