One row per cell of one <table>, with its position in the grid that
html_table() builds: spans are placed, clipped and checked exactly as
there, and rows are numbered in the same logical order, header rows
included. Use it when the data frame loses what you need: which cells are
headers, how far they span, or the links inside them.
Usage
html_table_cells(x, trim = TRUE, absolute = FALSE, limits = html_limits())Arguments
- x
A nodeset holding exactly one
<table>element.- trim
Whether to trim each cell's text; see
html_text_clean().- absolute
If
TRUE, links are resolved withhtml_url(); otherwise they arehrefas written, as inhtml_links().- limits
Resource limits from
html_limits();max_table_cellsbounds the grid, spans included.
Value
A data frame with one row per <td> or <th>, in grid order
(by row, then column), and columns:
row,column: the integer position of the cell's top-left slot;rowspan,colspan: the integer number of rows and columns it covers, afterrowspan="0"is expanded and spans are clipped;section:"thead","tbody"or"tfoot"; rows directly in the table are"tbody";header:TRUEfor a<th>;text: the cell's cleaned text, without nested tables, as inhtml_table();links: a list of character vectors, thehrefof each<a>and<area>in the cell, outside nested tables.
See also
Other extraction:
html_forms(),
html_links(),
html_list(),
html_table(),
html_url()
Examples
doc <- html_parse(paste0(
"<table><tr><th rowspan=2>Package<th colspan=2>Links",
"<tr><th>Home<th>Docs",
"<tr><td>zuhtml<td><a href='https://example.org/'>site</a>",
"<td><a href='/ref/'>ref</a> <a href='/news/'>news</a></table>"
), base_url = "https://example.org/pkg/")
cells <- html_table_cells(html_element(doc, "table"), absolute = TRUE)
cells[, c("row", "column", "rowspan", "colspan", "header", "text")]
#> row column rowspan colspan header text
#> 1 1 1 2 1 TRUE Package
#> 2 1 2 1 2 TRUE Links
#> 3 2 2 1 1 TRUE Home
#> 4 2 3 1 1 TRUE Docs
#> 5 3 1 1 1 FALSE zuhtml
#> 6 3 2 1 1 FALSE site
#> 7 3 3 1 1 FALSE ref news
cells$links[[7]]
#> [1] "https://example.org/ref/" "https://example.org/news/"