1

I am writing a code to scrape data from websites in R where I'm bound to get an error saying no such directory was found.

It is a part of a longer for-loop and in case of error when the loop usually ends itself, I wish to continue the loop but instead, the iteration should move to the next count.

Code:

library(geniusr)

lyrics_list = list(length = nrow(artists))
for(i in 1:50)

{
  counter = 1
  for(j in 1:row(artist_list[[i]]))
  {
    if(error == TRUE)
    {
      counter = j+1
      next
    }
    lyrics_list[[i]][[counter]] <- list(get_lyrics_search(artist_name = artist_list[[i]][counter,1], song_title = artist_list[[i]][counter,30]))
    counter = counter + 1
  }
}

This was my earlier approach before I tried my hands with tryCatch()

library(geniusr)

lyrics_list = list(length = nrow(artists))

for(i in 1:50)
{
  counter = 1
  for(j in 1:nrow(artist_list[[i]]))
  {
    tryCatch({lyrics_list[[i]][[counter]] <- list(get_lyrics_search(artist_name = artist_list[[i]][counter,1], song_title = artist_list[[i]][counter,30]))
    counter = counter + 1})
  }
}

The expected error message which I'll encounter in the loop is:

Error in open.connection(x, "rb") : HTTP error 404.

In case of the error, I want to move to the next iteration, i.e., increase j or increase I if that's the case

All suggestions for the if() conditional part will be welcomed

driver
  • 273
  • 1
  • 13
  • you want to use `tryCatch()` I think. [see the highly rated answer here for more detail](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r) – Paul Stafford Allen Oct 06 '22 at 12:11
  • `tryCatch()` is something I read about too, but not sure how that will work in my case. @PaulStaffordAllen any advice on how to use it will be helpful – driver Oct 06 '22 at 12:12
  • Presumably, your function `get_lyrics_search()` is throwing the error, so I'd start by wrapping that in a `tryCatch()` call and see how you get on. Details of how to set it up are in the link I provided. – Paul Stafford Allen Oct 06 '22 at 12:16
  • @PaulStaffordAllen you got it right that the function `get_lyrics_search()` is creating the error. the link you provided is not helping me and I have been left with more unanswered question. if you can show me in my case how to implement it, that shall be great – driver Oct 06 '22 at 12:24
  • please update the question with the new things you've tried and we can see what can be suggested. – Paul Stafford Allen Oct 06 '22 at 13:02
  • please recheck now @PaulStaffordAllen – driver Oct 06 '22 at 13:07

1 Answers1

0

Try replacing your trycatch with the fol­lowing:

tryCatch({ ​ ​ ​ lyrics_list­[[i]][[counter]] <- list(get_lyrics_sear­ch(artist_name = art­ist_list[[i]][counte­r,1], song_title = artist_list[[i]][coun­ter,30])) ​ ​ counter = coun­ter + 1 ​ ​ }, error = fun­ction(e) {conditionM­essage(e) } ​ ​ )

This is what my professor suggested!

driver
  • 273
  • 1
  • 13