R Markdown: How to format tables and figures in .docx files
来源:互联网 发布:淘宝怎么删除中评 编辑:程序博客网 时间:2024/05/17 01:01
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).
Tables can be formated using either the knitr package’s kable()
function or several functions of the pander package.
Figure sizes can be determined in the chunk options, e.g. {r name_of_chunk, fig.height=8, fig.width=12}
.
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.
In this blog post, I show how to quickly format tables and figures in the final MS Word document using a macro). 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 Create Macros In Word 2010.
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.
The first macro called FormatTables customizes the format of alltables of the active MS Word document. WithwdTableFormatGrid2
, we use a table style predefined in MS Word. A list of other table styles can be found under the following link. Furthermore, we define font name (Arial) and font size (8 pt), space before (6 pt) and after (10 pt) the table. Finally, the row height is set to 18 pt exactly.
Sub FormatTables() Dim tbl As Table For Each tbl In ActiveDocument.Tables tbl.AutoFormat wdTableFormatGrid2 tbl.Range.Font.Name = "Arial" tbl.Range.Font.Size = 8 tbl.Range.ParagraphFormat.SpaceBefore = 6 tbl.Range.ParagraphFormat.SpaceAfter = 10 tbl.Range.Cells.SetHeight RowHeight:=18, HeightRule:=wdRowHeightExactly NextEnd Sub
The second macro called FormatFigures merely reduces the size ofall figures in the active MS Word document to 45% of its original size.
Sub FormatFigures()Dim shp As InlineShapeFor Each shp In ActiveDocument.InlineShapes shp.ScaleHeight = 45 shp.ScaleWidth = 45NextEnd Sub
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 blogScripts and Statistics for more R tutorials.
Related Post
- R Markdown: How to insert page breaks in a MS Word document
- Working on Data-Warehouse (SQL) with R
- Implementing Apriori Algorithm in R
- Handling missing data with MICE package; a simple approach
- Best packages for data manipulation in R
- R Markdown: How to format tables and figures in .docx files
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- How to read and write JSON files in Java(Gson)
- How to create simple and advanced pivot tables in C# and ASP.NET
- How to define hash tables in bash?
- How to Output a List of Files to a File and Sort Them in Linux
- how to use deb files in ubuntu
- How to access files in a jar?
- How To View .chm Files In Ubuntu
- How to open PDF files in sharepoint
- How to: load DDS files in OpenGL.
- How to Upload Large Files in PHP
- How to play .rmvb files in Ubuntu
- FFmpeg Understanding(4. How to compile and run testing files utilizing FFMPEG in VC?)
- How do I write to files using cheerio and request in node?
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- How to format DateTime in PowerShell window
- POI操作PPT之BUG处理-----不定期更新
- 优酷蓝鲸近千节点的Redis集群运维经验总结
- web前端总结
- VM虚拟机增加磁盘空间的扩容操作(little by little)
- 【转载】linux MySQL数据库入门教程
- R Markdown: How to format tables and figures in .docx files
- 由于本机的限制,该操作已被取消
- 五年Android开发,让我“刻骨铭心”的那些坑
- HDU 5782 Cycle(后缀数组+bitset)
- 计算两个一维数组的卷积
- C语言冒泡排序
- iOS内购坑坑坑
- 转-如何给变量取个简短且无歧义的名字
- document.body.scrollTop与document.documentElement.scrollTop区别