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


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?



urls <- c("", "", "")

map(urls, httr::GET) %>%
  map_df(., magrittr::extract, c("url", "status_code"))
#> # A tibble: 3 x 2
#>   url                                                          status_code
#>   <chr>                                                              <int>
#> 1                                       200
#> 2         200
#> 3         200

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


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


urls <- c("", "", "")

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


Perfect! Thank you for the informative answer.