Making HTML Tables in R

TL;DR: htmlTable and tableHTML are better than xtable for creating HTML tables in R.

Today, I had to make tables for a Word document, meaning I could not convert my R table to Latex code.  (I know I know, pandoc.  Just another crusty old thing to learn.)  In the past, I had used xtable, but it is really designed to write tables in Latex code, so it’s html functionality is limited.  I therefore went in search of a better solution.

The internet pointed me to two: htmlTable and tableHTML.  Yes, you read that right.  Those are two separate packages with almost identical names.  So identical, in fact, that it wasn’t until struggling through code in tableHMTLs’s vignette that did not work for me that I realized they were two separate packages (I was working with htmlTable).

I first messed with htmlTable because its vignette was easy to follow.  Early results were encouraging.  Just when I was ready to move onto my second table, I noticed that the columns were too narrow.  Wanting to increase the horizontal spacing between columns, I went digging into the instruction manual.  I could not find an easy solution.  Perhaps there is a way to do it with CSS, but that would be another technique to learn.  And being time crunched and not interested in websites, I decided, after a solid 30 minutes of thinking I found my solution, htmlTable was not the package for me.

Fortunately, tableHTML does allow you to change column widths.  In fact, it was seeing that in its vignette and failing to get the code to work that caused me to initially realize that there are two competing R packages.  So, I ended up using tableHTML and am happy with the results.  Its “scientific” theme is especially useful, as that theme’s default formatting is very clean.

It is possible, however, that htmlTable is the better package, for two formatting reasons.  First, I could not find a way to change the alignment of each column in tableHTML, which is possible with htmlTable.  Second, I could not insert a newline in the table’s footnote.  htmlTable recognizes ‘\n’ as newline, but tableHTML just prints ‘\n’ in the footer.  On the other hand, I can manually edit these features once the HTML output is pasted into Word.  Because of the ability to manually edit, I will stick with with tableHTML.  It is also possible I could have  manually edited the column width of the htmlTable output in Word, but my 10 seconds of trying failed; in retrospect, I think I needed to increase my page margins first.  Oh well!

To summarize: both are good.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.