Format character vectors into search
terms for openFDA API queries
Source: R/format_search_term.R
format_search_term.Rd
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/.
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
operator
.- exact
A single-length logical vector. When
TRUE
(the default), individual search terms will be surrounded with double quotes (""
). Setexact
toFALSE
if your search term contains multiple words to be searched on, e.g.c("openfda.generic_name" = "losartan+candesartan")
.This parameter only applies if
search
is a named character vector.- mode
A single-length character vector, which defines how searches in multiple fields should be combined. By default (
"or"
) they will be combined with an 'OR' operator, but you can make an 'AND' operator be used instead ("and"
). This argument is case-sensitive and will throw an error ifmode
is not one of either"and"
or"or"
.This parameter only applies if
search
is a named character vector.
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")
#> [1] "openfda.generic_name:verapamil"
# Provide a named vector and the function will format it for you
format_search_term(c("openfda.generic_name" = "verapamil"))
#> [1] "openfda.generic_name:%22verapamil%22"
# 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)
#> [1] "openfda.generic_name:verapamil+amlodipine"
# 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*"))
#> [1] "openfda.generic_name:%22verapamil%22+openfda.manufacturer_name:%22glaxo%2A%22"
# 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")
#> [1] "openfda.generic_name:%22verapamil%22+AND+openfda.manufacturer_name:%22glaxo%2A%22"