I would like to save a html page to a tibble so that I could later use mutate on the page content
I thought about reading the html direct to a tibble:
library(tidyverse)
library(rvest)
#does not work
tibble(html=read_html("https://www.accessdata.fda.gov/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=040445"))
#> Error: All columns in a tibble must be vectors.
#> x Column `html` is a `xml_document/xml_node` object.
Reading as a list
works:
#works
works <- tibble(html=list(read_html("https://www.accessdata.fda.gov/scripts/cder/daf/index.cfm?event=overview.process&ApplNo=040445")))
works
#> # A tibble: 1 x 1
#> html
#> <list>
#> 1 <xml_dcmn>
However, I cannot use mutate
then:
# does not work
works %>%
mutate(table=html_nodes(unlist(page),"#exampleApplSuppl"))
#> Error: Problem with `mutate()` column `table`.
#> i `table = html_nodes(unlist(page), "#examleApplSuppl")`.
Created on 2021-11-02 by the reprex package (v2.0.1)
CodePudding user response:
As the 'html' column is list
, loop over the list
and return the output in a list
library(purrr)
library(dplyr)
works %>%
mutate(table = map(html, ~ html_nodes(.x, "#examleApplSuppl")))
-output
# A tibble: 1 × 2
html table
<list> <list>
1 <xml_dcmn> <xml_ndst>