4
votes

I'm sorry for a minimally produced question...... not lots of information because it just fails the first time I'm ever trying with this and I am not very skilled in RMarkdown debugging yet.

I'm trying flextable for the first time (interested in controlling table output in both HTML and Word).

Flextable installed and it runs for HTML, but stops any rendering to Word at all. When I try to knit to Word, before I get a new Word file at all, it stops with an error in the RMarkdown panel, being:


Line [first line of R-chunk]: Error: read_docx only support docx files Execution halted  

Has anyone seen and solved this before? There is just one SO question potentially related to this here , but no answers yet :(


EDIT -- I found something, but that's still a problem :-p
I thought of one more thing that is "custom" ... I already have my own reference_docx .
When I took that away so that the default Word template is used... it runs!!
But I definitely want my styles !! flextable is not the only thing that I can possibly use :-D.
Does anyone know about any restrictions on the reference_docx for flextable?
Or might we guess the restrictions are actually for officer ?
Can they be overcome?

I won't bother dumping the headers because you don't have my file anyways. But certainly it's the newest format of Word as I have Office 365 and can't hardly stop Windows Updates if I want to.
I'll try to unpack the docx myself and check out if there's any differences in the metadata of the docx and the standard one as seen in a Knit using the default.

EDIT/UPDATE 2 -- failed to hack file, and..... maybe doesn't matter So I did go through the zip transformation to make my preferred reference_docx a folder and the output docx made with "default" into a folder. I checked with beyondCompare and perhaps the biggest difference is that there is information for endnotes in my preferred one.
I use Zotero, so I actually uninstalled Zotero from Word, resaved, and no change.
I tried removing all the endnotes from the document and resaved and no change.
I tried editing the raw folder to remove an "endnotes.xml" file, re-zipped it, tried to open in Word, let it repair, resaved . . . no change.
I tried additionally removing an entry in a top-level xml related to "endnotes" and also rezipped and opened and repaired and resaved ..... no change.

So I accepted that maybe I just have to rebuild my preferred reference doc styles, and tried to use the output docx from pandoc "default" to start over. And I did think to just use that as the reference_docx first just to make sure it works . . . . . .
it does not . :(

  • So we can see that there is something about docx files on my box that freetable or officer simply won't accept for the reference docx.

I'll probably have to raise it with them.
In the meantime, I can see if there's an actual doc file used for "default" and maybe then I can still get things going.


I'm running RStudio 1.3.1073 on Windows 10.
The simple code I'm running is:

``` {r}

library(dplyr)
library(flextable)

myDF = mtcars 


myDF[1:10,] %>%
      flextable()


```

running sessionInfo() yields:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

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

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

loaded via a namespace (and not attached):
 [1] rstudioapi_0.11  knitr_1.29       magrittr_1.5     tidyselect_1.1.0 munsell_0.5.0    colorspace_1.4-1 R6_2.4.1         rlang_0.4.7     
 [9] dplyr_1.0.2      tools_4.0.2      grid_4.0.2       gtable_0.3.0     xfun_0.16        tinytex_0.25     htmltools_0.5.0  ellipsis_0.3.1  
[17] yaml_2.2.1       digest_0.6.25    tibble_3.0.3     lifecycle_0.2.0  crayon_1.3.4     purrr_0.3.4      ggplot2_3.3.2    vctrs_0.3.2     
[25] glue_1.4.1       evaluate_0.14    rmarkdown_2.3    compiler_4.0.2   pillar_1.4.6     generics_0.0.2   scales_1.1.1     pkgconfig_2.0.3 
> 

just to confirm ~~~ just to be clear that I have already isolated it to using flextable() - or potentially magrittr, but that seems less related - this is fine:

``` {r }


library(dplyr)
library(flextable)


myDF = datasets::mtcars 


myDF[1:10,] 
  # %>%
  #     flextable()


```  

I have been using RMarkdown for a couple weeks and generating docx previously.
I can still generate docx if I don't use flextable, including this minimal data dump with just selecting 10 rows as above without flextable.
When I incorporate flextable we have a strange problem.

And just to be extra, extra clear , taking out magrittr I still get the same behavior ... HTML is ok, Word gets error as stated:

``` {r }


library(dplyr)
library(flextable)


myDF = datasets::mtcars 


flextable(myDF[1:10,])


  # %>%
  #     flextable()


```

And to answer another question,
yes, I've already tried updating all packages and checking for RStudio updates

2
That's not a complete example: something in your YAML header (or elsewhere in the document?) might be messing you up. And it might be informative to run sessionInfo() after loading those two packages, so we can see if they're out of date...user2554330
Yes... if you read my Edit, I say that I did have the header with my own docx. Also I did run sessionInfo and dumped that - that was in my original posting. And no, nothing is out of date :) I've already tried updating all packages and looking for RStudio updates. If you read my whole question, you can see that I'm finding it is a problem with my own reference_docx .Mike M
actual answer added :)Mike M
could you add sessionInfo(). If not using officer_0.3.13 (the github version or the new cran version), could you update?David Gohel
@DavidGohel hello ~ sure, I'll update and try again. I do see .12 before updatingMike M

2 Answers

3
votes

omg -- it's because the filepath for my reference_docx has spaces (maybe other chars also)

I didn't check for other special characters yet, so may be other characters, but for sure space is enough to break it.

My original file path was (partially redacted with same length items for letters and numbers)

C:\Zzzzz\yyyy\XxxXxxxx\--------1111,1111 aaaaaaa bbbb cccc\ddd e.f. !\_-_ ggggg hhhhh iiii jjjjjjj kkkkk\R source\_outputTemplates\  

I tried making it really short and also simple to see if this by chance is the problem, because I hadn't tried it yet and I keep thinking... we know it's a local issue so it must be something like this.

So I jumped it up to

C:\Zzzzz\yyyy\Qqqqqq\  
  • and it works!

So to debug the type of character that is a problem, I made a folder to play with, and started with spaces...

C:\Zzzzz\yyyy\Qqqqqq\a folder with spaces\  

==> ERR !

Changing to same length but no spaces...

C:\Zzzzz\yyyy\Qqqqqq\aFolderWithNooSpaces\  

==> ok :) !!!

I might check for other special characters, but this is already more than enough debugging, I think ;-)
I'm sure the dev can take it from here. The github says they follow the tag.

0
votes

I just tried your example and changed a thing or two. I will post my code underneath. However, be aware, that the package you meant is not dbplyr but dplyr. The first one is for databases, the latter for extract, transform and load. Here is the code:

library(dplyr)
library(flextable)
library(datasets)

data(iris)

iris[1:10,] %>%
      flextable()

With that code, it perfectly knits to word (.docx) on my machine.