【问题标题】:How to format Gmisc::htmlTable如何格式化 Gmisc::htmlTable
【发布时间】:2014-05-28 13:16:49
【问题描述】:

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

我的问题是 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)?

【问题讨论】:

    标签: r rstudio r-markdown


    【解决方案1】:

    不要在htmTable 上使用print,而是使用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)))
    }
    

    【讨论】:

    • 谢谢!我记得在某处读过关于“打印不是猫”或类似内容的咆哮。这只是为了表明。非常感谢,@MrFlick
    【解决方案2】:

    当从htmlTable 函数对对象执行打印时,会调用print.htmlTable 函数。它应该会自动调用cat,不确定这是否是 14 年 5 月的真实情况,但它今天有效。

    1.1版本的htmlTable-package(功能从Gmisc-package中分离出来)有一个总选项:

    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 元素,则无需指定它。

    #作者

    【讨论】:

    • 是不是CRAN版的htmlTable有问题? install.packages("htmlTable") 告诉我它不适用于 R 版本。 3.2.1
    • 奇怪 - 我不知道,CRAN 页面没有显示任何问题:cran.rstudio.com/web/packages/htmlTable/index.html
    • 我刚才又试了一次,使用的是RStudio的安装包。相同的消息。我也很想用这个包。
    • 好吧,老把戏奏效了:关闭 R 并重新开始。然后安装包。抱歉打扰你。 :(
    猜你喜欢
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2018-09-06
    相关资源
    最近更新 更多