Search code examples
rpurrrhttr

Using httr on multiple rows of a data frame


I am trying to use R to get a response from a URL. I can successfully write code to do so using httr::GET followed by 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")

I would like to do this for each row of a data frame. Data is similar to this:

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",)

I have written code to assemble the correct url for each row, and can get a response for each using 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)))

However, now I can't find any way to use content to parse the result that I got.

Appending

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

to the code above gives an error of

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

It would seem that the result from the URL is not being written correctly in the data frame. The "result" column shows up as type "list," with each row having a value of "<S3: response>."

How can I get content to parse the responses?


Solution

  • You may use map to get data from GET request.

    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…