<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Word | Liming Wang</title>
    <link>https://lmwang.info/tags/word/</link>
      <atom:link href="https://lmwang.info/tags/word/index.xml" rel="self" type="application/rss+xml" />
    <description>Word</description>
    <generator>HugoBlox Kit (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Fri, 14 Aug 2020 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://lmwang.info/media/icon_hu_185555ddd9a5f715.png</url>
      <title>Word</title>
      <link>https://lmwang.info/tags/word/</link>
    </image>
    
    <item>
      <title>Creating Word Documents with R Markdown</title>
      <link>https://lmwang.info/blog/creating-word-doc/</link>
      <pubDate>Fri, 14 Aug 2020 00:00:00 +0000</pubDate>
      <guid>https://lmwang.info/blog/creating-word-doc/</guid>
      <description>&lt;h1 id=&#34;references&#34;&gt;References&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;
 and 
&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ul&gt;
</description>
    </item>
    
    <item>
      <title>Creating Tables for Microsoft Word Output with R Markdown</title>
      <link>https://lmwang.info/blog/creating-tables/</link>
      <pubDate>Sun, 22 Oct 2017 00:00:00 +0000</pubDate>
      <guid>https://lmwang.info/blog/creating-tables/</guid>
      <description>&lt;h1 id=&#34;r-markdown&#34;&gt;R Markdown&lt;/h1&gt;
&lt;p&gt;I have switched to solely use R Markdown for the &lt;code&gt;communication&lt;/code&gt; step of my 
 and found it pleasant and actually prodocutive to use. However, there is one major hassle: output in Word format. In a perfect world, I would never need to use any Microsoft products ever again. In reality when I need to work with collaborators that do not use RMarkdown and funders of research projects that require reports in Word format (and a required style template), there are no other options. As a matter of fact, it seems that the only viable solution collaborating with anybody that doesn&amp;rsquo;t use RMarkdown and/or &lt;code&gt;git&lt;/code&gt; is still MS Word as of 2017/10.&lt;/p&gt;
&lt;p&gt;Rendering R Markdown file to word_document works for the most part, except for sophisticated tables, which, unfortunately, are prevalent in most of my work.&lt;/p&gt;
&lt;h1 id=&#34;possible-solutions&#34;&gt;Possible Solutions&lt;/h1&gt;
&lt;p&gt;There are a few possible solutions, none of them good enough for me at this point:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Gmisc::docx_document (the output is a html file with an additional &lt;code&gt;css&lt;/code&gt; file; no support for Word style template);&lt;/li&gt;
&lt;li&gt;Convert html output to Word document through &lt;code&gt;soffice&lt;/code&gt; (Tables in the output do not resemble the html input; some support for template);&lt;/li&gt;
&lt;li&gt;Pure R solution &lt;code&gt;officer&lt;/code&gt; + &lt;code&gt;flextable&lt;/code&gt; (They can generate good looking Word output, but they do not work with R Markdown yet (see below)).&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;my-solutionhack-as-of-201710&#34;&gt;My solution/hack (as of 2017/10)&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Render Rmd file with output set to rmarkdown::word_document;&lt;/li&gt;
&lt;li&gt;The Rmd file uses texreg/htmlreg to generate html tables for regression results and saves them into separate html files (one for each table) during rendering;&lt;/li&gt;
&lt;li&gt;Convert html to docx:
a. Via Word COM through py32win module (the used option)
b. Via 

c. Via soffice: &lt;code&gt;soffice --headless --convert-to docx:&amp;quot;MS Word 2007 XML&amp;quot; --writer --outdir . Table\ 1.html&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Patch the rendered word_document and replace placeholder tables with actual tables&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;steps-and-files&#34;&gt;Steps and Files:&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;rmarkdown::render(&amp;quot;reg_tables_demo.Rmd&amp;quot;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;python html2docx.py tabs/&lt;/code&gt; (requiring py32win and Microsoft Word on a Windows PC, tested with MS Office 2016 and python 2.7)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;source(&amp;quot;patch_docx_tables.R&amp;quot;); patchFlexTables(&amp;quot;reg_tables_demo.docx&amp;quot;, &amp;quot;tabs/&amp;quot;, &amp;quot;reg_tables_demo_final.docx&amp;quot;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Final 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All files/scripts used can be founded on github: 
&lt;/p&gt;
&lt;h1 id=&#34;alternatives--things-to-keep-an-eye-on&#34;&gt;Alternatives / Things to keep an eye on&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;broom&lt;/code&gt; &amp;gt; &lt;code&gt;huxtable&lt;/code&gt; &amp;gt; &lt;code&gt;flextable&lt;/code&gt; &amp;gt; &lt;code&gt;pandoc (2.0)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This solution directly &lt;code&gt;knitr::knit&lt;/code&gt; Rmd files into Word documents embedding flextables through xml chunks. It eliminates the need to run scripts outside &lt;code&gt;knit&lt;/code&gt; and is a much more elegant solution than my solution above. However it is not yet an acceptable solution at the moment for a number of reasons:&lt;/p&gt;
&lt;p&gt;i. &lt;code&gt;pandoc&lt;/code&gt; 2.0 is not yet released. Users will need to build their own &lt;code&gt;pandoc&lt;/code&gt; installation from source.
i. The &lt;code&gt;flextable&lt;/code&gt; implementation, standing as 
, is yet to be incorporated in &lt;code&gt;flextable&lt;/code&gt;&amp;rsquo;s code base, although Maxim&amp;rsquo;s code should work already.
i. &lt;code&gt;broom&lt;/code&gt; supports 
 than 
, although &lt;code&gt;broom&lt;/code&gt; + &lt;code&gt;huxtable&lt;/code&gt; seem to be a better solution in the long run given they follow the tidy philosophy and &lt;code&gt;broom&lt;/code&gt; is now part of &lt;code&gt;tidyverse&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;pandoc&lt;/code&gt;, when it improves its support for sophisticated tables in unknown future&lt;/p&gt;
&lt;p&gt;The table support currently in &lt;code&gt;pandoc&lt;/code&gt; is rudimentary, in particular, 
, which is what sent me down this rabbit hole. Once the issue is resolved, it&amp;rsquo;d be as easy and functional to produce Word outputs as html and pdf ones. Not sure when this would be resolved (if ever), as the github issue has been open for 4 years now.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;credits&#34;&gt;Credits&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
 for coming out with the idea and sharing his code for the Word patching hack;&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ol&gt;
</description>
    </item>
    
  </channel>
</rss>
