2
votes

I've started using expss in R Markdown for generating tables with the help of Knitr. I would like to automate the tables and analysis for a report I need to prepare in Microsoft Word format.

When knitting to HTML, the tables look wonderful. The tables in Word are displayed as rows of plain text and does not resemble a table. Does expss support exports of tables to Word? Are there instructions on how to do it?

Tables generated with kable and dplyr display correctly in Word. However, I'm struggling to reproduce the HTML tables made with expss.

library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

cro(mtcars$am, mtcars$vs)

I'm hoping that my Word tables would look like the HTML table examples that could be found at this link or in this Image of HTML table example

enter image description here

I would also be happy if they look like the the tables in my R Console output

enter image description here

The table output in Word looks like this:

Engine

V-engine

Straight engine

Transmission

Automatic

12

7

Manual

6

7

#Total cases

18

14

1

1 Answers

1
votes

expss uses htmlTable package for table rendering. Unfortunately, htmlTable doesn't support word output. However, you can use split_table_to_df and kable functions. They give you table-like output in the Microsoft Word. See example:

library(expss)
library(knitr)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      vs = "Engine",
                      vs = c("V-engine" = 0,
                             "Straight engine" = 1),
                      am = "Transmission",
                      am = c("Automatic" = 0,
                             "Manual"=1),
                      gear = "Number of forward gears",
                      carb = "Number of carburetors"
)

cro(mtcars$am, mtcars$vs) %>% 
    split_table_to_df() %>% 
    kable()