R Markdown: How to format tables and figures in .docx files
2016-08-26 09:45
387 查看
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
Figure sizes can be determined in the chunk options, e.g.
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. With
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.
The second macro called FormatFigures merely reduces the size ofall figures in the active MS Word document to 45% of its original size.
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
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. With
wdTableFormatGrid2, 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 Next End 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 InlineShape For Each shp In ActiveDocument.InlineShapes shp.ScaleHeight = 45 shp.ScaleWidth = 45 Next End 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
相关文章推荐
- How to View MySQL Temp Files and Temp Tables in Linux
- How to read and write JSON files in Java(Gson)
- How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files
- How to delete subfolders and files in a folder
- How to index email and attachments in nsf files?
- How to create simple and advanced pivot tables in C# and ASP.NET
- 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
- FFmpeg Understanding(4. How to compile and run testing files utilizing FFMPEG in VC?)
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- (极力推荐)BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files
- How to Output a List of Files to a File and Sort Them in Linux
- BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- Netezza External Tables --How to use local files in external table
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs (文档 ID 290614.1)
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- How to create and extract zip, tar, tar.gz and tar.bz2 files in Linux
- How To Get Log, Trace Files In OA Framework Pages And Concurrent Request Programs
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML