Formatting tables in R Markdown to export to MS Word document

半城伤御伤魂 提交于 2020-02-05 13:59:30

问题


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

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

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:


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()


来源:https://stackoverflow.com/questions/55542838/formatting-tables-in-r-markdown-to-export-to-ms-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!