Create a function

We've seen how we can use safely() to identify non-reachable urls in the previous exercise: we wrote a little process that called a safe version of read_lines(), and returned a list of $errors.

In this exercise, we'll try another approach, as we won't focus on errors only. Instead of mapping a safe function and extracting the "error" elements from the results, we will write a helper function that will immediately discard() the NULL elements of the output of safe_read().

This way, instead of extracting the $error or $result part of the output, we'll be able to know if the elements are reachable (the content is returned in $results) or if it's not (then the error is returned in $error).

The urls vector has been provided for you.

This exercise is part of the course

Intermediate Functional Programming with purrr

View Course

Exercise instructions

  • Create a safe version of read_lines().

  • Create a function called safe_read_discard() that will run the safe version of read_lines() and discard() the NULL elements.

  • Map this function on the url list that has been provided for you.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a safe version of read_lines()
safe_read <- ___

# Code a function that discard() the NULL from safe_read()
safe_read_discard <- function(url){
  ___(___) %>%
    ___(is.null)
}

# Map this function on the url list
res <- ___(urls, ___)