Everything in one call
In order to make this code even more reproducible, we are going to create a function that does it in one call. We have already provided you a skeleton for this function, now it's your turn to complete it!
In the previous exercises, we have written the process in several steps. Now, we want this to be done in just one call: we'll then write a function that takes a list of URLs, and return the names of the elements that are not reachable.
Once you have written this function, you could save it, and reuse it whenever you need to clean a list of URLs. And maybe put it into a package ;)
The urls
list from the previous exercise is available in your workspace.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
Create, inside the
map()
call, apossibly()
version ofread_lines()
that will otherwise return a404
.Set the names of the output.
Use the
paste()
function with thecollapse
argument set to" "
to turn each sublist into a character vector.Remove the elements which are equal to 404.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
url_tester <- function(url_list){
url_list %>%
# Map a version of read_lines() that otherwise returns 404
map( ___(___, otherwise = ___) ) %>%
# Set the names of the result
___( urls ) %>%
# paste() and collapse each element
map(___, ___ = " ") %>%
# Remove the 404
___(___) %>%
names() # Will return the names of the good ones
}
# Try this function on the urls object
url_tester(urls)