Is there a way to get data from OpenFDA via their API ?

Hi,

I'm wondering if we can download data from OpenFDA into R studio ?

https://open.fda.gov/apis/downloads/

Much appreciated

According to your link, yes. Did you check the sections "API Basics" and "Construct the Query"? The idea is to build an URL that is a valid query, using the keywords they describe. So they give an example query:

https://api.fda.gov/drug/event.json?limit=1

In R, that means you need to "call" that URL, receive the result, and process them. The library httr is nice for making queries.

library(httr)
library(jsonlite)

response <- GET("https://api.fda.gov/drug/event.json?limit=1")

content <- fromJSON(rawToChar(response$content))

res <- content$results

res$companynumb
#> [1] "HQWYE821915MAR04"

Don't hesitate to explore each step, briefly with $content you get the content of the response (httr also gives you a lot of other information about your query). At this point it's binary data, so you use rawToChar() to get a normal string. For this particular query, the response was in json format, so it can be read with jsonlite. At this point if you peek inside the object you see it's the same thing that gets displayed when you open the query in your browser. So you can access particular subfields using $.

Depending on the structure of the data, you may need to think hard about the right way to extract the information you need.

To build a different query programmatically, you can use paste0().

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.