Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a For loop() or lapply() to run with error message in R

Tags:

r

On this code when I use for loop or the function lapply I get the following error

"Error in get_entrypoint (debug_port):
  Cannot connect R to Chrome. Please retry. "


library(rvest)
library(xml2) #pull html data
library(selectr) #for xpath element

url_stackoverflow_rmarkdown <- 
  'https://stackoverflow.com/questions/tagged/r-markdown?tab=votes&pagesize=50'

web_page <- read_html(url_stackoverflow_rmarkdown)

questions_per_page <- html_text(html_nodes(web_page, ".page-numbers.current"))[1]

link_questions <- html_attr(html_nodes(web_page, ".question-hyperlink")[1:questions_per_page], 
                            "href")

setwd("~/WebScraping_chrome_print_to_pdf") 

for (i in 1:length(link_questions)) {
  question_to_pdf <- paste0("https://stackoverflow.com",
                            link_questions[i])

  pagedown::chrome_print(question_to_pdf) 
}

Is it possible to build a for loop() or use lapply to repeat the code from where it break? That is, from the last i value without breaking the code?

Many thanks

like image 450
Laura Avatar asked Dec 10 '19 18:12

Laura


People also ask

Is Lapply faster than for loop in R?

The for loops in R have been made a lot more performant and are currently at least as fast as lapply .

Is Lapply a loop?

The lapply() function does the following simple series of operations: it loops over a list, iterating over each element in that list. it applies a function to each element of the list (a function that you specify) and returns a list (the l is for “list”).

What is the use of looping function in R?

For loop in R Programming Language is useful to iterate over the elements of a list, dataframe, vector, matrix, or any other object. It means, the for loop can be used to execute a group of statements repeatedly depending upon the number of elements in the object.


1 Answers

I edited @Rui Barradas idea of tryCatch(). You can try to do something like below. The IsValues will get either the link value or bad is.

IsValues <- list()
for (i in 1:length(link_questions)) {
  question_to_pdf <- paste0("https://stackoverflow.com",
                            link_questions[i])

  IsValues[[i]] <- tryCatch(
    {
      message(paste("Converting", i))

      pagedown::chrome_print(question_to_pdf)
    },
    error=function(cond) {
      message(paste("Cannot convert", i))
      # Choose a return value in case of error
      return(i)
    }) 
}

Than, you can rbind your values and extract the bad is:

do.call(rbind, IsValues)[!grepl("\\.pdf$", do.call(rbind, IsValues))]

[1] "3"  "5"  "19" "31"

You can read more about tryCatch() in this answer.

like image 163
DJV Avatar answered Sep 29 '22 16:09

DJV