Cómo formatear Gmisc::htmlTable
Frecuentes
Visto 946 equipos
1
Below is an rmarkdown document that can be pasted into rstudio.
My problem is that output from htmlTable is prepended/appended with cruft from the htmlTable attributes.
---
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 Respuestas
2
En lugar de usar print
de su htmTable
, Utilizar cat
to properly render it.
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)))
}
contestado el 28 de mayo de 14 a las 15:05
1
Hay un print.htmlTable
function that is called when a print is performed on an object from the htmlTable
function. It should automatically call the cat
, not sure if this was true May '14 but it works today.
En la versión 1.1 del htmlTable-package (the function was separated from the Gmisc-package) there is a total option:
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))
}
Nota: No es necesario especificar el rgroup
element if you are not using it.
#autor
Respondido 09 Feb 15, 09:02
Is there a problem with the CRAN version of htmlTable? install.packages("htmlTable") tells me it is not available for R ver. 3.2.1 - abogado
Odd - not that I'm aware of and the CRAN page doesn't indicate any issue: cran.rstudio.com/web/packages/htmlTable/index.html - max gordon
I tried again just now, using the install packages of RStudio. Same message. I would really like to use the package, too. - abogado
Well, the old trick worked: close R and start over. Then the package installed. Sorry to bother you. :( - abogado
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas r rstudio r-markdown or haz tu propia pregunta.
Thanks! I remember reading a rant somewhere about "print is not cat" or something to that effect. This just goes to show. Thanks a lot, @MrFlick - Andreas