davidgohel / flextable

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

Need to manually adjust height of cells in MS Word tables for long text #167

Closed alan-y closed 4 years ago

alan-y commented 4 years ago

flextable used to automatically adjust the height of cells in tables outputted to MS Word to accomodate the amount of text but does not seem to anymore.

library(tidyverse)
library(flextable)

z_df <- iris %>% 
    as_tibble() %>% 
    rename(Sepal.Length.lets.make.this.very.long = Sepal.Length)

flextable(z_df) %>% 
    width(j = 1, width = 1) %>% 
    print(preview = "docx")

Now need to explicitly adjust the height with e.g.

flextable(z_df) %>% 
    width(j = 1, width = 1) %>% 
    height(height = 0.75, part = "header") %>% 
    print(preview = "docx")

Here is my session info for completeness. I am also using MS Word 2007 in case that may be an issue.

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)

Matrix products: default

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

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

other attached packages:
 [1] flextable_0.5.6 forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.3     readr_1.3.1    
 [7] tidyr_1.0.0     tibble_2.1.3    ggplot2_3.2.1   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] xfun_0.11         tidyselect_0.2.5  haven_2.2.0       lattice_0.20-38   colorspace_1.4-1 
 [6] vctrs_0.2.0       generics_0.0.2    htmltools_0.4.0   base64enc_0.1-3   utf8_1.1.4       
[11] rlang_0.4.2       pillar_1.4.2      glue_1.3.1        withr_2.1.2       DBI_1.0.0        
[16] gdtools_0.2.1     dbplyr_1.4.2      uuid_0.1-2        modelr_0.1.5      readxl_1.3.1     
[21] lifecycle_0.1.0   munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0  zip_2.0.4        
[26] rvest_0.3.5       evaluate_0.14     knitr_1.26        fansi_0.4.0       broom_0.5.2      
[31] Rcpp_1.0.3        scales_1.1.0      backports_1.1.5   jsonlite_1.6      fs_1.3.1         
[36] systemfonts_0.1.1 hms_0.5.2         digest_0.6.23     stringi_1.4.3     grid_3.6.1       
[41] cli_1.1.0         tools_3.6.1       magrittr_1.5      lazyeval_0.2.2    crayon_1.3.4     
[46] pkgconfig_2.0.3   zeallot_0.1.0     data.table_1.12.6 xml2_1.2.2        reprex_0.3.0     
[51] lubridate_1.7.4   rmarkdown_1.18    officer_0.3.6     assertthat_0.2.1  httr_1.4.1       
[56] rstudioapi_0.10   R6_2.4.1          nlme_3.1-142      compiler_3.6.1   
davidgohel commented 4 years ago

No, the function to do that is autofit() but it has never been called systematically.

You can read more about flextable here: https://davidgohel.github.io/flextable/

and the function autofit is documented here: https://davidgohel.github.io/flextable/reference/autofit.html

alan-y commented 4 years ago

I have tried autofit() but the height always seems to stay the same and it only adjusts the width

davidgohel commented 4 years ago

This is what I get with the code adapted from your repex:

library(tidyverse)
library(flextable)

z_df <- iris %>% 
  as_tibble() %>% 
  rename(Sepal.Length.lets.make.this.very.long = Sepal.Length)

flextable(z_df) %>% 
  width(j = 1, width = 1) %>% 
  height(height = 3, part = "header") %>% 
  print(preview = "docx")
Capture d’écran 2019-11-27 à 21 40 28
flextable(z_df) %>% 
  autofit() %>%  
  print(preview = "docx")
Capture d’écran 2019-11-27 à 21 40 34

Don't you have the same results??

alan-y commented 4 years ago

Yeah I get the same results. Sorry I think the confusion is that I want to keep the width as 1 but stretch the height to fit the text, keeping the width fixed This is because sometimes autofit will make the width stretch beyond the dimensions of the page

p.s. in a previous version of flextable, if I set the width to be 1, the height would automatically adjust to fit the text without running autofit() or having to manually adjust with height() - this is what I mean.

davidgohel commented 4 years ago

Ah ok. This has never been a functionality I think :) There is no implemented option for the functionality you are after, sorry

alan-y commented 4 years ago

hmm maybe it used to do this but was unintended? I know it used to do this because I'm writing up a paper and just had to write code to adjust the heights after updating the package... I'll try to generate some screenshots along with a reprex (maybe in the next couple of days).

Anyway if it was never intended to adjust the heights, then that's ok but just interesting!

davidgohel commented 4 years ago

So sorry!

I just went through some of the recent changes and yes, there has been a change. It's related to #151. I will implement an option to allow the choice....

alan-y commented 4 years ago

No problem - thanks for looking into this 👍

eusebe commented 4 years ago

Hi all,

Same "feature" here after updating the package, I need to manually adjust the height of each table... I am not sure to clearly undestand #151 , but an option would be great!

Best, David

rishi-gupta-md commented 4 years ago

I too would request that this be Fixed. Going through the thread, I realize that the functionality that row heights would automatically adjust to fit content was accidental, but it was crucial to some scenarios, especially those that I encounter.

I would also request if you could suggest a quick workaround till this is fixed.

Thanks so much for your wonderful work. Regards

alice-hannah commented 4 years ago

Hi all,

I'm having the same issue - a solution and/or workaround would be much appreciated 👍

Thanks, Alice

TomasHovorka commented 4 years ago

+1

davidgohel commented 4 years ago

I have created a new function named hrule to let users select rule to apply to row height.

Argument rule specifies the meaning of the height. Possible values are "atleast" (height should be at least the value specified), "exact" (height should be exactly the value specified), or the default value "auto" (height is determined based on the height of the contents, so the value is ignored).

The following code will produce the following outputs :

library(officer)
library(tidyverse)
library(flextable)

z_df <- iris %>% 
  as_tibble() %>% 
  rename(Sepal.Length.lets.make.this.very.long = Sepal.Length)

z <- flextable(z_df) %>% 
  autofit() %>% 
  width(j = 1, width = 1) %>% 
  height(i = 1, height = 0.5, part = "header") %>% 
  hrule(rule = "auto", part = "all") # this is the default option so that command is not really useful

print(z, preview = "html")
print(z, preview = "docx")
print(z, preview = "pptx")
Capture d’écran 2020-01-28 à 17 58 28

If using hrule(rule = "exact", part = "all") you would get:

Capture d’écran 2020-01-28 à 18 14 47

Setting rule to exact will have no effect on pptx output. On html output, text overflow is hidden but in word text overflow let break words.

alan-y commented 4 years ago

fantastic, sounds great, thanks David!

rishi-gupta-md commented 4 years ago

Great!! Thanks so much David!!

alice-hannah commented 4 years ago

Brilliant - thanks so much 👍

davidgohel commented 4 years ago

Thanks all, I am closing the issue then!

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.