Search code examples
rr-markdownpandocbookdownofficer

R Markdown - knitting to bookdown::word_document2 fails because of officer::ftext used in inline code


Minimum Reproducible example of my R notebook (.Rmd) file which causes the error while knitting to word document:

---
title: "R Notebook"
output: bookdown::word_document2
---

Using inline R code chunks with `officer::ftext` causes an error as such


`r officer::ftext('Example text', officer::fp_text(color = 'red'))`

The error when knitting is as follows:

Error in vapply(x, format_sci_one, character(1L), ..., USE.NAMES = FALSE) : values must be length 1, but FUN(X[[2]]) results in length 8 Calls: <Anonymous> ... paste -> hook -> .inline.hook -> formact_sci -> vapply

This error started occurring only recently. Previously with this code, I was able to output a word document where the inline code chunk would produce text 'Example text' with red font color.

My .SessionInfo() output is:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Switzerland.1252  LC_CTYPE=German_Switzerland.1252    LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C                       
[5] LC_TIME=German_Switzerland.1252    

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

other attached packages:
[1] ROracle_1.3-1.1 DBI_1.1.2      

loaded via a namespace (and not attached):
 [1] compiler_3.5.1  bookdown_0.24   fastmap_1.1.0   htmltools_0.5.2 tools_3.5.1     yaml_2.2.1      rmarkdown_2.11  knitr_1.37      xfun_0.29       digest_0.6.29  
[11] rlang_0.4.12    evaluate_0.14  

I'm using Pandoc 2.1. Please note that I'm using this R setup at work, so I'm not able to update to a newer version of R or RStudio IDE.


Solution

  • The .Rmd file started knitting properly to word after I installed and loaded package officedown version 0.1.0. I initially tried with version 0.2.0, but then if I used non-alphanumeric characters as text input in officer::ftext function (eg. '%', '[') in the word output it appeared as an xml structure instead of formatted text.

    Credit goes to David Gohel, developer of the officer packages who pointed me to the officedown package, see discussion on his github page: https://github.com/davidgohel/officer/issues/402