R on FHIR is an easy to use wrapper around the ‘HL7 FHIR’ REST API (STU 3 and R4). It provides tools to easily read and search resources on a FHIR server and bring the results into the R environment. R on FHIR is based on the FhirClient of the official ‘HL7 FHIR .NET API’, also made by Firely.
# To download the latest release from CRAN use:
install.packages("RonFHIR")
# Or download the development version from GitHub:
# install.packages("devtools")
::install_github("FirelyTeam/RonFHIR") devtools
library(RonFHIR)
# Setting up a fhirClient
<- fhirClient$new("https://vonk.fire.ly/")
client
# Setting up a fhirClient with OAuth 2.0
<- fhirClient$new("Endpoint of FHIR server that supports SMART on FHIR OAuth2 access")
client
<- "id"
client_id <- "secret"
client_secret <- "TestApp"
app_name <- c("patient/*.read")
scopes
<- httr::oauth_app(appname = app_name, client_id, client_secret)
app <- httr::oauth_endpoint(authorize = paste(client$authUrl, "?aud=", client$endpoint, sep=""), access = client$tokenUrl)
oauth_endpoint
<- httr::oauth2.0_token(endpoint = oauth_endpoint, app = app, scope = scopes)
token
$setToken(token)
client
# Search
<- client$search("Patient", c("name=Peter", "address-postalcode=3999"))
bundle
while(!is.null(bundle)){
# Do something useful here
# Go to the next page of the bundle using FHIRs paging mechanism
<- client$continue(bundle)
bundle
}
# Searching with a searchParams object
<- searchParams$new()
query $select(c("name", "birthDate"))$where("given:exact=Peter")$orderBy("family")
query
<- client$searchByQuery(query, "Patient")
peters # equivalent: client$search("Patient", c("_elements=name,birthDate","given:exact=Peter", "_sort=family"))
#GraphQL read
$qraphQL("{id name{given,family}}", "Patient/example")
client
#GraphQL read
$qraphQL("{PatientList(name:\"pet\"){name @first @flatten{family,given @first}}}")
client
# Operations
$operation("Observation", name = "lastn") client
<- openssl::read_key("PrivateKey.pem")
privatekey
# Create your claim
<- jose::jwt_claim(iss = "ServiceURL",
claim sub = "ClientID",
aud = "TokenURL",
# expiration date as epoch (5 minutes)
exp = as.integer(as.POSIXct( Sys.time() + 300)),
# 'random' number
jti = charToRaw(as.character(runif(1, 0.5, 100000000000))))
# Sign your claim with your private key
<- jose::jwt_encode_sig(claim, privatekey)
jwt
# Define your scope(s)
<- c("system/*.read", "system/CommunicationRequest.write")
scopes
# Create a new fhirBulkClient
<- fhirBulkClient$new("FHIRBulkServerURL", tokenURL = "TokenURL")
bulkclient
# Retrieve your token
<- bulkclient$retrieveToken(jwt, scopes)
token
# Set your token
$setToken(token$access_token)
bulkclient
# Request a download for Patient Cohort 3
$groupExport(3)
bulkclient
# Request the progress of the requests
$getBulkStatus()
bulkclient
# When the downloads a available, download the bulkdata
<- bulkclient$downloadBulk(1)
patient_cohort_3
View(patient_cohort_3)