davidgohel / flextable

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

Wrong caption after officer::body_add_flextable() #509

Closed erydit closed 1 year ago

erydit commented 1 year ago

Hello again!

flextable_0.8.5 officer_0.5.2

When I save a flextable with a caption to docx using body_add_flextable(), a word "Table: " adds to a caption in the resulted document.

Code:

library(officer)
library(flextable)

ft <- iris %>% flextable() %>% set_caption("flxname")
x <- read_docx()
x <- body_add_flextable(x, value = ft)

print(x, "Desktop/x.docx")

And in Word i get: 1

But I expect the caption without "Table: ". The "Table: " also adds if i use a template with predefined styles for table captions. Previous versions of flextable and officer worked as expected. In addition, now Word ask "The document contains fields that link to other files." at every opening of the generated file. That behavior also have come with the last updates.

resulted.docx

R version 4.2.2 (2022-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Manjaro Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.11.0
LAPACK: /usr/lib/liblapack.so.3.11.0

locale:
 [1] LC_CTYPE=ru_RU.UTF-8       LC_NUMERIC=C               LC_TIME=ru_RU.UTF-8        LC_COLLATE=ru_RU.UTF-8    
 [5] LC_MONETARY=ru_RU.UTF-8    LC_MESSAGES=ru_RU.UTF-8    LC_PAPER=ru_RU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=ru_RU.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] flextable_0.8.5 officer_0.5.2   magrittr_2.0.3 

loaded via a namespace (and not attached):
 [1] zip_2.2.2         Rcpp_1.0.10       compiler_4.2.2    later_1.3.0       base64enc_0.1-3   gfonts_0.2.0      tools_4.2.2      
 [8] digest_0.6.31     uuid_1.1-0        evaluate_0.20     jsonlite_1.8.4    memoise_2.0.1     lifecycle_1.0.3   rlang_1.0.6      
[15] shiny_1.7.4       cli_3.6.0         rstudioapi_0.14   crul_1.3          curl_5.0.0        xfun_0.37         fastmap_1.1.0    
[22] knitr_1.42        xml2_1.3.3        gdtools_0.3.0     askpass_1.1       systemfonts_1.0.4 grid_4.2.2        glue_1.6.2       
[29] httpcode_0.3.0    data.table_1.14.6 R6_2.5.1          rmarkdown_2.20    promises_1.2.0.1  htmltools_0.5.4   ellipsis_0.3.2   
[36] mime_0.12         xtable_1.8-4      httpuv_1.6.8      openssl_2.0.5     cachem_1.0.6      crayon_1.5.2  
davidgohel commented 1 year ago

I think best is to stop using captions.

This is just a paragraph, add a paragraph before or after the table and that's it.

davidgohel commented 1 year ago

The expected behavior is now back (https://github.com/davidgohel/flextable/commit/fb7bd2bbcb18172d221ecc2cd2a23f432592c711). If you only need a simple caption in a Word document (from read_docx()) you can also replace caption with a simple paragraph associated with your caption style, i.e.:

library(officer)
library(flextable)

ft <- iris %>% flextable()
x <- read_docx()
x <- 
x <- body_add_flextable(x, value = ft)
x <- body_add_par(x, value = "flxname", style = "Table Caption")
print(x, "x.docx")
github-actions[bot] commented 1 year 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.