<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/wordr-a-new-r-package-for-rendering-documents-in-ms-word-format/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/10/04/wordr---a-new-r-package-for-rendering-documents-in-ms-word-format/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h2>Motivation</h2>
<p>One day earlier this year, I was faced with the challenge of creating a report for management. It had to be an MS Word document (corporate requirement, you know). It was supposed to be polished and use many of the standard MS Word features like headers, footers, table of contents, and styles.</p>
<p>I am not a Word guy, and besides, I wanted to make a reproducible document that would make it easy for me to include R code and plots in the text. Having no idea what tools were available, I revved up Google and found several packages including <a rel="nofollow" href="https://cran.r-project.org/package=R2wd" target="_blank">R2wd</a>, <a rel="nofollow" href="https://cran.r-project.org/package=officer" target="_blank">officer</a> and <a rel="nofollow" href="https://cran.r-project.org/package=ReporteRs" target="_blank">ReporteRs</a>. They are all very good packages, but for different reasons, none of them covered a hundred percent of my needs. Also, I thought that they make some things unnecessarily complicated (again, for my needs).</p>
<p>For example, I wanted to be able to create the document template that included a significant amount of text and styles. I also wanted to be able to mark a place to add a table or plot and mainly, I wanted to have something like R inline code in R markdown language (<a rel="nofollow" href="http://rmarkdown.rstudio.com/lesson-4.html" target="_blank">link</a>).</p>
<p>I found out that with combination of <a rel="nofollow" href="https://cran.r-project.org/package=officer" target="_blank">officer</a> and <a rel="nofollow" href="https://cran.r-project.org/package=ReporteRs" target="_blank">ReporteRs</a> packages and some effort, I could achieve what I needed. (It could have been done solely with <a rel="nofollow" href="https://cran.r-project.org/package=officer" target="_blank">officer</a>, but the tables and plots creation/insertion capability in <a rel="nofollow" href="https://cran.r-project.org/package=ReporteRs" target="_blank">ReporteRs</a> serves me better).</p>
<p>Package <a rel="nofollow" href="https://cran.r-project.org/package=WordR" target="_blank">WordR</a>, in version 0.2.2 on <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a> now, is the result.</p>
</div>
<div>
<h2>How to use it</h2>
<p>This is, I hope, pretty simple. Details are described in the package <a rel="nofollow" href="https://cran.r-project.org/package=WordR/vignettes/WordR.html" target="_blank">vignette</a>. The text below should provide a basic understanding.</p>
<div>
<h3>MS Word part</h3>
<p>As a first step, prepare a Word document with static text, images, headers, footers, styles, etc. In your text, if you need, you can add an R inline code part. It needs to be separated from the rest of the text by style separators at the beginning and the end (insert style separator by pressing <code>Ctrl+Alt+Enter</code> (it is preferable to have formatting symbols visible by clicking the “new line” sign button on <code>Home>Paragraph</code> panel in MS Word)), and it should have a form of <code>`r expression`</code>, where <code>expression</code> is an R expression like <code>1+1</code>. So the result should look something like this, in the MS Word editor:</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-10-02-Hovorka-WordR_files/lineWithCode.gif?w=456&ssl=1" data-recalc-dims="1">
</div>
<p></p>
<p>At a place where you want to render a table, insert a bookmark named <code>t_XYZ</code>, where XYZ will be the name of a FlexTable (<code>ReporteRs::FlexTable</code>) table which you want to insert.</p>
<p>At a place where you want to render a plot/image, insert a bookmark named <code>p_XYZ</code>, where XYZ will be the name of plot function (<code>ReporteRs::addPlot.docx</code>) you want to be used to render the plot/image.</p>
<p>Save the prepared Word template.</p>
<div>
<h4>Example</h4>
<p>The resulting document may look like this (print screen; original document is <a rel="nofollow" href="https://rviews.rstudio.com/2017/10/04/wordr---a-new-r-package-for-rendering-documents-in-ms-word-format/resources/templateMTcars.docx" target="_blank">here</a>):</p>
<div>
<img src="https://i1.wp.com/rviews.rstudio.com/post/2017-10-02-Hovorka-WordR_files/templateMTcarsPrintscreen.gif?w=456&ssl=1" data-recalc-dims="1">
</div>
<p></p>
<p>If you don’t see the file in the MS Word as in the print screen above, please refer to the package <a rel="nofollow" href="https://cran.r-project.org/package=WordR/vignettes/WordR.html" target="_blank">vignette</a> on how to make the bookmarks and non-printable signs visible.</p>
<p>Please mind a few things in the example:</p>
<ul>
<li>R inline code in the header</li>
<li>two inline code snippets in the text, the second one formatted in a custom style <code>red style</code></li>
<li>two bookmarks, one for plot and one for table</li>
</ul>
</div>
</div>
<div>
<h3>R part</h3>
<p>Now, let’s switch to R. Install the <a rel="nofollow" href="https://cran.r-project.org/package=WordR" target="_blank">WordR</a> package (and all the dependencies; Java is required by the <a rel="nofollow" href="https://cran.r-project.org/package=ReporteRs" target="_blank">ReporteRs</a> package, so make sure you have it working for table and plot rendering):</p>
<pre><code>install.packages(&quot;WordR&quot;)</code></pre>
<p>Then you have three functions from the <a rel="nofollow" href="https://cran.r-project.org/package=WordR" target="_blank">WordR</a> package at your disposal:</p>
<ul>
<li><code>renderInlineCode</code> for rendering the R inline code</li>
<li><code>addFlexTables</code> for inserting FlexTables at the locations defined by the bookmarks (for more about FlexTables and their wide variety of formatting possibilities, look at <code>ReporteRs::FlexTable</code>)</li>
<li><code>addFlexPlots</code> for inserting plots/images at the locations defined by the bookmarks</li>
</ul>
<p>Please refer to the help of each function for details, but in general, you provide the path of your prepared Word file as an argument (and the tables or plots respectively), and also the desired path where you want to have your result file saved. The function returns that path upon successful execution.</p>
<div>
<h4>Back to our example</h4>
<p>Assume we saved our file as <code>templateMTcars.docx</code> in sub-directory <code>/resources</code> located in our working directory:</p>
<pre><code>dir(path = paste0(getwd(), &quot;/resources/&quot;), pattern = &quot;.docx&quot;)</code></pre>
<pre><code>## character(0)</code></pre>
<p>Then the rest of the code is simple:</p>
<pre><code>library(WordR)
library(ReporteRs)
library(ggplot2)
# prepare outputs (Tables and Plots)
ft_mtcars &lt;- vanilla.table(mtcars)
FT &lt;- list(mtcars = ft_mtcars)
Plots &lt;- list(mtcars1 = function() print(ggplot(mtcars, aes(x = wt, y = hp, col = as.factor(cyl))) + geom_point()))

