-
Notifications
You must be signed in to change notification settings - Fork 16
/
README.Rmd
79 lines (59 loc) · 2.18 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# openfda
## Convenient access to the OpenFDA API
This package provides some simple helpers for accessing the [OpenFDA](https://open.fda.gov/) 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:
```R
install.packages("devtools")
````
Once devtools is installed, you can grab this package:
```R
library("devtools")
devtools::install_github("ropenhealth/openfda")
```
Load it in like any other package:
```{r}
library("openfda")
```
## Examples
```{r, echo=FALSE, results='hide'}
library("knitr")
opts_knit$set(upload.fun = imgur_upload, base.url = NULL)
```
```{r}
patient_ages = fda_query("/drug/event.json") %>%
fda_count("patient.patientonsetage") %>%
fda_exec()
# 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:
```{r, results="hide"}
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.
```{r, results="hide"}
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:
```{r, results="hide"}
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`).