-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_2.R
62 lines (51 loc) · 1.6 KB
/
main_2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Title : main.R
# Objective : fhirton fhirckrar comparison
# Created by: tpeschel
# Created on: 19.07.20
#devtools::install_github("POLAR-fhir/fhircrackr", quiet = T)
install.packages( "fhircrackr" )
library('dplyr')
library('fhircrackr')
# fhir endpoint
endp <- "https://hapi.fhir.org/baseR4/"
# fhir request
req <- paste0(
"Observation?",
"code=http://loinc.org|85354-9&",
"_include=Observation:patient&",
"_include=Observation:encounter&",
"_format=xml&_pretty=true&_count=500" )
#req <- "Observation?_include=Observation:patient&_include=Observation:encounter&_format=xml&_pretty=true&_count=500"
#req <- "Observation?_include=Observation:patient&_include=Observation:encounter&_format=xml&_pretty=true&_count=500"
fsq <- paste_paths(endp, req)
# designs 3, 2, 3
design <- list(
"Observations" = list(
".//Observation"
),
"Encounters" = list(
".//Encounter",
".//*"
),
"Patients" = list(
".//Patient"
)
)
# download fhir bundles and babble a lot
bundles <- fhir_search(fsq, max_bundles = 3, verbose=2)
# crack/flatten the downloaded bundles to tables via designs
# use space as separator and [] as brackets for indexing and babble a lot
tables <- fhir_crack(bundles, design, sep = " ", brackets = c('[', ']'), verbose = 2)
# sort column names
tables <- lapply(
tables,
function(tbl) {
tbl[, sort(names(tbl))]
}
)
# create dir if not exist
if (! dir.exists('csv2'))
dir.create( 'csv2', recursive = T )
# write tables as csv files
for (n in names(tables))
write.table(tables[[n]], paste0("csv2/", n, "_r.csv" ), na = "", sep = ";", dec = ".", row.names = F, quote = F )