只是想用我决定做的事情来更新此内容。我现在正在使用该hwriter
软件包打印出表格,并使用row.*
和col.*
功能将CSS类放到不同的元素上。然后,我编写了自定义CSS来按需显示。因此,这是一个示例,以防其他人正在处理类似的问题。
首先,创建一个文件,将knitting
Markdown更改为HTML:
FILE: file_knit.r
#!/usr/bin/env Rscript
library(knitr)
library(markdown)
knit("file.Rmd")
markdownToHTML("file.md","file.html",stylesheet="~/custom.css")
接下来,创建实际的Markdown文件:
FILE: file.Rmd
Report of Fruit vs. Animal Choices
==================================
This is a report of fruit vs. animal choices.
```{r echo=FALSE,results='asis'}
library(hwriter)
set.seed(9850104)
my.df <- data.frame(Var1=sample(x=c("Apple","Orange","Banana"),size=40,replace=TRUE),
Var2=sample(x=c("Dog","Cat","Bunny"),size=40,replace=TRUE))
tbl1 <- table(my.df$Var1,my.df$Var2)
tbl1 <- cbind(tbl1,rowSums(tbl1))
tbl1 <- rbind(tbl1,colSums(tbl1))
colnames(tbl1)[4] <- "TOTAL"
rownames(tbl1)[4] <- "TOTAL"
# Because I used results='asis' for this chunk, I can just use cat() and hwrite() to
# write out the table in HTML. Using hwrite()'s row.* function, I can assign classes
# to the various table elements.
cat(hwrite(tbl1,
border=NA,
table.class="t1",
row.class=list(c("header col_first","header col","header col","header col", "header col_last"),
c("col_first","col","col","col","col_last"),
c("col_first","col","col","col","col_last"),
c("col_first","col","col","col","col_last"),
c("footer col_first","footer col","footer col","footer col","footer col_last"))))
```
最后,只需创建一个自定义CSS文件。
FILE: custom.css
body {
font-family: sans-serif;
background-color: white;
font-size: 12px;
margin: 20px;
}
h1 {font-size:1.5em;}
table {
border: solid;
border-color: black;
border-width: 2px;
border-collapse: collapse;
margin-bottom: 20px;
text-align: center;
padding: 0px;
}
.t1 .header {
color: white;
background-color: black;
border-bottom: solid;
border-color: black;
border-width: 2px;
font-weight: bold;
}
.t1 .footer {
border-top: solid;
border-color: black;
border-width: 2px;
}
.t1 .col_first {
border-right: solid;
border-color: black;
border-width: 2px;
text-align: left;
font-weight: bold;
width: 75px;
}
.t1 .col {
width: 50px;
}
.t1 .col_last {
width: 50px;
border-left: solid;
border-color: black;
border-width: 2px;
}
执行./file_knit.r
给我file.html,看起来像这样:
因此,希望这对希望在Markdown输出中提供更多格式的其他人可能有所帮助!
print(xtable(data), type = "html")
。