Вот способ сделать это с tryCatch
.
library(tidyverse)
library(rvest)
map(urls, ~ {
name <- .x[1]
link <- .x[2]
Sys.sleep(2)
tryCatch({
temp <- link %>%
read_html() %>%
html_nodes("table") %>%
html_table(fill = TRUE)
map_df(temp, ~filter_all(.x, any_vars(. %in% c("Singapore", "SGP")))) %>%
mutate(name = name)
}, error = function(e) NA
)
})
#[[1]]
#[1] X1 X2 name
#<0 rows> (or 0-length row.names)
#[[2]]
#[1] NA
#[[3]]
# X1 X2 X3 X4 name
#1 Celanese PTE. LTD. NA Singapore NA Celanese Corp
#2 Celanese Singapore Acetyls Holding PTE. LTD. NA Singapore NA Celanese Corp
#3 Celanese Singapore Chemical Holding PTE. LTD. NA Singapore NA Celanese Corp
#4 Celanese Singapore PTE. LTD. NA Singapore NA Celanese Corp
#5 Celanese Singapore VAM PTE. LTD. NA Singapore NA Celanese Corp
#6 Celanese Singapore Emulsions PTE. LTD. NA Singapore NA Celanese Corp
Хотя это выдает предупреждение, но работает без ошибок.