View source: R/download_resources.R
fhir_search | R Documentation |
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.
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()
)
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 (see fhir_url()
). The search via GET is the default and performed whenever
the argument body
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 in
fhir_search()
, the url in request
should only contain the base URL and the resource type.
The function will automatically amend it with _search
and perform a POST.
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 the password
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 function fhir_authenticate()
to create this object.
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.
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.
A fhir_bundle_list when save_to_disc = NULL
(the default), else NULL
.
Creating a FHIR search request: fhir_url()
and fhir_body()
(for POST based search)
OAuth2 Authentication: fhir_authenticate()
Saving/reading bundles from disc: fhir_save()
and fhir_load()
Flattening the bundles: fhir_crack()
#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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.