Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R rvest: could not find function "xpath_element"

I am trying to simply replicate the example of rvest::html_nodes(), yet encounter an error:

library(rvest)
ateam <- read_html("http://www.boxofficemojo.com/movies/?id=ateam.htm")
html_nodes(ateam, "center")

Error in do.call(method, list(parsed_selector)) : could not find function "xpath_element"

The same happens if I load packages such as httr, xml2, selectr. I seem to have the latest version of these packages too...

In which packages are functions such as xpath_element, xpath_combinedselector located? How do I get it to work? Note that I am running on Ubuntu 16.04, so that code might work on other platforms...

like image 893
Matifou Avatar asked Nov 29 '16 19:11

Matifou


2 Answers

I understand this issue is rather old but I wanted to post a comment for those who may have similar issues.

I stumbled upon this same error and was unable to find much help. So, I thought instead of targeting CSS I would try to target the xpath instead. I do not know what the best practice is.

My original functions worked fine on Ubuntu 16, R 3.4.0. However, they failed on Debian 8 R 3.3.3 and R 3.4.0.

When I modified my code to target xpaths instead of css they began working as expected. For example, changing this...

contents <- link %>% 
    xml2::read_html() %>%
    rvest::html_nodes(css = "pre") %>%
    rvest::html_text()

to this...

contents <- link %>%
    xml2::read_html() %>%
    rvest::html_nodes(xpath = "//pre") %>%
    rvest::html_text()

resolved my issue.

like image 81
timtrice Avatar answered Nov 01 '22 04:11

timtrice


As pointed out by @tbrugz, the issue seems to come form package selectr.

This happens however only when the package is installed with apt-get install r-cran-selectr. Installing the package with sudo R, then install.packages works fine.

pkg <- installed.packages()
subset(as.data.frame(pkg), Package=="selectr", c("Package", "LibPath"))
      Package                                         LibPath
  selectr   selectr /home/matifou/R/x86_64-pc-linux-gnu-library/3.3
  selectr.1 selectr                         /usr/lib/R/site-library
library(selectr, lib.loc="/home/matifou/R/x86_64-pc-linux-gnu-library/3.3")
css_to_xpath(".testclass")
  [1] "descendant-or-self::*[@class and contains(concat(' ', normalize-    space(@class), ' '), ' testclass ')]"

detach("package:selectr", unload=TRUE)

library(selectr, lib.loc="/usr/lib/R/site-library")
css_to_xpath(".testclass")
  Error in do.call(method, list(parsed_selector)) : 

could not find function "xpath_class"

like image 27
Matifou Avatar answered Nov 01 '22 04:11

Matifou