fhir_search {fhircrackr} | R Documentation |
Download FHIR search result
Description
Downloads all FHIR bundles of a FHIR search request from a FHIR server by iterating through the bundles. Search via GET and POST is possible, see Details.
Usage
fhir_search(
request = fhir_current_request(),
body = NULL,
username = NULL,
password = NULL,
token = NULL,
add_headers = NULL,
max_bundles = Inf,
verbose = 1,
delay_between_attempts = c(1, 3, 9, 27, 81),
log_errors = NULL,
save_to_disc = NULL,
delay_between_bundles = 0,
rm_tag = "div",
max_attempts = deprecated()
)
Arguments
request |
An object of class fhir_url or a character vector of length one containing the full FHIR search request. It is
recommended to explicitly create the request via |
body |
A character vector of length one or object of class |
username |
A character vector of length one containing the username for basic authentication. |
password |
A character vector of length one containing the password for basic authentication. |
token |
A character vector of length one or object of class httr::Token, for bearer token authentication (e.g. OAuth2). See |
add_headers |
A named character vector of custom headers to add to the HTTP request, e.g. |
max_bundles |
Maximal number of bundles to get. Defaults to Inf meaning all available bundles are downloaded. |
verbose |
An integer vector of length one. If 0, nothing is printed, if 1, only finishing message is printed, if > 1, downloading progress will be printed. Defaults to 1. |
delay_between_attempts |
A numeric vector specifying the delay in seconds between attempts of reaching the server
that |
log_errors |
Either |
save_to_disc |
Either |
delay_between_bundles |
A numeric scalar specifying a time in seconds to wait between pages of the search result, i.e. between downloading the current bundle and the next bundle. This can be used to avoid choking a weak server with too many requests to quickly. Defaults to zero. |
rm_tag |
Character vector of length 1 defining an xml tag of elements that will removed from the bundle automatically.
Defaults to |
max_attempts |
The number of maximal attempts is now determined by the length of |
Details
Request type
fhir_search
allows for two types of search request:
FHIR search via GET: This is the more common approach. All information on which resources to download is contained in the URL that is send to the server (
request
argument). This encompasses the base url of the server, the resource type and possible search parameters to further qualify the search (seefhir_url()
). The search via GET is the default and performed whenever the argumentbody
is NULL.FHIR search via POST: This option should only be used when the parameters make the search URL so long the server might deny it because it exceeds the allowed length. In this case the search parameters (everything that would usually follow the resource type after the
?
) can be transferred to a body of type"application/x-www-form-urlencoded"
and send via POST. If you provide a body infhir_search()
, the url inrequest
should only contain the base URL and the resource type. The function will automatically amend it with_search
and perform a POST.
Authentication
There are several ways of authentication implemented in fhir_search()
. If you don't need any authentication,
just leave the arguments described in the following at their default values of NULL
.
Basic Authentication: Provide the
username
and thepassword
for basic authentication in the respective arguments.Token Authentication: Provide a token in the argument
token
, either as a character vector of length one or as as an object of class httr::Token. You can use the functionfhir_authenticate()
to create this object.
Additional headers
Per default, the underlying HTTP requests are equipped with Accept and Authorization headers. If you need to pass additional headers,
e.g. cookies for authentication or other custom headers, you can add these to the request as a named character vector using the
add_headers
argument.
HTML removal
FHIR resources can contain a considerable amount of html code (e.g. in a narrative object),
which is often created by the server for example to provide a human-readable summary of the resource.
This data is usually not the aim of structured statistical analysis, so in the default setting fhir_search()
will remove the html
parts immediately after download to reduce memory usage (on a hapi server typically by around 30%, see fhir_rm_div()
).
The memory gain is payed with a runtime increase of 10%-20%. The html removal can be disabled by setting rm_tag = NULL
to increase speed at the cost of increased memory usage.
Value
A fhir_bundle_list when save_to_disc = NULL
(the default), else NULL
.
See Also
Creating a FHIR search request:
fhir_url()
andfhir_body()
(for POST based search)OAuth2 Authentication:
fhir_authenticate()
Saving/reading bundles from disc:
fhir_save()
andfhir_load()
Flattening the bundles:
fhir_crack()
Examples
#the try({}, silent = TRUE) statement is only there to catch errors when the server is down
#you can skip it when the server is reachable
try({
### Search with GET
#create fhir search url
request <- fhir_url(url = "https://server.fire.ly",
resource = "Patient",
parameters = c(gender="female"))
#download bundles
bundles <- fhir_search(request, max_bundles = 5)
### Search with POST (should actually be used for longer requests)
request <- fhir_url(url = "https://server.fire.ly",
resource = "Patient")
body <- fhir_body(content = list(gender = "female"))
bundles <- fhir_search(request = request,
body = body,
max_bundles = 5)
}, silent = TRUE)