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']"install.packages("selectr")# install.packages("remotes")
remotes::install_github("sjp/selectr")The key functions in selectr are:
-
Translate a CSS selector into an XPath expression with
css_to_xpath(). -
Query an
XMLorxml2document withquerySelector()and its variants.-
Find the first matching node with
querySelector(). -
Find all matching nodes with
querySelectorAll(). -
Find the first matching node in a namespaced document with
querySelectorNS(). -
Find all matching nodes in a namespaced document with
querySelectorAllNS().
-
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">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"))The Makefile wraps the usual tasks:
make testruns the test suite (testthat::test_local()).make lintlints the package (configured by.lintr).make checkbuilds the source tarball and runs a full CRAN-styleR CMD check --as-cranon it (make buildonly builds the tarball).
A devcontainer is provided with R and the package's dependencies preinstalled.