Learn R Programming

fhircrackr (version 0.2.1)

fhir_crack: Flatten list of FHIR bundles

Description

Converts all FHIR bundles (the result of fhir_search) to a list of data frames.

Usage

fhir_crack(
  bundles,
  design,
  sep = NULL,
  remove_empty_columns = NULL,
  brackets = NULL,
  verbose = 2,
  data.table = FALSE,
  add_indices
)

Arguments

bundles

A FHIR search result as returned by fhir_search.

design

A named list of data frame descriptions. Each data frame description will produce one data frame in the list of data frames returned by fhir_crack, where the data frame has the same name as the data frame description in design.

Each data frame description is a list of 3 named elements:

1) design$resource: Mandatory. A string with an XPath expression locating the entries for this data frame in a FHIR bundle page. This is usually the path to a resource tpye such as "//Patient" or "//Observation".

2) design$cols: Optional. Either a string containing an XPath expression referencing a certain level of attributes that should be extracted ( "./@value" e.g. would extract all values on the root level) or a named list where the elements are XPath expressions indicating the specific position of attributes to extract and the names of the list elements are the column names of the resulting data frame. If design$cols is NULL, all available attributes will be extracted.

3) design$style: Optional. This can be used instead of the function arguments sep, brackets and remove_empty_columns, but will be overwritten if the corresponding function arguments are not NULL.

A named list with the following optional elements:

a) design$style$sep : A string to separate pasted multiple entries.

b) design$style$brackets: A character vector of length two defining the brackets surrounding indices for multiple entries, e.g. c( "<", ">"). If NULL, no indices will be added to multiple entries.

c) design$style$rm_empty_cols: Logical scalar. Remove empty columns?

For a more detailed explanation and comprehensive examples of design, please see the package vignette.

sep

A string to separate pasted multiple entries. NULL means sep is looked up in design, if it is NULL there too, sep will be set to " " as the default.

remove_empty_columns

Logical scalar. Remove empty columns? NULL means remove_empty_columns is looked up in design, if it is NULL there too, remove_empty_columns will be set to TRUE as the default.

brackets

A character vector of length two defining the brackets surrounding indices for multiple entries, e.g. c( "<", ">"). If NULL, no indices will be added to multiple entries. NULL means brackets is looked up in design, if it is NULL there too, no indices are added.

verbose

An Integer Scalar. If 0, nothing is printed, if 1, only finishing message is printed, if > 1, extraction progress will be printed. Defaults to 2.

data.table

Logical scalar. Should tables be returned in data.table format instead of data.frame? defaults to FALSE.

add_indices

Deprecated. This argument was used to control adding of indices for multiple entries. This is now done via the brackets argument. If brackets is NULL, no indices are added, if brackets is not NULL, indices are added to multiple entries.

Value

A list of data frames (if data.table = FALSE) or a list of data.tables if data.table = TRUE.

Examples

Run this code
# NOT RUN {
#unserialize example bundle
bundles <- fhir_unserialize(medication_bundles)

#define attributes to extract
design <- list(

 #define specifically which elements to extract
	MedicationStatement = list(

		resource = ".//MedicationStatement",

		cols = list(
				MS.ID              = "id",
				STATUS.TEXT        = "text/status",
				STATUS             = "status",
				MEDICATION.SYSTEM  = "medicationCodeableConcept/coding/system",
				MEDICATION.CODE    = "medicationCodeableConcept/coding/code",
				MEDICATION.DISPLAY = "medicationCodeableConcept/coding/display",
				DOSAGE             = "dosage/text",
				PATIENT            = "subject/reference",
				LAST.UPDATE        = "meta/lastUpdated"
		),

		style = list(
				sep = " ",
				brackets = c("[", "]"),
				rm_empty_cols= FALSE
				)
	),

 #extract all values
	Patients = list(

		resource = ".//Patient"
	)
)

#convert fhir to data frames
list_of_tables <- fhir_crack(bundles, design)

#check results
head(list_of_tables$MedicationStatement)
head(list_of_tables$Patients)


# }

Run the code above in your browser using DataLab