[R] check broken links in a column
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Jun 21 17:06:10 CEST 2016
On 21/06/2016 10:42 AM, Jenny Vander Pluym - NOAA Federal wrote:
> Hello all,
>
> I am having trouble finding code that will check links in a table, not all
> of the links on a specific web page.
>
> I have csv files that include links to images which are stored on the web.
> I have over 1,000 of them to check.
>
> I see things for curl, but that appears to be specific to pulling
> information from a website vs. just using a column of url in a table. I
> would like the results in a readable table format that tells me which links
> do not work. I do not want all of the images opened on my machine, just the
> links checked.
>
> Thank you so much for your time.
>
> Jenny VP
>
You could loop through the entries, and try to read from each. If the
link doesn't exist, you'll get an error. For example:
urls <- c("http://www.r-project.org", "http://foo.bar")
result <- rep(NA, length(urls))
for (i in seq_along(urls)) {
if (inherits(try(readLines(urls[i], 1), silent = TRUE), "try-error"))
result[i] <- FALSE
else
result[i] <- TRUE
}
You can put together something more sophisticated with tryCatch(), which
would make it easier to catch the warning messages when the reads fail.
Duncan Murdoch
More information about the R-help
mailing list