[英]How can I turn an R data frame into a simple, unstyled html table?
Let's say I have a data frame in R . 假设我在R中有一个数据框。 I'd like to write it to a file as a simple HTML table. 我想将它作为简单的HTML表写入文件。 Just the <table>, <tr>, and <td> tags. 只是<table>,<tr>和<td>标签。
So far this seems harder than it should be. 到目前为止,这似乎比它应该更难。 Right now I'm trying to use R2THML like so: 现在我正试图像这样使用R2THML:
HTML(dataframe, file=outpath, append=FALSE)
But then I get a ugly, html-styled file that might look like so: 但后来我得到了一个丑陋的html风格的文件,可能看起来像这样:
<table cellspacing=0 border=1>
<caption align=bottom class=captiondataframe></caption>
<tr><td>
<table border=0 class=dataframe>
<tbody>
<tr class= firstline >
<th> </th>
<th>name </th>
<th>donations </th>
<th>clicks </th>
...
</tr>
<tr>
<td class=firstcolumn>1
</td>
<td class=cellinside>Black.text
</td>
...
</tbody>
</table>
</td></table>
<br>
Is there a way to get output that's simpler (without specifying border, headings, captions, etc. Without outputting a table inside another table)? 有没有办法让输出更简单(没有指定边框,标题,标题等,而不在另一个表中输出表格)? Or is this as good as it gets? 或者这是不是很好?
The xtable
package can generate HTML output as well as LaTeX output. xtable
包可以生成HTML输出以及LaTeX输出。
# install.packages("xtable")
library("xtable")
sample_table <- mtcars[1:3,1:3]
print(xtable(sample_table), type="html", file="example.html")
gives, in the file example.html
: 在文件example.html
:
<!-- html table generated in R 3.0.1 by xtable 1.7-1 package -->
<!-- Fri Jul 19 09:08:15 2013 -->
<TABLE border=1>
<TR> <TH> </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH> </TR>
<TR> <TD align="right"> Mazda RX4 </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
<TR> <TD align="right"> Mazda RX4 Wag </TD> <TD align="right"> 21.00 </TD> <TD align="right"> 6.00 </TD> <TD align="right"> 160.00 </TD> </TR>
<TR> <TD align="right"> Datsun 710 </TD> <TD align="right"> 22.80 </TD> <TD align="right"> 4.00 </TD> <TD align="right"> 108.00 </TD> </TR>
</TABLE>
This could be further simplified with more options to xtable
and print.xtable
: 这可以通过xtable
和print.xtable
更多选项进一步简化:
print(xtable(sample_table, align="llll"),
type="html", html.table.attributes="")
gives 给
<!-- html table generated in R 3.0.1 by xtable 1.7-1 package -->
<!-- Fri Jul 19 09:13:33 2013 -->
<TABLE >
<TR> <TH> </TH> <TH> mpg </TH> <TH> cyl </TH> <TH> disp </TH> </TR>
<TR> <TD> Mazda RX4 </TD> <TD> 21.00 </TD> <TD> 6.00 </TD> <TD> 160.00 </TD> </TR>
<TR> <TD> Mazda RX4 Wag </TD> <TD> 21.00 </TD> <TD> 6.00 </TD> <TD> 160.00 </TD> </TR>
<TR> <TD> Datsun 710 </TD> <TD> 22.80 </TD> <TD> 4.00 </TD> <TD> 108.00 </TD> </TR>
</TABLE>
(which could be directed to a file with the file
argument to print.xtable
as in the previous example.) (可以将其定向到带有print.xtable
file
参数的file
,如上例所示。)
The answer is actually quite simple, if you use xtable. 如果使用xtable,答案实际上非常简单。 (Thanks to Señor O for the tip.) (感谢SeñorO的提示。)
install.packages("xtable")
library(xtable)
out_table_x <- xtable(out_table)
print(out_table_x, type='html', file="./example.html")
A prettier but slower option: 一个更漂亮但更慢的选择:
library(htmlTable)
htmlTable(iris)
You could also have a look at the tableHTML
package, that was developed for this reason. 您还可以查看为此原因开发的tableHTML
包。
library(tableHTML)
mtcars %>%
tableHTML()
And to print the HTML on the console: 并在控制台上打印HTML:
tableHTML(mtcars[1:2, 1:3]) %>%
print(viewer = FALSE)
# <table style="border-collapse:collapse;" class=table_9302 border=1>
# <thead>
# <tr>
# <th id="tableHTML_header_1"> </th>
# <th id="tableHTML_header_2">mpg</th>
# <th id="tableHTML_header_3">cyl</th>
# <th id="tableHTML_header_4">disp</th>
# </tr>
# </thead>
# <tbody>
# <tr>
# <td id="tableHTML_rownames">Mazda RX4</td>
# <td id="tableHTML_column_1">21</td>
# <td id="tableHTML_column_2">6</td>
# <td id="tableHTML_column_3">160</td>
# </tr>
# <tr>
# <td id="tableHTML_rownames">Mazda RX4 Wag</td>
# <td id="tableHTML_column_1">21</td>
# <td id="tableHTML_column_2">6</td>
# <td id="tableHTML_column_3">160</td>
# </tr>
# </tbody>
# </table>
The table can also be styled with CSS using the add_css_
family of functions, if needed. 如果需要,还可以使用add_css_
系列函数使用CSS对表进行样式设置。
Details of the package and tutorials (vignettes) are here 包和教程(小插曲)的详细信息在这里
to_html_table<-function(dataframe){
tags$table(
tags$thead(tags$tr(lapply(colnames(dataframe), function(x) tags$th(x)))),
tags$tbody(
apply(dataframe,1, function(x) { tags$tr(lapply(x, function(y) tags$td(y)))})
))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.