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

FR: Both width and style of table #142

Closed
pank opened this issue Jul 11, 2018 · 7 comments
Closed

FR: Both width and style of table #142

pank opened this issue Jul 11, 2018 · 7 comments

Comments

@pank
Copy link

pank commented Jul 11, 2018

Hi,

Thank you for providing and working on officer. It is a very neat tool! I hope this is the right place to ask this (as opposed to the flextable issues page or stackoverflow).

In a docx file, I hope to make a table with a certain width and a certain Word style, defined by the initial document.
As far as I can tell, I can get my desired width via flextable and my desired style via officer (body_add_table), but I am struggling to get both.

Example:

library(magrittr)
library(officer)
library(flextable)

desired_style='Table Professional' # really my_imposed_word_table_style

df <- with(list(var=LETTERS[1:3], val=1:3,
                blank0=rep("", 3),
                blank10=rep(strrep("", 10))),
           data.frame(Variable=var,
                      Initial.value=val,
                      Addition=blank0,
                      Reason=blank10,
                      Total=blank0))

cm_to_inches <- 0.3937008
desired_width <- c(3, 1.75, 1.75, 8, 1.75) * cm_to_inches
ft <- flextable(df) %>% width(seq_along(df), desired_width)

doc <- read_docx()
doc %<>% body_add_par(paste("The desired outcome is a table in the 'desired' style",
                            "that is the width of the page, or at least a certain width."),
                      style="Normal") %>%
    body_add_par("", style = "Normal") %>%
    body_add_par("This table will be too short, as some values will only be filled out later",
                 style = "Normal") %>%
    body_add_par("", style = "Normal") %>%
    body_add_table(df, style=desired_style) %>%
    body_add_par("", style = "Normal") %>%
    body_add_par("For the flextable, I can't seem to specify my desired style",
                 style = "Normal") %>%
    body_add_par("", style = "Normal") %>%
    body_add_flextable(ft)
print(doc, target="dummy.docx")

Output:

dummy

Additional notes:

  • I am not a Word expert, but it seems that I cannot fix this with a new style as styles cannot dictate the width.
  • I tried to insert a long string to get the desired proportions of the body_add_table table and replacing a long string with "" later, but it did not work.
  • I don't know anything about office xml, but looking at the two following strings, it seems that the style of the officer table is determined by <w:tblStyle w:val=\"Table Professional\"/>. However, I don't know if that would "overrule" all the inlined style of ft_str (probably not). Would it be possible to have a "blank" flextable theme that simply adds a w:tblStyle? E.g. ft %>% theme_word_style(style=desired_word_document_style)
of_str <- officer:::wml_table(officer:::characterise_df(df), desired_style)
ft_str <- flextable:::docx_str.regulartable(ft)

Thank you for reading this,
Rasmus

PS: sessionInfo

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

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

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

other attached packages:
[1] magrittr_1.5    flextable_0.4.4 officer_0.3.1  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.17    lattice_0.20-35 zoo_1.8-2       rprojroot_1.3-2 digest_0.6.15   grid_3.4.3      R6_2.2.2       
 [8] backports_1.1.2 evaluate_0.10.1 zip_1.0.0       stringi_1.1.7   gdtools_0.1.7   uuid_0.1-2      xml2_1.2.0     
[15] xts_0.10-2      rmarkdown_1.10  functional_0.6  tools_3.4.3     stringr_1.3.1   compiler_3.4.3  base64enc_0.1-3
[22] htmltools_0.3.6 knitr_1.20
@davidgohel
Copy link
Owner

Hello Rasmus

Thanks, your email is very clear. I will take time later to answer your question (probably this we).

@davidgohel
Copy link
Owner

This is what I would do to reproduce your table:

library(magrittr)
library(officer)
library(flextable)

df <- with(list(var=LETTERS[1:3], val=1:3,
                blank0=rep("", 3),
                blank10=rep(strrep("", 10))),
           data.frame(Variable=var,
                      Initial.value=val,
                      Addition=blank0,
                      Reason=blank10,
                      Total=blank0))

ft <- flextable(df) %>% 
  theme_box() %>% 
  bg(bg = "black", part = "header") %>% 
  color(color = "white", part = "header") %>% 
  bold(bold = TRUE, part = "header") %>% 
  fontsize(size = 12, part = "all") %>% 
  font(fontname = "Cambria", part = "all") %>% 
  autofit()

doc <- read_docx() %>% 
  body_add_flextable(ft)
print(doc, target="dummy.docx")

capture d ecran 2018-07-17 a 10 35 13

I am not sure how to help on the width aspect of you columns. You can use autofit, it will auto adjust width (so that content always fit on one row). You can also use dim_pretty and use all computed widths except the corresponding width for Addition.

Would it be possible to have a "blank" flextable theme that simply adds a w:tblStyle?

No, there are already some theme_ functions. They do not depend of any Word template. Also you can derivate one of these functions to create your own style.

@pank
Copy link
Author

pank commented Jul 17, 2018

Thanks for the example.

I have been given a table template by my organization which I am supposed to use for their documents. It would be better to use the official style rather than reproducing it, but perhaps that is the only way to gain the added flexibility of flextables.

Kind regards,
Rasmus

@davidgohel
Copy link
Owner

davidgohel commented Jul 17, 2018

OK,

I will give a try later, it should be possible :)

@davidgohel
Copy link
Owner

Need to be able to specify widths with function body_add_table()

@davidgohel
Copy link
Owner

This issue is opened since too much time. Sorry. I will re open when I will find time for it

KR
David

@github-actions
Copy link

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.

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

No branches or pull requests

2 participants