Skip to contents

This function acts as a helper for constructing a sort term in the openFDA API.

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 to work with the API. If named, the vector will be collapsed to include your field and sorting choice.

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_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.

Examples

# Provide a formatted search string and the function will do no formatting
format_sort_term("openfda.generic_name:asc")
#> [1] "openfda.generic_name:asc"

# Provide a named vector and the function will format it for you
format_sort_term(c("openfda.generic_name" = "asc"))
#> [1] "openfda.generic_name:asc"

# Errors will be thrown if you supply a bad input
try(format_sort_term("receivedate:no_order"))
#> Error in format_sort_term("receivedate:no_order") : 
#>   `sort` must end with either "asc" or "desc".
#> ! `sort` was "receivedate:no_order".
try(format_sort_term(c("receivedate" = "ascending")))
#> Error in format_sort_term(c(receivedate = "ascending")) : 
#>   `sort` must be either "asc" or "desc".
#> ! `sort` was "ascending".