https://github.com/simpar1471/openfda
An R package for the openFDA API (https://open.fda.gov/apis)
https://github.com/simpar1471/openfda
openfda openfda-api rstats rstats-package
Last synced: 7 months ago
JSON representation
An R package for the openFDA API (https://open.fda.gov/apis)
- Host: GitHub
- URL: https://github.com/simpar1471/openfda
- Owner: simpar1471
- License: gpl-3.0
- Created: 2024-10-16T13:17:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-22T16:31:10.000Z (11 months ago)
- Last Synced: 2025-10-06T07:52:50.276Z (8 months ago)
- Topics: openfda, openfda-api, rstats, rstats-package
- Language: R
- Homepage: https://simpar1471.github.io/openFDA/
- Size: 2.62 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codemeta: codemeta.json
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# openFDA
[](https://CRAN.R-project.org/package=openFDA)
[](https://github.com/simpar1471/openFDA/actions/workflows/R-CMD-check.yaml)
[](https://github.com/simpar1471/openFDA/actions/workflows/check-no-suggests.yaml)
[](https://codecov.io/gh/simpar1471/openFDA)
[](https://www.repostatus.org/#active)
[](https://github.com/simpar1471/openFDA/actions?query=workflow%3Apkgcheck)
[](https://github.com/ropensci/software-review/issues/714)
openFDA makes querying the [openFDA API](https://open.fda.gov/apis/) from R a breeze.
The API itself serves publicly available data from the FDA about foods, drugs, devices, and more.
This data includes data such as recall enforcement reports, adverse events, manufacturer details, and -- again -- even more!
Note that the data on openFDA has not been validated for clinical or production use.
## Installation
The easiest way to install openFDA is to get it from CRAN.
However, given a spate of recent improvements which are not yet on CRAN, you may have a better time using the development version.
Nonetheless, to install from CRAN:
```r
install.packages("openFDA")
```
### Development version
```r
# install.packages("pak")
pak::pkg_install("simpar1471/openFDA")
```
## Using openFDA
```{r load_openFDA}
library(openFDA)
```
The full documentation for the API is online, so look at the [openFDA
website](https://open.fda.gov/apis/) to get a full feel for the API itself.
Before using the package, you will need to generate an API key on the [openFDA website](https://open.fda.gov/apis/authentication/).
With an API key you can make 240 requests per minute, and are limited to 120,000 requests per day (the package will handle this request rate throttling for you).
Without a key, you can only make 1000 requests a day.
For this package, if you try to run `openFDA()` without setting an API key, it will fail:
```{r store_api_key, echo = FALSE, eval = TRUE}
rlang::push_options(openFDA.paging = "never",
openFDA.paging_verbosity = "quiet")
encrypted_api_key <- paste0("TEaDtqdFMq9_Montij5p9IY6T57IyqkbF8IYFVOpk-",
"ttxotFUNdJSxgccAnkq4nQhplaf-r3deQ")
decrypted_api_key <- httr2::secret_decrypt(encrypted_api_key, "OPENFDA_KEY")
suppressWarnings(try(
keyring::key_delete("OPENFDA_KEY", "openFDA", "openFDA"),
silent = TRUE)
)
suppressWarnings(try(
keyring::key_delete("OPENFDA_KEY", "openFDA"), silent = TRUE)
)
```
```{r no_api_key, error = TRUE}
search <- openFDA(
search = "openfda.generic_name:furosemide",
limit = 5
)
```
That's not good.
To fix this error, get an API key from the webpage linked above.
Once you have your API key, use `set_api_key()` to store your API key.
Once you run this function, **openFDA** will 'know' your API key, even for future sessions:
```{r setting_api_key_fake, echo = TRUE, eval = TRUE}
# n.b. this API key is a fake, just for demonstration
set_api_key(api_key = "iHXeqlbXtlYEwX9MYLwyCBuiZxfn98Sj3oX2FNtSx")
```
```{r setting_api_key_real, echo = FALSE, eval = TRUE, message = FALSE}
set_api_key(api_key = decrypted_api_key)
```
Let's try using `openFDA()` again, now that you have an API key:
```{r example1_query}
search <- openFDA(
search = "openfda.generic_name:furosemide",
limit = 5
)
search
```
The returned `httr2` response object contains JSON data from the API - you can use `httr2::resp_body_json()` to extract the JSON data as a nested list, then extract the fields you want.
```{r example2_json}
json <- httr2::resp_body_json(search)
json$results[[1]]$openfda$brand_name
json$results[[1]]$openfda$pharm_class_epc
```
I've found [purrr](https://purrr.tidyverse.org/) to be very useful for parsing this data quickly.
```{r example3_purrr}
purrr::map_chr(
.x = json$results,
.f = \(result) purrr::pluck(result, "openfda", "manufacturer_name", 1)
)
```
## Other R packages for openFDA
The [openfda](https://github.com/rOpenHealth/openfda) package from `rOpenHealth` also wraps the openFDA API from R, and is available on GitHub.
It's got a pretty neat structure whereby you build up a query using individual functions for each parameter - though it's my personal preference to keep these as parameters to a single function.
It also makes results available in data frames, which is nice, but I think working with the response object and parsing the underlying JSON yourself permits more powerful interactions with the API.
There is also [FDAopenR](https://github.com/ck2136/FDAopenR/), which is all object-oriented.
I couldn't quite wrap my head around it, but it all appears to be in working order!