Learn R Programming

openfda

Convenient access to the OpenFDA API

This package provides some simple helpers for accessing the OpenFDA API from R. It uses the jsonlite and magrittr packages to provide a simple way to convert from OpenFDA queries to R dataframes suitable for quick analysis and plotting.

Installation

This library has not yet been added to CRAN, so you'll need the devtools package to install it:

install.packages("devtools")

Once devtools is installed, you can grab this package:

library("devtools")
devtools::install_github("ropenhealth/openfda")

Load it in like any other package:

library("openfda")

Examples

patient_ages = fda_query("/drug/event.json") %>%
               fda_count("patient.patientonsetage") %>%
               fda_exec()
## Fetching: https://api.fda.gov/drug/event.json?search=&count=patient.patientonsetage
# patient ages is now a data frame with "term" and "count" columns
# let's plot it with ggplot2
library("ggplot2")
qplot(x=term, y=count, data=patient_ages)

You can filter the results to count on using the fda_filter() method:

paxil_ages = fda_query("/drug/event.json") %>%
               fda_filter("patient.drug.openfda.generic_name", "paroxetine") %>%
               fda_count("patient.patientonsetage") %>%
               fda_exec()

Using this API with your API key is easy: just add fda_api_key("MY_KEY") to your pipeline.

patient_ages = fda_query("/drug/event.json") %>%
               fda_api_key("MYKEY") %>%
               fda_count("patient.patientonsetage") %>%
               fda_exec()

You can also specify options up front and re-use the query:

age_query = fda_query("/drug/event.json") %>%
            fda_api_key("MYKEY") %>%
            fda_count("patient.patientonsetage");

paxil_ages = age_query %>% fda_filter("patient.drug.openfda.generic_name", "paroxetine") %>% fda_exec()
zoloft_ages = age_query %>% fda_filter("patient.drug.openfda.generic_name", "sertraline") %>% fda_exec()

R documentation for each method is also provided in the package (? fda_exec).

Copy Link

Version

Install

install.packages('openFDA')

Version

1.7.0.9000

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Russell Power

Last Published

October 18th, 2024

Functions in openFDA (1.7.0.9000)

fda_fetch

Fetch the given URL as JSON.
fda_filter

Apply a filter to a query
fda_exec

Execute a query.
fda_api_key

Attach an API key to the query.
extract_field

Fetch a (nested field) from a list or dataframe.
fda_debug

Turn off/on API debugging.
%>%

Pipe operator for chaining together operations.
fda_count

Count results from a given field.
copy_query

Make a copy of query (keeps the class).
fda_limit

Set the number of results desired by this query.
fda_plot

Plot a count query.
fda_url

Return the URL that will be fetched for the current query.
fda_query

Create a new query.
fda_search

Fetch search results.
fda_skip

Set the number of records to skip that match the search parameter.
openfda

openfda: A package for interfacing to the OpenFDA API