selectr

selectr is a package which makes working with HTML and XML documents easier. It does this by performing translation of CSS selectors into XPath expressions so that you can query XML and xml2 documents easily.

library(selectr)
xpath <- css_to_xpath("#selectr")
xpath
#> [1] "descendant-or-self::*[@id = 'selectr']"

Installation

Install the release version from CRAN

install.packages("selectr")

Install the development version from GitHub

# install.packages("remotes")
# remotes::install_github("sjp/selectr")
install.packages("selectr", repos = c('https://sjp.r-universe.dev', 'https://cloud.r-project.org'))

Overview

The key functions in selectr are:

Examples

Scraping an HTML document

library(selectr)
library(xml2)

html <- paste0(
  "<html><body>",
  "<table id='products'>",
  "<tr><td class='name'>Widget</td><td class='price'>9.99</td></tr>",
  "<tr><td class='name'>Gadget</td><td class='price'>19.99</td></tr>",
  "</table>",
  "<input type='checkbox' checked>",
  "</body></html>")
doc <- read_html(html)

# :has() picks out the table, chaining then walks its rows
rows <- querySelectorAll(doc, "table:has(.price) tr")
querySelectorAll(rows, "td.name")
#> {xml_nodeset (2)}
#> [1] <td class="name">Widget</td>
#> [2] <td class="name">Gadget</td>

# The html translator (auto-detected from read_html()) implements :checked
querySelector(doc, "input:checked")
#> {html_node}
#> <input type="checkbox" checked="checked">

Querying a namespaced XML document

library(selectr)
library(xml2)

# A document with both SVG and MathML content
svgdoc <- read_xml(system.file("demos/svg-mathml.svg", package = "selectr"))
querySelectorAllNS(svgdoc, "svg|script, math|mo",
                   c(svg = "http://www.w3.org/2000/svg",
                     math = "http://www.w3.org/1998/Math/MathML"))

Development

The Makefile wraps the usual tasks:

A devcontainer is provided with R and the package's dependencies preinstalled.