Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save result as "ND" when there is no record? rvest and R

I have these two example html: url1.html ; url2.html

In URL1.html there is no information (71) and in URL2.html there is.

I’m using this code in R:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(rvest)
library(tidyverse)

x<-data.frame(
    URL=c(1:2),
    page=c(paste(readLines("url1.html"), collapse="\n"),
                 paste(readLines("url2.html"), collapse="\n"))
) 

for (i in 1:nrow(x)){
    html<-x$page[i]%>% unclass() %>% unlist()
    read_html(html,encoding = "ISO-8859-1") %>% 
        rvest::html_elements(xpath = '//*[@id="principal"]/table[2]') %>% 
        rvest::html_elements(xpath = '//div[@id="tituloContext"]') %>% 
        html_text()%>%  
        str_replace_all(.,"[\\n\\r\\t]+", "")%>%
        stringr::str_trim( ) -> x$title[i]
}

Result: title

[1] "Â  CARRINHO DE LIXO PARA LIMPEZA URBANA"
character(0)

Problem: although I’m bringing the correct content from URL1, I would like to save the "-" value when it doesn’t exist (e.g. URL2)

Expected output: not available (ND).

[1] "Â  CARRINHO DE LIXO PARA LIMPEZA URBANA"
[1] "ND" 

Any idea how to solve this problem?

Is it possible to optimize this code as well?

>Solution :

We could check the length and if it is 0 (length(character(0)) is 0), change the value to ‘ND’

for (i in seq_len(nrow(x))){
    html<-x$page[i]%>% 
                 unclass() %>%
                 unlist()
    read_html(html,encoding = "ISO-8859-1") %>% 
        rvest::html_elements(xpath = '//*[@id="principal"]/table[2]') %>% 
        rvest::html_elements(xpath = '//div[@id="tituloContext"]') %>% 
        html_text()%>%  
        str_replace_all(.,"[\\n\\r\\t]+", "")%>%
        stringr::str_trim( ) -> tmp
      if(length(tmp) == 0) tmp <- "ND"
      x$title[i] <- tmp
}

-checking

> x$title
[1] "CARRINHO DE LIXO PARA LIMPEZA URBANA" "ND"       
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading