<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/" data-count="horizontal" data-text="R Markdown: How to format tables and figures in .docx files" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In research, we usually publish the most important findings in tables and figures. When writing research papers using Rmarkdown (*.Rmd), we have several options to format the output of the final MS Word document (.docx).</p>
<p>Tables can be formated using either the <strong><em>knitr</em></strong> package’s <code>kable()</code> function or several functions of the <strong><em>pander</em></strong> package.</p>
<p>Figure sizes can be determined in the <a target="_blank" rel="nofollow" href="http://yihui.name/knitr/options/#chunk_options">chunk options</a>, e.g. <code>{r name_of_chunk, fig.height=8, fig.width=12}</code>.</p>
<p>However, options for customizing tables and figures are rather limited in Rmarkdown. Thus, I usually customize tables and figures in the final MS Word document. </p>
<p>In this blog post, I show how to quickly format tables and figures in the final MS Word document using a <em><a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Macro_(computer_science">macro</a></em>). MS Word macros are written in VBA (Visual Basic for Applications) and can be accessed from a menu list or from the tool bar and run by simply clicking. There are loads of tutorials explaining how to write a macro for MS Word, e.g Usman Javaid’s <a target="_blank" rel="nofollow" href="http://www.addictivetips.com/microsoft-office/create-macros-in-word-2010/">Create Macros In Word 2010</a>.</p>
<p>The following two macros are very helpful to format drafts. Since I want drafts to be as compact as possible, tables and figures should not to be too space consuming. </p>
<p>The first macro called <em>FormatTables</em> customizes the format of <strong>all</strong> tables of the active MS Word document. With <code>wdTableFormatGrid2</code>, we use a table style predefined in MS Word. A list of other table styles can be found under the following <a target="_blank" rel="nofollow" href="http://is.gd/Jk2FH9">link</a>. Furthermore, we define font name (<em>Arial</em>) and font size (<em>8 pt</em>), space before (<em>6 pt</em>) and after (<em>10 pt</em>) the table. Finally, the row height is set to <em>18 pt</em> exactly.</p>
<pre>
Sub FormatTables()

 Dim tbl As Table
    For Each tbl In ActiveDocument.Tables
         tbl.AutoFormat wdTableFormatGrid2
         tbl.Range.Font.Name = &quot;Arial&quot;
         tbl.Range.Font.Size = 8
         tbl.Range.ParagraphFormat.SpaceBefore = 6
         tbl.Range.ParagraphFormat.SpaceAfter = 10
         tbl.Range.Cells.SetHeight RowHeight:=18, HeightRule:=wdRowHeightExactly

    Next

End Sub
</pre>
<p>The second macro called <em>FormatFigures</em> merely reduces the size of <strong>all</strong> figures in the active MS Word document to <em>45%</em> of its original size.</p>
<pre>
Sub FormatFigures()

Dim shp As InlineShape


For Each shp In ActiveDocument.InlineShapes
    shp.ScaleHeight = 45
    shp.ScaleWidth = 45
Next

End Sub
</pre>
<p>I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog <a target="_blank" rel="nofollow" href="https://scriptsandstatistics.wordpress.com/">Scripts and Statistics</a> for more R tutorials.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toR Markdown: How to insert page breaks in a MS Word document" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/">R Markdown: How to insert page breaks in a MS Word document</a></li>
<li><a title="Permanent Link toWorking on Data-Warehouse (SQL) with R" target="_blank" rel="bookmark" href="http://datascienceplus.com/working-on-data-warehouse-sql-with-r/">Working on Data-Warehouse (SQL) with R</a></li>
<li><a title="Permanent Link toImplementing Apriori Algorithm in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/implementing-apriori-algorithm-in-r/">Implementing Apriori Algorithm in R</a></li>
<li><a title="Permanent Link toHandling missing data with MICE package; a simple approach" target="_blank" rel="bookmark" href="http://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/">Handling missing data with MICE package; a simple approach</a></li>
<li><a title="Permanent Link toBest packages for data manipulation in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/best-packages-for-data-manipulation-in-r/">Best packages for data manipulation in R</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=_AIHjQFG_m0:8WxmlhqZhrU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=_AIHjQFG_m0:8WxmlhqZhrU:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=_AIHjQFG_m0:8WxmlhqZhrU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=_AIHjQFG_m0:8WxmlhqZhrU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/" data-count="vertical" data-text="R Markdown: How to format tables and figures in .docx files" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/"> DataScience+</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/QetHDPMLKRA"></a></div>