【问题标题】:Returning column subset from list of dataframes in API response in R从R中API响应中的数据框列表返回列子集
【发布时间】:2020-12-29 10:56:31
【问题描述】:

我是一名 API 新手,自学如何在 R 中与 Planet Labs 的 API 交互,但 Planet 的文档是针对 Python 的,我不熟悉(因此在 R 中工作)。

在他们的tutorials 之一上,他们提供了一些 python/curl/jq 代码,该代码从列表中的数据框中返回一列。

➜  curl -L -H "Authorization: api-key $PL_API_KEY" \
    'https://api.planet.com/data/v1/item-types' | jq '.item_types[].id'
"REOrthoTile" # This is the desired output
"PSOrthoTile" # Desired output

我的问题是:如何将上面代码的 jq 部分合并到我在 R 中的 API 请求中,以便响应仅包含相关列而不是整个列表和数据框?我可以通过从更大的列表和数据框中进行子集来实现很长的路要走,但我试图避免用所有不需要的信息混淆 API 响应。

library(httr)
library(jsonlite)

key = "my_Planet_API_key"
res = GET("https://api.planet.com/data/v1/item-types",
           authenticate(user = key, password = ""))
>res
Response [https://api.planet.com/data/v1/item-types]
  Date: 2020-09-11 17:29
  Status: 200
  Content-Type: application/json
  Size: 7.43 kB
{"_links":{"_self":"https://api.planet.com/data/v1/item-type...

data = fromJSON(rawToChar(res$content))

>summary(data)
           Length Class      Mode
_links     1      -none-     list
item_types 5      data.frame list

> str(data, vec.len = 1)
List of 2
 $ _links    :List of 1
  ..$ _self: chr "https://api.planet.com/data/v1/item-types/"
 $ item_types:'data.frame': 15 obs. of  5 variables:
  ..$ _links               :'data.frame':   15 obs. of  1 variable:
  .. ..$ _self: chr [1:15] "https://api.planet.com/data/v1/item-types/PSOrthoTile" ...
  ..$ display_description  : chr [1:15] "PlanetScope orthorectified 4-band imagery as 25km x 25km UTM tiles" ...
  ..$ display_name         : chr [1:15] "PlanetScope Ortho Tile" ...
  ..$ id                   : chr [1:15] "PSOrthoTile" ...
  ..$ supported_asset_types:List of 15
  .. ..$ : chr [1:11] "analytic" ...
  .. ..$ : chr [1:6] "analytic" ...
  .. ..$ : chr [1:16] "analytic" ...
  .. ..$ : chr [1:23] "analytic" ...
  .. ..$ : chr [1:16] "basic_analytic_b1" ...
  .. ..$ : chr [1:14] "analytic_b1" ...
  .. ..$ : chr [1:15] "analytic_b1" ...
  .. ..$ : chr [1:26] "analytic" ...
  .. ..$ : chr [1:12] "ortho_analytic" ...
  .. ..$ : chr [1:3] "video_file" ...
  .. ..$ : chr [1:4] "ortho_analytic_hh" ...
  .. ..$ : chr [1:22] "analytic_gflags" ...
  .. ..$ : chr [1:8] "analytic_granule_pnt" ...
  .. ..$ : chr [1:22] "analytic_gflags" ...
  .. ..$ : chr [1:8] "analytic_granule_pnt" ...

names(data$item_types)
[1] "_links"                "display_description"  
[3] "display_name"          "id"                   
[5] "supported_asset_types"

data$item_types$id # Desired output should be same as Python output above

谢谢!

【问题讨论】:

    标签: python r api curl httr


    【解决方案1】:
    as.data.frame(list.flatten(res[['']])) %>% select()
    

    【讨论】:

    • 请务必在给出代码答案时添加说明。
    【解决方案2】:

    本质上,jq 是一个单独安装的命令行模块,可以在curl 检索或不限于curl 的任何 JSON 数据之后提取或转换 JSON 数据。因此,它不会比 R 的 httr 调用做更多的工作。见GitHub app page

    为避免不必要的 API 信息,只需通过名称链接从返回对象中提取所需的项目:

    data = fromJSON(rawToChar(res$content))$item_types$id
    
    data = fromJSON(rawToChar(res[["content"]]))[["item_types"]][["id"]]
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2015-10-04
      • 2020-11-21
      • 1970-01-01
      相关资源
      最近更新 更多