davidgohel / officedown

https://ardata-fr.github.io/officeverse/officedown-for-word.html
Other
366 stars 27 forks source link

Flextable in subsection not importing reference_docx correctly #52

Closed casblaauw closed 3 years ago

casblaauw commented 3 years ago

Issue

I've been using officedown to create reports for a while, and recently started to implement flextables to create tables with proper layouts. However, the structure of my report means there is one master .Rmd, which imports subsections from a subfolder. Whenever attempting to add a flextable to one of these subsections, it attempts to search for the master markdown's reference_docx within the subsections folder, even though any reference to that should be run while working in the main folder. When writing other code and text and referring to variables, everything is executed in the context of the main Rmd.

The exact error:

Quitting from lines NA-7 (./subsections/subsection.Rmd) Error: could not find file 'word_templates/template.docx' Execution halted

In terms of the reproducible example: reprex.Rmd uses reference_docx, found at word_templates/template.docx subsection.Rmd is imported in reprex.Rmd, and all of its code is executed in the context of reprex.Rmd in the base folder. However, once it has a flextable, it attempts to look for reference_docx in subsections/word_templates/template.docx.

Reproducible example

This can be reproduced with the following file structure:

reprex.rmd in the main folder

---
output:
  officedown::rdocx_document:
    reference_docx: word_templates/template.docx
---

```{r include=FALSE}
library(knitr)
library(officedown)
library(officer)
library(flextable)

### subsections/subsection.Rmd in that subfolder

output: word_document

table_test <- flextable(head(airquality))
table_test

Alongside any word file called template.docx, at the path `word_templates/template.docx`.

sessionInfo():

sessionInfo() R version 4.0.3 (2020-10-10) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Catalina 10.15.7

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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

loaded via a namespace (and not attached): [1] compiler_4.0.3 htmltools_0.5.0 tools_4.0.3 yaml_2.2.1 tinytex_0.27 rmarkdown_2.5 knitr_1.30
[8] digest_0.6.27 xfun_0.19 rlang_0.4.9 evaluate_0.14



Note: I'm not entirely sure whether this belongs in the issues of `flextable`, `officer`, or `officedown`. If you want me to repost it in another repository, please let me know.
davidgohel commented 3 years ago

I am able to reproduce, thanks!

davidgohel commented 3 years ago

This should now work (you need to update officedown from github).

Template path is transformed as absolute path now.

casblaauw commented 3 years ago

Great, thanks for the fix!