I am trying to create a dataframe of all directory URLs for my Github repository. This is what I wrote:
library(httr)
req <- GET("https://api.github.com/repos/thedivtagguy/daily-data/git/trees/master")
In urls
, I wish to only store the URLs of those items of type: tree
. This is what req
looks like:
{
"sha": "e5acaf1fd8973e010922ffc4366af68359de8456",
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/trees/e5acaf1fd8973e010922ffc4366af68359de8456",
"tree": [
{
"path": ".gitignore",
"mode": "100644",
"type": "blob",
"sha": "aaf221658979cc888d499702eea62beafa52d4e4",
"size": 631,
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/blobs/aaf221658979cc888d499702eea62beafa52d4e4"
},
{
"path": "README.md",
"mode": "100644",
"type": "blob",
"sha": "2b0515e164c8a76d877324984f01e4bde6725410",
"size": 1119,
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/blobs/2b0515e164c8a76d877324984f01e4bde6725410"
},
{
"path": "_config.yml",
"mode": "100644",
"type": "blob",
"sha": "2f7efbeab578c8042531ea7908ee8ffd7589fe46",
"size": 27,
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/blobs/2f7efbeab578c8042531ea7908ee8ffd7589fe46"
},
{
"path": "byrne_pebbles_8.png",
"mode": "100644",
"type": "blob",
"sha": "95d9552636489a812d164e63776892c91c859785",
"size": 10862,
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/blobs/95d9552636489a812d164e63776892c91c859785"
},
{
"path": "dd01_kharifAndRabiCrops",
"mode": "040000",
"type": "tree",
"sha": "b0863850cf04b73f76a8ed1f60558c6d340d142a",
"url": "https://api.github.com/repos/thedivtagguy/daily-data/git/trees/b0863850cf04b73f76a8ed1f60558c6d340d142a"
}
This is what I have written:
reponse <- content(req)$tree
urls <- response %>%
map( ~ ., ~ filter(.x, type == "tree")) %>%
unnest(url)
But this does not work, I am getting this error:
Error in UseMethod("unnest") :
no applicable method for 'unnest' applied to an object of class "list"
How do I filter so that I can only store the URLs of items of type tree
? I know how do to this in base R but I would prefer a tidy approach.
CodePudding user response:
We may loop over the 'response' nested list
, and convert the named inner list to tibble
by binding with bind_cols
- if we use map
to loop, it will return a list
of tibble
, by adding the suffix _dfr
, it rbinds the list
s of tibbles to a single tibble output. Then, do the filter
ing on the 'type' column
library(purrr)
library(dplyr)
map_dfr(response, bind_cols) %>%
filter(type == 'tree')
-output
# A tibble: 8 × 6
path mode type sha size url
<chr> <chr> <chr> <chr> <int> <chr>
1 dd01_kharifAndRabiCrops 040000 tree b0863850cf04b73f76a8ed1f60558c6d340d142a NA https://api.github.com/repos/thedivta…
2 dd02_commonSenseMedia 040000 tree 93461ea8897a3dcd026a88e62a284ba4a835a219 NA https://api.github.com/repos/thedivta…
3 dd03_cropYieldsD3 040000 tree 63b5de06defae6e8524e40d7942ba61ed2a599fc NA https://api.github.com/repos/thedivta…
4 dd04_digitsPi 040000 tree 0f7e99e051a41fef6cde2706e6ded7538c3b6f8f NA https://api.github.com/repos/thedivta…
5 dd05_indiaR 040000 tree 2bd552a7f9918602df043d54130c3d0b33d27294 NA https://api.github.com/repos/thedivta…
6 dd06_ttDrWho 040000 tree 7a0e0461228e0b39ba202032e09b46ba49e7eab6 NA https://api.github.com/repos/thedivta…
7 dd07_ggWaves 040000 tree 06def68fecdf576d45efa16ba6d98bba27edcc8f NA https://api.github.com/repos/thedivta…
8 resources 040000 tree ed4cb9ff46f19764292dea8e655dd4a500389dda NA https://api.github.com/repos/thedivta…