'Iteration over a non-existing file in the directory

I have around five files in my directory that I want to read in r. Each file has a name pattern: "filex.html", where x=1,2,3 and so on. However, a few files are missing. I wanted to create a loop to read all the files and whenever any file is non-existential, the loop should jump to next file in the sequence. However, my loop stops whenever it encounters the first non-existing file.

Following is the loop.

ids = c(1:10)

for (i in ids) {
  myurl = paste("mypage",i,".html") 
  myurl = gsub(" ","",myurl)
  pointer = read_html(myurl)
   if(is_null(pointer)){
     next
   }
    
    
}

This is the error.

Error: 'mypage3.html' does not exist in current working directory ('E:/My_projects/mydb').

How can iterate my loop over the non-existing file?



Solution 1:[1]

Instead of looping over your ids vector that may include non-existant files, try to lapply over a list of the actual files, obtained from list.files().

You can use a pattern to only get the html files with list.files(pattern = "*.html").

Here is an example

html_files = list.files(pattern = "*.html").

lapply(html_files, function(x) {
    pointer = read_html(x)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mhovd