Learn R Programming

rvest (version 1.0.4)

html_table: Parse an html table into a data frame

Description

The algorithm mimics what a browser does, but repeats the values of merged cells in every cell that cover.

Usage

html_table(
  x,
  header = NA,
  trim = TRUE,
  fill = deprecated(),
  dec = ".",
  na.strings = "NA",
  convert = TRUE
)

Value

When applied to a single element, html_table() returns a single tibble. When applied to multiple elements or a document, html_table() returns a list of tibbles.

Arguments

x

A document (from read_html()), node set (from html_elements()), node (from html_element()), or session (from session()).

header

Use first row as header? If NA, will use first row if it consists of <th> tags.

If TRUE, column names are left exactly as they are in the source document, which may require post-processing to generate a valid data frame.

trim

Remove leading and trailing whitespace within each cell?

fill

Deprecated - missing cells in tables are now always automatically filled with NA.

dec

The character used as decimal place marker.

na.strings

Character vector of values that will be converted to NA if convert is TRUE.

convert

If TRUE, will run type.convert() to interpret texts as integer, double, or NA.

Examples

Run this code
sample1 <- minimal_html("
  Col ACol B
  1x
  4y
  10z
")
sample1 %>%
  html_element("table") %>%
  html_table()

# Values in merged cells will be duplicated
sample2 <- minimal_html("
  ABC
  123
  45
  67
")
sample2 %>%
  html_element("table") %>%
  html_table()

# If a row is missing cells, they'll be filled with NAs
sample3 <- minimal_html("
  ABC
  12
  3
  4
")
sample3 %>%
  html_element("table") %>%
  html_table()

Run the code above in your browser using DataLab