0

I have a code that reads each line of my dataframe's first column, visits the website and then downloads the photo of each deputy. But it doesn't work properly because there are some deputies who don't have a photo yet.

That's why my code breaks and stop working. I tried to use "next" and if clauses, but it still didn't work. So a friend recomended me to use the tryCatch(). I couldn't find enough information online, and the code still doesn't work.

The file is here: https://gist.github.com/gabrielacaesar/940f3ef14eaf29d18c3780a66053bbee

deputados <- fread("dep-legislatura56-14jan2019.csv")

i <- 1

while(i <= 514) {
  this.could.go.wrong <- tryCatch(
  attemptsomething(),
  error=function(e) next
  )
  url <- deputados$uri[i]
  api_content <- rawToChar(GET(url)$content)
  pessoa_info <- jsonlite::fromJSON(api_content)
  pessoa_foto <- pessoa_info$dados$ultimoStatus$urlFoto
  download.file(pessoa_foto, basename(pessoa_foto), mode = "wb")
  Sys.sleep(0.5)
  i <- i + 1
}
polo
  • 67
  • 7

2 Answers2

3

Here is a solution using purrr:

library(purrr)

download_picture <- function(url){
  api_content <- rawToChar(httr::GET(url)$content)
  pessoa_info <- jsonlite::fromJSON(api_content)
  pessoa_foto <- pessoa_info$dados$ultimoStatus$urlFoto
  download.file(pessoa_foto, basename(pessoa_foto), mode = "wb")
}

walk(deputados$uri, possibly(download_picture, NULL))
DiceboyT
  • 3,298
  • 5
  • 18
1

Simply wrap tryCatch on the lines that can potentially raise errors and have it return NULL or NA on the error block:

i <- 1

while(i <= 514) {
   tryCatch({
      url <- deputados$uri[i]
      api_content <- rawToChar(GET(url)$content)
      pessoa_info <- jsonlite::fromJSON(api_content)
      pessoa_foto <- pessoa_info$dados$ultimoStatus$urlFoto
      download.file(pessoa_foto, basename(pessoa_foto), mode = "wb")
      Sys.sleep(0.5)
    }, error = function(e) return(NULL)
   )
   i <- i + 1
}
Parfait
  • 87,576
  • 16
  • 87
  • 105