Introduction

The R package is developed to implement the FHIR client with essential features of authentication, CRUD operations, bundle search, and data structures for the FHIR resources. The package is built based on the python library fhircient and S4 class. Tidy-style functions were developed for the clinial data manipulation more friendly.

Import the package

To get started, first we need to load the package.

library(fhiRclient)
#> Loading required package: reticulate
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

The dependent python fhirclient can be installed from the build-in method. The latest version can be installed from its github repository directly for the version of R4 support.

install_fhirclient(version = "latest")

Building connection

ct <- Client(app_id = "my_app",
             api_base = "https://r4.smarthealthit.org")
ct
#> <class 'fhirclient.client.FHIRClient'> 
#> length: 21 
#> names: app_id app_secret authorize_url ...

CRUD functions

Read a Patient record

To read a patient record and convert it to json list.

pt <- Patient(list(id = "326b4675-0bc8-4dbd-b406-a5564c282401"))
ct %>% Read(pt) %>% as_json %>% as_json_tbl
#> # A tibble: 88 x 2
#>    name                                    value                               
#>    <chr>                                   <chr>                               
#>  1 id                                      326b4675-0bc8-4dbd-b406-a5564c282401
#>  2 meta.lastUpdated                        2020-11-18T02:05:08.474-05:00       
#>  3 meta.tag.code                           synthea-5-2019                      
#>  4 meta.tag.system                         https://smarthealthit.org/tags      
#>  5 meta.versionId                          16                                  
#>  6 extension.extension.url                 ombCategory                         
#>  7 extension.extension.valueCoding.code    2106-3                              
#>  8 extension.extension.valueCoding.display White                               
#>  9 extension.extension.valueCoding.system  urn:oid:2.16.840.1.113883.6.238     
#> 10 extension.extension.url                 text                                
#> # … with 78 more rows

To read a patient and get the birth date.

ct %>% Read(pt) %>% BirthDate
#> [1] "1991-07-20"

To read a patient and get the full name.

ct %>% Read(pt) %>% getName
#> [[1]]
#> [1] "Mr. Bradly Douglas"

Create a new Patient record

pt1 <- Read(ct, pt)
pt1$id <- NULL
newpt <- ct %>% Create(pt1)
newID <- newpt$id
newID
#> [1] "774785"

Update a patient record

An example to add middle name and update the record.

pt1 <- ct %>% Read(Patient(list(id = newID)))
pt1 %>% getName
#> [[1]]
#> [1] "Mr. Bradly Douglas"
pt1$name[[1]]$given <- list("Bradly", "Middle")
update <- ct %>% Update(pt1)
ct %>% Read(pt1) %>% getName
#> [[1]]
#> [1] "Mr. Bradly Middle Douglas"

Delete a patient record

ct %>% Delete(pt1)
#> $resourceType
#> [1] "OperationOutcome"
#> 
#> $issue
#> $issue[[1]]
#> $issue[[1]]$severity
#> [1] "information"
#> 
#> $issue[[1]]$code
#> [1] "informational"
#> 
#> $issue[[1]]$diagnostics
#> [1] "Successfully deleted 1 resource(s) in 7ms"

Data Classes

Here we have two ways to create a patient model, with Patient functon or a general Model constructor.

pt <- Patient(list(id = "pid"))
pt <- Model("patient", "Patient", list(id = "pid"))

The function getModelClass can be used to query all available classes for a model. The general Model function require the resource model name and a data class to build.

getModelClass("humanname")
#> $HumanName
#> <class 'fhirclient.models.humanname.HumanName'>
hn <- Model("humanname", "HumanName", list(family = "A", given = list("B", "C")))
hn
#> <class 'fhirclient.models.humanname.HumanName'> 
#> length: 19 
#> names: as_json didResolveReference elementProperties ...
as_json(hn)
#> $family
#> [1] "A"
#> 
#> $given
#> [1] "B" "C"

Shiny Demo

SessionInfo

sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-apple-darwin19.6.0 (64-bit)
#> Running under: macOS  10.16
#> 
#> Matrix products: default
#> BLAS/LAPACK: /Users/qi28068/homebrew/Cellar/openblas/0.3.10_1/lib/libopenblasp-r0.3.10.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.0.2      fhiRclient_0.0.1 reticulate_1.18 
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.5        later_1.1.0.1     compiler_4.0.2    pillar_1.4.7     
#>  [5] tools_4.0.2       digest_0.6.27     jsonlite_1.7.1    evaluate_0.14    
#>  [9] memoise_1.1.0     lifecycle_0.2.0   tibble_3.0.4      lattice_0.20-41  
#> [13] pkgconfig_2.0.3   rlang_0.4.8       Matrix_1.2-18     cli_2.2.0        
#> [17] shiny_1.5.0       curl_4.3          yaml_2.2.1        pkgdown_1.6.1    
#> [21] xfun_0.19         fastmap_1.0.1     httr_1.4.2        stringr_1.4.0    
#> [25] knitr_1.30        desc_1.2.0        generics_0.1.0    fs_1.5.0         
#> [29] vctrs_0.3.5       systemfonts_0.3.2 rprojroot_2.0.2   grid_4.0.2       
#> [33] tidyselect_1.1.0  glue_1.4.2        R6_2.5.0          textshaping_0.2.1
#> [37] fansi_0.4.1       rmarkdown_2.5     tidyr_1.1.2       purrr_0.3.4      
#> [41] magrittr_2.0.1    promises_1.1.1    htmltools_0.5.0   ellipsis_0.3.1   
#> [45] assertthat_0.2.1  xtable_1.8-4      mime_0.9          httpuv_1.5.4     
#> [49] ragg_0.4.0        utf8_1.1.4        stringi_1.5.3     crayon_1.3.4