在数据框的多行上使用 httr

Using httr on multiple rows of a data frame

我正在尝试使用 R 从 URL 获得响应。我可以使用 httr::GET 后跟 httr::content:

成功编写代码
library(tidyverse)
library(httr)

test <- GET ("https://www.ebi.ac.uk/cgi-bin/ipd/imgt/hla/dpb_v2.cgi?pid=1&patdpb1=01:01&patdpb2=04:01&did=2&dondpb1=01:01&dondpb2=01:01") 
%>% content(as = "text")

我想对数据框的每一行执行此操作。数据类似这样:

test <- tribble(~case, ~mDPB11cd.recipient, ~mDPB12cd.recipient, ~mDPB11cd.donor, ~mDPB12cd.donor, 
101, "04:01", "01:01", "03:01", "04:01",
102, "04:01", "02:01", "04:01", "01:01",
103, "01:01", "104:01", "03:01", "05:01",)

我已经为每一行编写了 assemble 正确 url 的代码,并且可以使用 purrr::map:

获得每一行的响应
DPB1_permisive <- test 
%>% mutate (url =  (paste0("https://www.ebi.ac.uk/cgi-bin/ipd/imgt/hla/dpb_v2.cgi?pid=1&patdpb1=", mDPB11cd.recipient, "&patdpb2=", mDPB12cd.recipient, "&did=2&dondpb1=", mDPB11cd.donor, "&dondpb2=", mDPB12cd.donor))) 
%>% mutate (result = (map(url, GET)))

但是,现在我找不到任何方法来使用 content 来解析我得到的结果。

追加

%>% content(result, as = "text")

上面的代码给出了

的错误

Error in content(., result, as = "text") : is.response(x) is not TRUE

URL 的结果似乎没有正确写入数据框中。 “结果”列显示为“列表”类型,每行的值为“”。

如何让 content 解析响应?

您可以使用 mapGET 请求中获取数据。

library(tidyverse)
library(httr)

test %>% 
  mutate(url = paste0("https://www.ebi.ac.uk/cgi-bin/ipd/imgt/hla/dpb_v2.cgi?pid=1&patdpb1=", mDPB11cd.recipient, "&patdpb2=", mDPB12cd.recipient, "&did=2&dondpb1=", mDPB11cd.donor, "&dondpb2=", mDPB12cd.donor),
         result = map(url, GET), 
         data = map(result, content, as = "text"))

#   case mDPB11cd.recipie… mDPB12cd.recipie… mDPB11cd.donor mDPB12cd.donor url                      result data 
#  <dbl> <chr>             <chr>             <chr>          <chr>          <chr>                    <list> <lis>
#1   101 04:01             01:01             03:01          04:01          https://www.ebi.ac.uk/c… <resp… <chr…
#2   102 04:01             02:01             04:01          01:01          https://www.ebi.ac.uk/c… <resp… <chr…
#3   103 01:01             104:01            03:01          05:01          https://www.ebi.ac.uk/c… <resp… <chr…