我正在嘗試為我的 Github 存盤庫創建所有目錄 URL 的資料框。這是我寫的:
library(httr)
req <- GET("https://api.github.com/repos/thedivtagguy/daily-data/git/trees/master")
在 中urls,我希望只存盤type: tree. 這req看起來像:
{
"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"
}
這是我寫的:
reponse <- content(req)$tree
urls <- response %>%
map( ~ ., ~ filter(.x, type == "tree")) %>%
unnest(url)
但這不起作用,我收到此錯誤:
Error in UseMethod("unnest") :
no applicable method for 'unnest' applied to an object of class "list"
如何過濾以便我只能存盤型別專案的 URL tree?我知道如何在基礎 R 中做到這一點,但我更喜歡整潔的方法。
uj5u.com熱心網友回復:
我們可能會使用
library(purrr)
library(dplyr)
map_dfr(response, bind_cols) %>%
filter(type == 'tree')
-輸出
# 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…
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368505.html
上一篇:pivot_wider溶解排列
