CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Intermediate Functional Programming with purrr

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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, ___)
Modifier et exécuter le code