ComeçarComece de graça

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.

Este exercício faz parte do curso

Intermediate Functional Programming with purrr

Ver curso

Instruções do exercício

  • 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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, ___)
Editar e executar o código