# render docx
renderInlineCode(&quot;resources/templateMTcars.docx&quot;,&quot;resources/templateMTcarsOutput.docx&quot;)
addFlexTables(&quot;resources/templateMTcarsOutput.docx&quot;,&quot;resources/templateMTcarsOutput.docx&quot;, FT)
addPlots(&quot;resources/templateMTcarsOutput.docx&quot;,&quot;resources/templateMTcarsOutput.docx&quot;, Plots, height = 4)</code></pre>
<p>This should result in the rendered file <code>resources/templateMTcarsOutput.docx</code> looking like this (printscreen with non-printable characters and bookmarks off; in docx format <a rel="nofollow" href="https://rviews.rstudio.com/2017/10/04/wordr---a-new-r-package-for-rendering-documents-in-ms-word-format/resources/templateMTcarsOutput.docx" target="_blank">here</a>):</p>
<p> <img src="https://i2.wp.com/rviews.rstudio.com/post/2017-10-02-Hovorka-WordR_files/outputMTcarsPrintscreen.gif?w=456&ssl=1" data-recalc-dims="1"></p>
</div>
</div>
</div>
<div>
<h2>A few comments</h2>
<p>In my workflow, I let people who want the report prepare the Word template (without the bookmarks and R code). This is better both for me – I can do other things – and for them – they can format the report the way they want, without iterating with me. Then I take that file, put the R inline code in place, add the bookmarks, and the template is done.</p>
<p>Additionally, when the result is created, it can be further changed. For example, the person who receives the report from me may add some interpretation based on the current results, which would be difficult with the traditional Rmd/LaTeX -> PDF way of reporting.</p>
</div>
<div>
<h2>Summary</h2>
<p>I believe that the <a rel="nofollow" href="https://cran.r-project.org/package=WordR" target="_blank">WordR</a> package offers something new. I admit that it is just a small modification of much more powerful packages <a rel="nofollow" href="https://cran.r-project.org/package=officer" target="_blank">officer</a> and <a rel="nofollow" href="https://cran.r-project.org/package=ReporteRs" target="_blank">ReporteRs</a>, but it simplified things for me a lot. Obviously it will never be the first choice for reproducible research, but I believe it can be useful for few corporate rats and other people who want to produce fully formatted reports but do not want to go deep into LaTeX.</p>
</div>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rviews.rstudio.com/2017/10/04/wordr---a-new-r-package-for-rendering-documents-in-ms-word-format/"> R Views</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/7qp6ZB_WVos" height="1"></a></div>