Safe iterations
As in the previous chapter, let's pretend you are a data analyst working for a web agency. This time, you've been asked to do some web scraping.
(Note: don't be afraid if you don't know how to do web scraping, we'll start simple, and all the functions will be explained).
You have received a list of URLs, but you suspect that some are not real addresses. The first thing you will do is test if you can connect to these URLs. For this, we'll use a simple function from the readr
package: read_lines()
, that we will put inside a safely()
. When given an URL, read_lines()
reads the HTML, or returns an error if the URL is not reachable.
Theurls
vector is available in your workspace. Print it in the console if you want to know what is inside.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
Create a safe version of the
read_lines()
function.Map this newly created function of the provided vector called
urls
.Set the names of the results with the
set_names()
function.Extract the
"error"
element of each sublist.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a safe version of read_lines()
safe_read <- ___(___)
# Map it on the urls vector
res <- ___(urls, ___)
# Set the name of the results to `urls`
named_res <- ___(res, ___)
# Extract only the "error" part of each sublist
___(named_res, ___)