davidgohel / officer

:cop: officer: office documents from R
https://ardata-fr.github.io/officeverse/
Other
592 stars 106 forks source link

body_add_toc(style = "table title") not working #183

Closed MajoroMask closed 5 years ago

MajoroMask commented 5 years ago

Minimum instance:

library(magrittr)
library(officer)
library(ggplot2)

gg <- ggplot(data = iris, aes(Sepal.Length, Petal.Length)) +
  geom_point()

doc <-
  read_docx() %>%

  body_add_par(value = "plot examples", style = "heading 1") %>%
  body_add_gg(value = gg, style = "centered" ) %>%
  body_add_par(value = "graph example 1", style = "graphic title") %>%
  shortcuts$slip_in_plotref(depth = 1) %>%

  body_end_section_continuous() %>%

  body_add_par(value = "Table of graphics", style = "heading 2") %>%
  body_add_toc(style = "graphic title")

print(doc, target = "test_graphic_toc.docx")

The codes runs just fine. Thing is when opening the output .docx (MS Office for Mac version 16.22), MS word warnings me Creating a table of contents? Start by applying a heading style from the Styles gallary to the selected text. And the toc field shows No table of contents entries found.

These warning(s) can be reproduced when opening the .docx on a Win10 platform (Office version 16.0.11029.20104 x64). So I'm thinking these may be introduced by certain MS Office update.

PS: sessionInfo()

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] officer_0.3.2   fs_1.2.6        forcats_0.3.0   stringr_1.4.0   dplyr_0.7.8     purrr_0.3.0    
 [7] readr_1.3.1     tidyr_0.8.2     tibble_2.0.1    ggplot2_3.1.0   tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] zip_1.0.0        Rcpp_1.0.0       cellranger_1.1.0 pillar_1.3.1     compiler_3.5.2   plyr_1.8.4      
 [7] bindr_0.1.1      base64enc_0.1-3  tools_3.5.2      uuid_0.1-2       digest_0.6.18    jsonlite_1.6    
[13] lubridate_1.7.4  nlme_3.1-137     gtable_0.2.0     lattice_0.20-38  pkgconfig_2.0.2  rlang_0.3.1     
[19] cli_1.0.1        rstudioapi_0.9.0 yaml_2.2.0       haven_2.0.0      bindrcpp_0.2.2   withr_2.1.2     
[25] xml2_1.2.0       httr_1.4.0       generics_0.0.2   hms_0.4.2        grid_3.5.2       tidyselect_0.2.5
[31] glue_1.3.0       R6_2.3.0         readxl_1.2.0     modelr_0.1.3     magrittr_1.5     htmltools_0.3.6 
[37] backports_1.1.3  scales_1.0.0     rvest_0.3.2      assertthat_0.2.0 colorspace_1.4-0 labeling_0.3    
[43] stringi_1.2.4    lazyeval_0.2.1   munsell_0.5.0    broom_0.5.1      crayon_1.3.4 
davidgohel commented 5 years ago

Hi, thanks for reporting this.

No table of contents entries found.

Did you clicked on 'yes' when the document opened and Word shown the dialog box? (in case of, you need to clik on yes so that Word can build the TOC).

davidgohel commented 5 years ago

Closing that for now as I am not able to reproduce - will reopen if necessary

MajoroMask commented 5 years ago

Thanks for your reply @davidgohel. I did click "yes" and let Word build TOC every time I open an officer generated document. Besides I've tried to change body_add_toc() argument level = 3 to level = 1, but the problem still exists.

I also tried to open the same .docx on Windows 10 (Office version Microsoft Word for Office 365 MSO (16.0.11231.20164) x64), didn't help.

This is the .docx I generated. I think this could be of help. test_graphic_toc.docx

rnewbie commented 2 years ago

Same issue. Carefull checking reveals the fact: to_wml function inside body_add_toc adds extra ";1" to style name. Example:

> body_add_toc
function (x, level = 3, pos = "after", style = NULL, separator = ";") 
{
    bt <- block_toc(level = level, style = style, separator = separator)
    out <- to_wml(bt, add_ns = TRUE)
    body_add_xml(x = x, str = out, pos = pos)
}
<bytecode: 0x560c074cc0e0>
<environment: namespace:officer>
> bt<-block_toc(level=3, style = "MISC: Table Caption Header 8pt", separator = ";")
> str(bt)
List of 4
 $ level    : num 3
 $ style    : chr "MISC: Table Caption Header 8pt"
 $ seq_id   : NULL
 $ separator: chr ";"
 - attr(*, "class")= chr [1:2] "block_toc" "block"
> out <- to_wml(bt, add_ns = TRUE)
> out
[1] "<w:p> ... TOC \\h \\z \\t \"MISC: Table Caption Header 8pt;1\" ... </w:p>"

Fixing this manually helps, so it looks like some bug in to_wml function

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.