| Title: | 'openFDA' API |
| Version: | 0.1.0 |
| Description: | The 'openFDA' API facilitates access to Federal Drug Agency (FDA) data on drugs, devices, foodstuffs, tobacco, and more with 'httr2'. This package makes the API easily accessible, returning objects which the user can convert to JSON data and parse. Kass-Hout TA, Xu Z, Mohebbi M et al. (2016) <doi:10.1093/jamia/ocv153>. |
| License: | GPL (≥ 3) |
| URL: | https://github.com/simpar1471/openFDA,https://simpar1471.github.io/openFDA/ |
| BugReports: | https://github.com/simpar1471/openFDA/issues |
| Imports: | cli, httr2, checkmate, purrr, rlang, vctrs |
| Config/Needs/website: | rmarkdown |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0), stringr |
| Config/testthat/edition: | 3 |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| VignetteBuilder: | knitr |
| NeedsCompilation: | no |
| Packaged: | 2024-10-17 10:10:15 UTC; tadeo |
| Author: | Simon Parker |
| Maintainer: | Simon Parker <simon.parker.24@ucl.ac.uk> |
| Repository: | CRAN |
| Date/Publication: | 2024-10-18 10:50:30 UTC |
Format character vectors intosearch terms for openFDA API queries
Description
This function is a helper for constructing search queries. Whilst it handlessome of the available formatting for openFDA APIs, it does not recapture allof the search term syntax available to you. To get a full appreciation of theopenFDA search syntax, seehttps://open.fda.gov/apis/advanced-syntax/.
Usage
format_search_term(search, exact = TRUE, mode = "or")Arguments
search | A character vector of length 1 or more. If scalar and unnamed,it will be assumed that you have already formatted your search string towork with the API. If named, the vector will be collapsed to include yourvarious search terms, separated by AND or OR terms based on the value of |
exact | A single-length logical vector. When This parameter only applies if |
mode | A single-length character vector, which defines how searches inmultiple fields should be combined. By default ( This parameter only applies if |
Value
A character vector of the S3 class<AsIS>, with a formatted searchterm which can be supplied toopenFDA().
Note
This function does not check that you're providing accurate field names orsearch terms. It is up to you to make sure you've provided correctly speltfields and search terms.
See Also
format_sort_term()performs similar formatting for thesortcomponentof an openFDA query.I()generates vectors with the<AsIs>S3 class.httr2::req_url()documents whyI()is applied to the output of thisfunction.
Examples
# Provide a formatted search string and the function will do no formattingformat_search_term("openfda.generic_name:verapamil")# Provide a named vector and the function will format it for youformat_search_term(c("openfda.generic_name" = "verapamil"))# If providing multiple elements in your search term, use `exact = FALSE`# to prevent the function from surrounding the term with double quotes.format_search_term(c("openfda.generic_name" = "verapamil+amlodipine"), exact = FALSE)# Provide a longer named vector and function will merge these with an OR# operatorformat_search_term(c("openfda.generic_name" = "verapamil", "openfda.manufacturer_name" = "glaxo*"))# Or you can set the `mode` argument to merge your search terms with an AND# operatorformat_search_term(c("openfda.generic_name" = "verapamil", "openfda.manufacturer_name" = "glaxo*"), mode = "and")Format character vectors intosort terms for openFDA API queries
Description
This function acts as a helper for constructing a sort term in the openFDAAPI.
Usage
format_sort_term(sort)Arguments
sort | A single-length character vector of length 1. If unnamed,it will be assumed that you have already formatted your search string towork with the API. If named, the vector will be collapsed to include yourfield and sorting choice. |
Value
A character vector of the S3 class<AsIS>, with a formatted searchterm which can be supplied toopenFDA().
Note
This function does not check that you're providing accurate field names orsearch terms. It is up to you to make sure you've provided correctly speltfields and search terms.
See Also
format_search_term()performs similar formatting for thesearchcomponent of an openFDA query.I()generates vectors with the<AsIs>S3 class.httr2::req_url()documents whyI()is applied to the output of thisfunction.
Examples
# Provide a formatted search string and the function will do no formattingformat_sort_term("openfda.generic_name:asc")# Provide a named vector and the function will format it for youformat_sort_term(c("openfda.generic_name" = "asc"))# Errors will be thrown if you supply a bad inputtry(format_sort_term("receivedate:no_order"))try(format_sort_term(c("receivedate" = "ascending")))Send requests to the openFDA API
Description
Send requests to the openFDA API
Usage
openFDA( search = "", sort = NULL, count = NULL, limit = 1000, skip = NULL, endpoint = "drug-drugsfda", api_key = get_api_key(), warn_on_http_error = TRUE)Arguments
search | A character vector which will be passed to |
sort | A single string or scalar named character vector describing howto sort the results. The |
count | A single string denoting a field on which to count results. If |
limit | A single integerish value describing the limit on the number ofrecords to retrieve. An error will be thrown if |
skip | A single integer describing how many records should be skipped.If more records are skipped than are found in your search, the openFDAAPI will return a 404 error. |
endpoint | A single-length character vector describing which openFDAendpoint to target.
This argument is case-sensitive. By default, the package will target theDrugs@FDA endpoint ( |
api_key | A single-length character vector with your openFDA API key.By default this is the result of |
warn_on_http_error | A scalar logical value. If |
Value
Anhttr2 response object fromhttr2::req_perform(). You can usehttr2::resp_body_json() to extract JSON data from the response.
References
Kass-Hout TA, Xu Z, Mohebbi M, Nelsen H, Baker A, LEvine J, Johansen E,Bright RA.OpenFDA: an innovative platform providing access to a wealth ofFDA's publicly available dataJ Am Med Inform Assoc 2016,23(3):596-600.doi:10.1093/jamia/ocv153
See Also
format_search_term() documents how inputsearch vectors areconverted to openFDA API searches.
Examples
if (httr2::secret_has_key("OPENFDA_KEY")) { set_api_key(httr2::secret_decrypt( "TEaDtqdFMq9_Montij5p9IY6T57IyqkbF8IYFVOpk-ttxotFUNdJSxgccAnkq4nQhplaf-r3deQ", "OPENFDA_KEY" )) resp <- openFDA(search = "openfda.manufacturer_name:gilead*", limit = 2, skip = 10) # The function returns an `httr2` object print(resp)}# Bad inputs will cause informative errors - here, a bad API key is suppliedtry( openFDA(search = "openfda.manufacturer_name:gilead*", api_key = "BAD_API_KEY", limit = 1))Get and set your openFDA API keys
Description
Get and set your openFDA API keys
Usage
set_api_key(api_key)get_api_key()Arguments
api_key | A single-length character vector with your openFDA API key.You can generate an API key on theFDA website. |
Value
A single length character vector with your API key. Forset_api_key(), this is returned invisibly.
Forget_api_key(), an error will be thrown if no key has been set.
Note
To permanently set the API key for a given project, setOPENFDA_TOKENin.Renviron.
Examples
# Set your openFDA API key with `set_api_key()`api_key <- "example_api_key"set_api_key(api_key)# Retrieve it with `get_api_key()`get_api_key()# An error will be thrown if your API key is an empty string.set_api_key("")try(get_api_key())