Site icon R-bloggers

Tables from R into Word

[This article was first published on G-Forge » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< g:plusone size="medium" href="http://gforge.se/2013/02/tables-from-r-into-word/">
A good looking table matters!

This tutorial is on how to create a neat table in Word by combining knitr and R Markdown. I’ll be using my own function, htmlTable, from the Gmisc package.

Background: Because most journals that I submit to want the documents in Word and not LaTeX, converting my output into Word is essential. I used to rely on converting LaTeX into Word but this was tricky, full of bugs and still needed tweaking at the end. With R Markdown and LibreOffice it’s actually rather smooth sailing, although I must admit that I’m disappointed at how bad Word handles html.

The tutorial

We start with loading the package, and labeling the dataset. The labels and the units are from the Hmisc package:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
library(Gmisc, verbose=FALSE)
 
data(mtcars)
 
label(mtcars$mpg) <- "Gas"
units(mtcars$mpg) <- "Miles/gal"
 
label(mtcars$wt) <- "Weight"
units(mtcars$wt) <- "10<sup>3</sup> lb"
 
mtcars$am <- factor(mtcars$am, 
                    levels=0:1, 
                    labels=c("Automatic", "Manual"))
label(mtcars$am) <- "Transmission"
 
mtcars$gear <- factor(mtcars$gear)
label(mtcars$gear) <- "Gears"
 
# Make up some data for making it slightly more interesting
mtcars$col <- factor(sample(c("red", "black", "silver"), 
                            size=NROW(mtcars), 
                            replace=TRUE))
label(mtcars$col) <- "Car color"

Now we calculate the statistics. The getDescriptionsStatsBy() is a more interesting alternative to just running table(). It can also run simple statistics that often are reported in table 1.

?View Code RSPLUS
1
2
3
4
5
6
7
mpg_data <- getDescriptionStatsBy(mtcars$mpg, mtcars$am, html=TRUE)
rownames(mpg_data) <- units(mtcars$mpg)
wt_data <- getDescriptionStatsBy(mtcars$wt, mtcars$am, html=TRUE)
rownames(wt_data) <- units(mtcars$wt)
 
gear_data <- getDescriptionStatsBy(mtcars$gear, mtcars$am, html=TRUE)
col_data <- getDescriptionStatsBy(mtcars$col, mtcars$am, html=TRUE)

Next we create the actual table with htmlTable. We can also have an internal reference to the table using the <a href=“#Table1” >, click here. The latex() function that I’ve used as a template for the parameters (to be able to quickly switch between the two) can feel a little overwhelming:

?View Code RSPLUS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
htmlTable(
  x        = rbind(gear_data, col_data, mpg_data, wt_data),
  caption  = paste("My table 1. All continuous values are reported with",
                   "mean and standard deviation, x̄ (± SD), while categories",
                   "are reported in percentages, no (%)."),
  label    = "Table1",
  rowlabel = "Variables",
  rgroup   = c(label(gear_data),
               label(col_data),
               label(mpg_data),
               label(wt_data)),
  n.rgroup = c(NROW(gear_data),
               NROW(col_data),
               NROW(mpg_data),
               NROW(wt_data)),
  ctable   = TRUE)

Below is the table. Note: the table is formatted by this blog CSL, it will look different after running the Rmd document through knitr.

My table 1. All continuous values are reported with mean and standard deviation, x̄ (± SD), while categories are reported in percentages, no (%).
Variables Automatic Manual
Gears
  3 15 (78.9 %) 0 (0.0 %)
  4 4 (21.1 %) 8 (61.5 %)
  5 0 (0.0 %) 5 (38.5 %)
Car color
  black 6 (31.6 %) 4 (30.8 %)
  red 7 (36.8 %) 3 (23.1 %)
  silver 6 (31.6 %) 6 (46.2 %)
Gas
  Miles/gal 17.1 (± 3.8) 24.4 (± 6.2)
Weight
  103 lb 3.8 (± 0.8) 2.4 (± 0.6)

Now install and open in LibreOffice Writer the html document that knitr has created:

The table looks actually a little funny in Writer but don’t worry, it’ll be great!

Now select the table, copy and paste into word, voila!

Now this looks better!

To leave a comment for the author, please follow the link and comment on their blog: G-Forge » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.