Title: | 'openFDA' API |
---|---|
Description: | The 'openFDA' API facilitates access to U.S. Food and Drug Administration (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>. |
Authors: | Simon Parker [aut, cre, cph] |
Maintainer: | Simon Parker <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.0.9000 |
Built: | 2024-11-18 02:53:08 UTC |
Source: | https://github.com/simpar1471/openfda |
search
terms for openFDA API queriesThis function is a helper for constructing search queries. Whilst it handles some of the available formatting for openFDA APIs, it does not recapture all of the search term syntax available to you. To get a full appreciation of the openFDA search syntax, see https://open.fda.gov/apis/advanced-syntax/.
format_search_term(search, exact = TRUE, mode = "or")
format_search_term(search, exact = TRUE, mode = "or")
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 to
work with the API. If named, the vector will be collapsed to include your
various 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 in
multiple fields should be combined. By default ( This parameter only applies if |
A character vector of the S3 class <AsIS>
, with a formatted search
term which can be supplied to openFDA()
.
This function does not check that you're providing accurate field names or search terms. It is up to you to make sure you've provided correctly spelt fields and search terms.
format_sort_term()
performs similar formatting for the sort
component
of an openFDA query.
I()
generates vectors with the <AsIs>
S3 class.
httr2::req_url()
documents why I()
is applied to the output of this
function.
# Provide a formatted search string and the function will do no formatting format_search_term("openfda.generic_name:verapamil") # Provide a named vector and the function will format it for you format_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 # operator format_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 # operator format_search_term(c("openfda.generic_name" = "verapamil", "openfda.manufacturer_name" = "glaxo*"), mode = "and")
# Provide a formatted search string and the function will do no formatting format_search_term("openfda.generic_name:verapamil") # Provide a named vector and the function will format it for you format_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 # operator format_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 # operator format_search_term(c("openfda.generic_name" = "verapamil", "openfda.manufacturer_name" = "glaxo*"), mode = "and")
sort
terms for openFDA API queriesThis function acts as a helper for constructing a sort term in the openFDA API.
format_sort_term(sort)
format_sort_term(sort)
sort |
A single-length character vector of length 1. If unnamed, it will be assumed that you have already formatted your search string to work with the API. If named, the vector will be collapsed to include your field and sorting choice. |
A character vector of the S3 class <AsIS>
, with a formatted search
term which can be supplied to openFDA()
.
This function does not check that you're providing accurate field names or search terms. It is up to you to make sure you've provided correctly spelt fields and search terms.
format_search_term()
performs similar formatting for the search
component of an openFDA query.
I()
generates vectors with the <AsIs>
S3 class.
httr2::req_url()
documents why I()
is applied to the output of this
function.
# Provide a formatted search string and the function will do no formatting format_sort_term("openfda.generic_name:asc") # Provide a named vector and the function will format it for you format_sort_term(c("openfda.generic_name" = "asc")) # Errors will be thrown if you supply a bad input try(format_sort_term("receivedate:no_order")) try(format_sort_term(c("receivedate" = "ascending")))
# Provide a formatted search string and the function will do no formatting format_sort_term("openfda.generic_name:asc") # Provide a named vector and the function will format it for you format_sort_term(c("openfda.generic_name" = "asc")) # Errors will be thrown if you supply a bad input try(format_sort_term("receivedate:no_order")) try(format_sort_term(c("receivedate" = "ascending")))
Send requests to the openFDA API
openFDA( search = "", sort = NULL, count = NULL, limit = 1000, skip = NULL, endpoint = "drug-drugsfda", api_key = get_api_key(), warn_on_http_error = TRUE )
openFDA( search = "", sort = NULL, count = NULL, limit = 1000, skip = NULL, endpoint = "drug-drugsfda", api_key = get_api_key(), warn_on_http_error = TRUE )
search |
A character vector which will be passed to
|
sort |
A single string or scalar named character vector describing how
to 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 of
records 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 openFDA API will return a 404 error. |
endpoint |
A single-length character vector describing which openFDA endpoint to target.
This argument is case-sensitive. By default, the package will target the
Drugs@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 |
An httr2
response object from httr2::req_perform()
. You can use
httr2::resp_body_json()
to extract JSON data from the response.
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 of FDA's publicly available data J Am Med Inform Assoc 2016, 23(3):596-600. doi:10.1093/jamia/ocv153
format_search_term()
documents how input search
vectors are
converted to openFDA API searches.
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 supplied try( openFDA(search = "openfda.manufacturer_name:gilead*", api_key = "BAD_API_KEY", limit = 1) )
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 supplied try( openFDA(search = "openfda.manufacturer_name:gilead*", api_key = "BAD_API_KEY", limit = 1) )
Get and set your openFDA API keys
set_api_key(api_key) get_api_key()
set_api_key(api_key) get_api_key()
api_key |
A single-length character vector with your openFDA API key. You can generate an API key on the FDA website. |
A single length character vector with your API key. For
set_api_key()
, this is returned invisibly.
For get_api_key()
, an error will be thrown if no key has been set.
To permanently set the API key for a given project, set OPENFDA_TOKEN
in .Renviron
.
# 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())
# 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())