davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
558 stars 80 forks source link

fontsize() does not modify size of empty cells #153

Open fabern opened 5 years ago

fabern commented 5 years ago

Hi David,

fontsize() does not modify size of empty cells. If printed to Word, the corresponding cells have the initial text size.

Best regards, Fabian

Reproducible example:

dat_frame <- head(iris)

# Make empty cell to show behavior
dat_frame[2,"Species"] <- ""

myft <- flextable::flextable(dat_frame)
myft <- flextable::fontsize(myft, part = "all", size = 4)
myft <- flextable::height_all(myft, height = 0.0001)

print(myft, preview = "docx")

sessionInfo():

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1          digest_0.6.15       R6_2.2.2            magrittr_1.5        evaluate_0.10.1     zip_2.0.1          
 [7] stringi_1.1.6       rlang_0.3.0.1       gdtools_0.1.7       uuid_0.1-2          data.table_1.10.4-3 xml2_1.2.0         
[13] flextable_0.5.5     rmarkdown_1.11      tools_3.4.3         stringr_1.3.0       officer_0.3.3       yaml_2.2.0         
[19] compiler_3.4.3      base64enc_0.1-3     htmltools_0.3.6     knitr_1.20  
sbihorel commented 4 years ago

Hi,

It seems that the style of empty cells (NA or "") revert to what the Normal style in the Word template is. This issue can be quite disruptive since, when one applies a smaller font size compare to the default font size of the Normal style, the height of any row containing empty cells will be different than that of rows that do not contain any empty cells.

Is there a workaround?

davidgohel commented 4 years ago

Is there a workaround?

If only about managing NA, you can use:

myft <- set_formatter_type(myft, na_str = " ")
sbihorel commented 4 years ago

Hi David,

While the workaround you provided works for simple tables, it does not in the complex case I am working on. It is proprietary stuff so I cannot share it and would need to make a vanilla regex that would reproduce the problem.

More generally, I guess one way to solve the problem would be to be able to apply a named Word style on the flextable content (something akin to the style argument of officer::body_add_table for example).

davidgohel commented 4 years ago

be able to apply a named Word style on the flextable content

No because this would reduce flextable output to only Word

I'll try to work on that soon

sbihorel commented 4 years ago

Well, the property could be used selectively by body_add_flextable()... Just a thought...

davidgohel commented 4 years ago

It's a solution, thanks. but I think it is better to fix it in the xml generation which is text processing only and avoid xml processing that can really slow down the production.

mattkumar commented 2 years ago

Hi all, just curious to see if this is still a live discussion point. I recently ran up against this myself.

I'm not quite sure how the "set_formatter_type" workaround can be applied - is it possible to elaborate?

Many thanks!

davidgohel commented 2 years ago

Yes, it's still open :)

Sorry, I don't have yet a technical solution for this.

For this case, it does not create a "" but a NA as the column is a factor. Then, this workaround can be applied:

library(flextable)
dat_frame <- head(iris)
set_flextable_defaults(na_str = " ")

# Make empty cell to show behavior
dat_frame[2,"Species"] <- ""

myft <- flextable(dat_frame)
myft <- fontsize(myft, part = "all", size = 4)

print(myft, preview = "docx")
davidgohel commented 2 years ago

Solution for this issue consists in adding default formatting properties for runs inside paragraphs formatting properties

abduazizR commented 1 year ago

Hi

I could not understand the solution. Could you please clarify with an example, because I am still facing the same issue

davidgohel commented 1 year ago

It's still open :)

Sorry, I don't have yet a technical solution for this.

davidgohel commented 1 week ago

It should be fixed in the dev version now

UUEM commented 2 days ago

I'm still having some difficulty, If I include the code below in a rmarkdown file and render the report in powerpoint, the "empty" cells have a default font of 13.5 instead of the 7 I specified.

library(dplyr)
library(flextable)

set_flextable_defaults(na_str = " ")

data.frame(Category = c("Ford", "Volkswagen", "Tesla"),
           Weight   = c(1000, 750, 1200),
           Speed    = c(60, 45, 70)) %>%
  flextable(cwidth   = c(2, 1, 1, 2),
            col_keys = c("Category", "Weight", "blank", "Speed")) %>%
  empty_blanks(width = 0.2,
               part  = 'all') %>%
  fontsize(size = 7,
           part = 'all') 
davidgohel commented 1 day ago

Thank you I only looked and fixed word version