You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言循环批量调用API并提取返回Id生成汇总表格问题

问题根源

你遇到的cannot coerce class ‘"jqr"’ to a data.frame报错,是因为运行环境中加载了jqr包,它的map函数覆盖了purrr包的同名map函数,返回的是特殊的jqr类对象而非普通列表,同时你之前的处理结果中,实际的响应数据存储在对象的$data子节点中。


完整批量调用+结果汇总方案

如果你重新运行全流程,可以参考以下规范代码,避免函数冲突:

# 加载依赖包,显式调用purrr避免函数冲突
library(httr)
library(jsonlite)
library(purrr)

# 批量执行API请求
response_list <- purrr::map(data_upsert, function(req_body) {
  httr::POST(
    url = 'https://api.secure.com/v1/people/Create',
    httr::add_headers(.headers = headers_upsert),
    body = req_body
  )
})

# 直接提取所有响应的Id字段并合并为数据框
res_table <- purrr::map_dfr(response_list, function(resp) {
  resp_content <- httr::content(resp, as = "parsed")
  data.frame(return_Id = as.character(resp_content$Id))
})

# 如果需要关联原始数据的Id,可直接补充:
res_table$original_Id <- test$Id

现有响应对象修复方案

如果你已经完成了批量调用,仅需要处理现有带jqr类的响应对象,用以下代码提取即可:

# 先从jqr对象中提取实际的响应列表
actual_responses <- your_response_object$data

# 提取Id并转为数据框
res_table <- purrr::map_dfr(actual_responses, ~ data.frame(Id = as.character(.x$Id)))

内容的提问来源于stack exchange,提问作者user16799707

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:33:03