flextable icon indicating copy to clipboard operation
flextable copied to clipboard

fontsize() does not modify size of empty cells

Open fabern opened this issue 5 years ago • 14 comments

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  

fabern avatar Sep 19 '19 13:09 fabern

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?

sbihorel avatar Jul 14 '20 20:07 sbihorel

Is there a workaround?

If only about managing NA, you can use:

myft <- set_formatter_type(myft, na_str = " ")

davidgohel avatar Jul 15 '20 09:07 davidgohel

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).

sbihorel avatar Jul 15 '20 12:07 sbihorel

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

davidgohel avatar Jul 15 '20 15:07 davidgohel

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

sbihorel avatar Jul 15 '20 17:07 sbihorel

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.

davidgohel avatar Jul 15 '20 17:07 davidgohel

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!

mattkumar avatar Nov 09 '21 13:11 mattkumar

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 avatar Nov 09 '21 13:11 davidgohel

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

davidgohel avatar May 30 '22 08:05 davidgohel

Hi

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

abduazizR avatar Jun 12 '23 18:06 abduazizR

It's still open :)

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

davidgohel avatar Jun 12 '23 18:06 davidgohel