xmlview
: View formatted and "pretty printed" HTML/XML in R
(if you need JSON, \code{list} or general object viewing in R check out xmlview
's sister package, jsonview)
It is intended for interactive use. When working with XML data, it's often times advantageous to be able to see the document and possibly even more advantageous to see the results of XPath or CSS selector slicing & dicing.
You can pass in:
xml2
xml_document
xml2
xml_nodeset
xml2
xml_node
and view the formatted & pretty-printed result in the RStudio viewer or web browser.
The widget uses vkbeautify, highlight.js and xml-viewer to do all the work.
The following functions are implemented:
xml_view
: view HTML/XML nodesxml_tree_view
: view HTML/XML nodesPlease devtools::install_github("ramnathv/htmlwidgets")
for this as we're now using the more modern (and bleeding edge htmlwidgets
functionality).
xml_tree_view()
doc
variable in the generated R code with the passed-in variable name (only really valid for passing in a varialbe that holds an xml_document
)scroll
parameter and refactored xml_view()
(enables multiple "xml views" per Rmd and prbly Shiny toos)xml_view
to not work on Linux RStudio Desktop xml_tree_view()
add_filter
option to xml_view()
for live XPath filtering (so you can see the results immediately and then just copy/paste the XPath string back into R)devtools::install_github("hrbrmstr/xmlview")
options(width=120)
library(xmlview) library(xml2) # available styles highlight_styles() # plain character txt <- "<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>" xml_view(txt) # xml document doc <- read_xml(txt) xml_view(doc, style="obsidian") xml_view(xml_find_all(doc, ".//to"), style="github-gist") xml_view(read_xml(system.file("extdata/dwml.xml", package="xmlview"))) xml_view(read_xml(system.file("extdata/getHistory.xml", package="xmlview")), "androidstudio") xml_view(read_xml(system.file("extdata/input.xml", package="xmlview")), "sunburst") # SO: http://stackoverflow.com/questions/34664028/extract-xml-data-using-xmlsapply-in-r doc <- read_xml("http://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml") xml_view(xml_find_all(doc, ".//*[contains(@_id, '7')]"), style="monokai_sublime")
Here is the R code (it's a bit hard to read):
library(xml2) doc <- read_xml("http://www.npr.org/rss/rss.php?id=1001") str(doc) xml_view(doc, add_filter=TRUE) # The first copied code: xml2::xml_find_all(doc, './/dc:creator', ns=xml2::xml_ns(doc)) # The second copied code: xml_text(xml2::xml_find_all(doc, './/link[contains(., "soccer")]', ns=xml2::xml_ns(doc)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.