How to process output from API call in R

108 Views Asked by At

I'm trying to get data from the ECB Data Portal by sending request to the API, using httr2 package in R.

Say I want to get EUR/USD exchange rate as in the example on the ECB website. I submit the following request:

install.packages("httr2")
install.packages("tidyverse")
library(httr2)
library(tidyverse)

resp <- request("https://data-api.ecb.europa.eu/service/data/EXR/M.USD.EUR.SP00.A") %>% 
  req_perform() 

How should I parse the response to get "clean" data in the form date-value? I've tried to do like this:

resp <- request("https://data-api.ecb.europa.eu/service/data/EXR/M.USD.EUR.SP00.A") %>% 
  req_perform() %>% 
  resp_body_xml()

but I don't know how to further proceed with the output. Could anyone explain me what I need to do next?

I know there are other ways to get data from the ECB warehouse (e.g. ecb package that produces nice output without any processing) but I would like to know how to do it in more generic way, to be able to work with APIs for which more convenient alternative is not available. Also - I'm using httr2, but I could use Rcurl or httr instead. It doesn't matter as my problem is not with sending the request but with processing the output.

0

There are 0 best solutions below