如何格式化Gmisc :: htmlTable

时间:2014-05-28 13:16:49

标签: r rstudio r-markdown

下面是一个可以粘贴到rstudio的rmarkdown文档。

我的问题是来自htmlTable的输出是从htmlTable属性的前缀/附加的。

---
title: "SO_question"
author: "AC"
date: "Wednesday, May 28, 2014"
output:
  html_document:
    theme: readable
---

My heading
============
This is a few tables. Notice that `htmlTable` prints `[1]"` before each table and `" attr(,“class”) [1] “htmlTable” “character” [1] “` after each table. How can I avoid this?

``` {r html_table, results='asis', echo=FALSE, message=FALSE}
library("htmlTable")
library("reshape2")
#Chick weight example
names(ChickWeight) <- tolower(names(ChickWeight))
chick_m <- melt(ChickWeight, id=2:4, na.rm=TRUE)

for (i in unique(chick_m$diet)) {
  diet <- subset(chick_m, diet==i)
  table_to_print <- dcast(chick_m, time ~ variable, mean)
  print(htmlTable(table_to_print, rgroup=c(""), n.rgroup=nrow(table_to_print)))
}

```

Bonus question: How to format the last row in each table as bold text (suited for a 'total' row)?

2 个答案:

答案 0 :(得分:2)

不要在print上使用htmTable,而是使用cat正确呈现它。

for (i in unique(chick_m$diet)) {
  diet <- subset(chick_m, diet==i)
  table_to_print <- dcast(chick_m, time ~ variable, mean)
  cat(htmlTable(table_to_print, rgroup=c(""), n.rgroup=nrow(table_to_print)))
}

答案 1 :(得分:1)

print.htmlTable函数对对象执行打印时,会调用htmlTable函数。它应该会自动调用cat,不确定这是否属实,可能会在今天发布。

htmlTable-package 的1.1版本中(该功能与Gmisc包分开)有一个完整的选项:

for (i in unique(chick_m$diet)) {
  diet <- subset(chick_m, diet==i)
  table_to_print <- dcast(chick_m, time ~ variable, mean)
  print(htmlTable(table_to_print, total=TRUE))
}

注意:如果您不使用它,则无需指定rgroup元素。

#author