Using purrr and httr::GET() to return a data frame


#1

I have the following code that takes shortened Twitter links and returns the actual long-form link and the status code as a tibble. My gut is telling me that I don't need to repeat myself by using map() and map_df() piped together.

Is there a better way to do this?

Reprex:

library(tidyverse)
library(httr)

urls <- c("https://t.co/jM8oizOCHs", "https://t.co/B5BS9Ad2na", "https://t.co/ZXER3i6BvQ")

map(urls, httr::GET) %>%
  map_df(., magrittr::extract, c("url", "status_code"))
#> # A tibble: 3 x 2
#>   url                                                          status_code
#>   <chr>                                                              <int>
#> 1 https://github.com/r-lib/vctrs                                       200
#> 2 https://peerj.com/preprints/3159/?utm_content=bufferc4f11&u~         200
#> 3 https://archervisualization.herokuapp.com/?utm_content=buff~         200

Example links grabbed from @mara 's Twitter :wink:


#2

You can do this all in a single map_dfr function call like this:

library(tidyverse)
library(httr)

urls <- c("https://t.co/jM8oizOCHs", "https://t.co/B5BS9Ad2na", "https://t.co/ZXER3i6BvQ")

original <- map(urls, httr::GET) %>%
  map_df(., magrittr::extract, c("url", "status_code"))


new <- map_dfr(urls, ~{
  httr::GET(.x) %>% 
    magrittr::extract(c("url", "status_code"))
})


identical(original, new)
#> [1] TRUE

Created on 2018-08-02 by the reprex package (v0.2.0).


As a side note, map_dfr is the same as map_df, except that it is explicit in the fact that it is binding by row and not by column (which you would use map_dfc if that was what you wanted).


#3

Perfect! Thank you for the informative answer.