Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I turn an R data frame into a simple, unstyled html table?

Tags:

html

html-table

r

Let's say I have a data frame in R. I'd like to write it to a file as a simple HTML table. Just the <table>, <tr>, and <td> tags.

So far this seems harder than it should be. Right now I'm trying to use R2THML like so:

HTML(dataframe, file=outpath, append=FALSE)

But then I get a ugly, html-styled file that might look like so:

<table cellspacing=0 border=1>
<caption align=bottom class=captiondataframe></caption>
<tr><td>
    <table border=0 class=dataframe>
    <tbody> 
    <tr class= firstline > 
        <th>&nbsp;  </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?

like image 686
PersianExcursion Avatar asked Jul 19 '13 14:07

PersianExcursion


People also ask

How do I turn a data frame into a table?

Method 1 : Using setDT() method The setDT() method can be used to coerce the dataframe or the lists into data. table, where the conversion is made to the original dataframe. The modification is made by reference to the original data structure.

Is a data frame the same as a table in R?

frame in R is similar to the data table which is used to create tabular data but data table provides a lot more features than the data frame so, generally, all prefer the data. table instead of the data.

How do you turn something into a table in R?

table() Function. as. table() function in R Language is used to convert an object into a table.

Which library package is used to import HTML table in R?

HTML tables are a standard way to display tabular information online. Getting HTML table data into R is fairly straightforward with the readHTMLTable() function of the XML package.


2 Answers

The xtable package can generate HTML output as well as LaTeX output.

# 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:

<!-- 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:

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.)

like image 168
Brian Diggs Avatar answered Oct 20 '22 05:10

Brian Diggs


A prettier but slower option:

library(htmlTable)
htmlTable(iris)
like image 32
qed Avatar answered Oct 20 '22 05:10

qed