Handling adverb results

1. Handling adverb results

In this lesson, we will discover some methods to clean the results of safely() and possibly().

2. Cleaning safely results

Once you've run a safe function, you can call the transpose() function on the results. What this function does is turn a list "inside-out." In other words, if you have a list of length 3 where each component is of length 2, transpose() creates a list of length 2, where each element is of length 3. This can be useful when you have a list in which all elements have the same structure. With transpose(), you can gather all the similar elements inside the same sublist. This kind of situation happens, for example, when you are using safely(). If you use map() on a list of 3 elements, and the function is a safely() function, you will have a list of 3 elements, and each of these elements is a list composed of two other elements: $result and $error. Using transpose() on this output will allow you to gather all the $result in one sublist, and all the $error in another. To sum up, calling map() on a list of 3 with a safely() function returns a list of 3 sublists, each sublist contains 2 elements. If you put this output in a transpose() function, you'll get a list of 2 sublists, each containing 3 elements.

3. About compact()

In the exercises, we have used a combination of discard() and is.null() to remove the NULL from a list. If you need to do that, you can also use the compact() function. This function takes a list, and remove all the NULL elements. This will work for cleaning safely() results, or if you provide NULL as the otherwise argument to possibly().

4. possibly() and compact()

A good practice is to provide NULL as the otherwise argument of possibly(). That way, you can pipe the output of possibly() in compact(), and you'll keep only the elements that didn't return an error. Of course, another good practice if you're doing this is to set the name of the list before piping it into compact(). Why? Because otherwise, you won't have access to the name of the elements that worked.

5. A Gentle introduction to httr

Since the beginning of this chapter, we've been using the read_lines() function to test if a URL is reachable. But there's an R package designed especially for making HTTP requests called httr. One basic function from httr is GET(). This function takes a URL and makes a connection to this URL. If the connection is not possible, an error is returned. If it succeeds, this function returns a lot of information about how the connection went. Notably, the status code, which indicates, with a numeric code, the status of the connection. 200 means it went as expected, 404 that the page is not reachable, and there are a lot of other possible codes. We'll talk about this again in the next chapter. Don't be afraid if you don't know httr: we will only be using the GET() function. If ever you want to know more about this package, feel free to have a look at the package documentation.

6. Let's practice!

Let's try this in some exercises!