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. But tables on the web are primarily designed for displaying and consuming data, not for analytical purposes. Peculiar design choices for HTML tables are therefore frequently made which tend to produce useless outputs when run through readHTMLTable(). I found that sometimes these outputs could be saved with a little bit of (tedious) post-processing, but just as often they could not. To make working with HTML tables easier and less time-consuming, I developed htmltab, a package for the R system that tries to alleviate these problems directly in the parsing stage when the structural information is still available. Its main advantages over readHTMLTable() are twofold:

This vignette discusses the application of htmltab for two use cases where the package provides a significant improvement over readHTMLTable().

(I make use of the R packages tidyr and stringr to process table outputs. Neither of the three is required for running htmltab.)

How to read HTML tables with htmltab()

The principal function of htmltab is htmltab(). The behaviour of htmltab() is modeled closely after readHTMLTable(), and many argument names are identical. Any function call requires passing a value to its doc argument. This value may be of three kinds:

  1. a URL or file path for the HTML document where the table lives
  2. a parsed HTML object of the entire page of class HTMLInternalDocument
  3. a table nodeset of class XMLNodeSet

The last of these methods returns a single R table object. For the first two, htmltab() requires users to be specific about the table they would like to have returned. This is done via the which argument. This may be either a numeric value for the table's position in the page, or a character value that describes an XPath statement.

1. Corrections for rowspans and colspans by default

In many HTML tables, spans are used to allow cell values to extend across multiple cells. htmltab() recognizes spans and expands tables automatically. To illustrate this feature, take a look at the HTML table in the Language section of this Wikipedia page about Demography in the UK. The header information spans across three consecutive rows. To get the table into R, we have to pass an identifiying information to the which argument. I use an XPath statement that I wrote while exploring the HTML page with Web Developer Tools. One that works is "//th[text() = 'Ability']/ancestor::table":

library(htmltab)

url <- "http://en.wikipedia.org/wiki/Demography_of_the_United_Kingdom"
ukLang <- htmltab(doc = url, which = "//th[text() = 'Ability']/ancestor::table")
head(ukLang)

The header information has been recast into a format that respects the hierarchical order of the variables and yet only spans a single line in the R table. If you prefer a different seperator between variables, pass it to the headerSep argument. This format was chosen to make further processing of the table easy. For example, using functionality from the tidyr package, the next couple of data cleaning steps may be the following:

library(tidyr)

ukLang <- gather(ukLang, key, value, -Ability)

This statement restructures the variables in a more useful long format. From this we can separate the variables using an appropriate regular expression such as " >> ".

ukLang <- separate(ukLang, key, into = c("region", "language", "statistic"), sep = " >> ")
head(ukLang)

htmltab() also automatically expands row and column spans when they appear in the table's body.

2. More control over cell value conversion

htmltab() offers you more control over what part of the HTML table is used in the R table. You can exert this control via htmltab()'s body, header, bodyFun, headerFun, rm_escape, rm_footnote, rm_superscript, rm_nodata_cols, rm_invisible and rm_whitespace arguments.

body and header arguments

It is not possible for htmltab() to correctly identify header and body elements in all the tables. Although there is a semantically correct way to organize header and body elements in HTML tables, web designers do not necessarily need to adhere to them to produce visually appealing tables. htmltab employs heuristics for identification but they are no guarantee. If you find that the table is not correctly assembled, you can try to give the function more information through its header and body arguments. These arguments are used to pass information about which rows should be used for the contruction of the header and the body. Both accept numeric values for the rows, but a more robust way is to use an XPath that identifies the respective rows. To illustrate, take a look at this Wikipedia page about the New Zealand General Election in 2002. The table uses cells that span the entire column range to classify General and Maori electorates (yellow background). We need to control for this problem explicitly in the assembling stage. I pass the XPath "//tr[./td[not(@colspan = '10')]]" to the body argument to explicitly discard all rows from the body that have a \<td> cell with a colspan attribute of 10:

url <- "http://en.wikipedia.org/wiki/New_Zealand_general_election,_2002"
xp <- "//caption[starts-with(text(), 'Electorate results')]/ancestor::table"
body_xp <- "//tr[./td[not(@colspan = '10')]]"

nz1 <- htmltab(doc = url, which = xp, body = body_xp, encoding = "UTF-8")
head(nz1)

Using table information that intercept body rows

In the previous example, we discarded the two intercepting rows in the body which signified the region of the electorate. You might object that ideally these rows should not be discarded, but used for what they are -- variable/header information! As of version 0.6.0, htmltab can process these sort of table designs more efficiently and prepend the information accurately in a new column variable. Information to the header argument can now be passed in the form of X1 + X2 + X3 + ..., where X1 codifies the main header (i.e. the one that spans the body grid), and X2, X3, ... signify groups of header information that appear in the body. Please note that the in-body information (X2, X3, ...) must not identify row elements (tr) but individual cells (td or th) from which the value of the new variable can be generated (usually from the node value). To illustrate, consider the following snippet:

nz2 <- htmltab(doc = url, which = xp, header = 1 + "//tr/td[@colspan = '10']",
               body = "//tr[./td[not(@colspan = '10')]]", encoding = "UTF-8")

Here, we pass '1' to signify that the main header information appear in the first row. We add to this the XPath "//td[@colspan = '10']" that refer to the two rows. Generally, you are free to use numeric information or XPath to refer to the values that are takes as header variable. htmltab extracts these information and prepends them to the main table.

tail(nz2, 9)

For more information on this feature take a look at this blog post and the Details section of the htmltab function in the package documentation.

Removal of unneeded information

Many HTML tables include additional information which are of little interest to data analysts such as information encoded in superscript and footnote tags, as well as escape sequences. By default, htmltab() removes information from the first two and replaces all escape sequences by a whitespace. You can change this behaviour through the rm_superscript, rm_footnotes, rm_escape, rm_nodata_cols, rm_invisible and rm_whitespace arguments.

Conclusion

HTML tables are a valuable data source but they frequently violate basic principles of data well-formedness. This is usually for good reason since their primary purpose is to increase readability of tabular information. htmltab's goal is to reduce the need for users to interfere when working with HTML tables by relying on available structural information as well as making some assumptions about the table's design. However, you are free to exert more control over the transformation by specifying various function arguments.



crubba/htmltab documentation built on May 14, 2019, 12:05 p.m.