format_search_term {openFDA} | R Documentation |
Format character vectors into search
terms for openFDA API queries
Description
This 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/.
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 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 |
Value
A character vector of the S3 class <AsIS>
, with a formatted search
term which can be supplied to openFDA()
.
Note
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.
See Also
-
format_sort_term()
performs similar formatting for thesort
component of an openFDA query. -
I()
generates vectors with the<AsIs>
S3 class. -
httr2::req_url()
documents whyI()
is applied to the output of this function.
Examples
# 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")