问题
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