The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Getting Started

No token available. Code chunks will not be evaluated.

The khisr R package simplifies interaction with the District Health Information System 2 (DHIS2) platform. Designed for researchers and public health professionals, khisr streamlines data retrieval and analysis, saving you valuable time compared to manual methods.

Authentication

khisr prioritizes security by operating in authenticated mode by default. This ensures you interact with DHIS2 as a recognized user. To begin exploring DHIS2 data, you’ll need to establish your credentials.

Setting Your Credentials:

  1. Obtain Credentials: Secure your DHIS2 username and password through appropriate channels within the DHIS2 organization.

  2. Store Credentials Securely: khisr offers a convenient way to store your credentials within your R environment. Refer to the comprehensive guide, Set Your Credentials, for detailed instructions on setting and managing credentials effectively.

# Set the credentials using username and password
khis_cred(username = 'your-dhis2-username', password = 'your-dhis2-password', server = 'https://<your dhis2 instance>')

# Set the credentials using a Personal Access Token (DHIS2's recommended
# method for scripts and integrations) instead of username/password
khis_cred(token = 'your-dhis2-token', server = 'https://<your dhis2 instance>')

# Set credentials using configuration path
khis_cred(config_path = 'path/to/secret.json')

Note: Replace placeholders like ‘your-dhis2-username’ and ‘path/to/your/secret.json’ with your actual credentials and file path.

Metadata

DHIS2 utilizes metadata to define the structure and meaning of its data. Explore the data dimensions resource for a deeper understanding.

Metadata helpers in khisr

khisr provides a set of high-level functions — one per DHIS2 metadata type (get_organisation_units(), get_data_elements(), get_programs(), and around 25 others) — that all share the same interface and can be filtered the same way. See ?metadata-helpers for the full list, and your R IDE’s auto-complete for faster typing.

Metadata object filter

khisr filters retrieved metadata using DHIS2’s property:operator:value pattern, exposed through metadata_filter() and a matching set of infix operators (%.eq%, %.like%, %.in%, and about 20 more — see ?metadata_filter for the complete list with descriptions).

Working with metadata filters

Basic usage of the metadata filter

# Retrieve organisation units by province (level 2)
province <- get_organisation_units(level %.eq% '2')
province

# Retrieve province by name (Vientiane Capital)
province <- get_organisation_units(level %.eq% '2',
                                   name %.like% 'vientiane capital')
province

data_element_id <- c('lYsfXxCw6Qi', 'GxlrIgMyEf4')

# Retrieve data elements by ID using operator in
data_elements <- get_data_elements(id %.in% data_element_id)
data_elements

# Retrieve data elements by filtering using dataElementGroups
data_elements <- get_data_elements(dataElementGroups.name %.like% 'malaria')
data_elements

Data analytics

The analytics resource in DHIS2 empowers you to access and analyze aggregated data across various dimensions. To effectively leverage this resource, let’s explore the key functions and parameters involved:

Key Functions

Dimension (dx)

The dimension query parameter defines which dimensions should be included in the analytics query. Any number of dimensions can be specified. The dimension parameter should be repeated for each dimension to include in the query response. The query response can potentially contain aggregated values for all combinations of the specified dimension items. The fixed dimensions are the data element (dx) period (time) (pe) and organisation unit (ou) dimension. You can dynamically add dimensions through categories, data element group sets and organisation unit group sets.

Dimension ID Dimensions
dx Data elements, indicators, data set reporting rate metrics, data element operands, program indicators, program data elements, program attributes, validation rules
pe ISO periods and relative periods (see Date and Period Format)
ou Organisation unit hierarchy: organisation unit identifiers, or keywords USER_ORGUNIT, USER_ORGUNIT_CHILDREN, USER_ORGUNIT_GRANDCHILDREN, LEVEL-<level>, and OU_GROUP-<group-id>
co Category option combo identifiers (use all to get all items)
ao Attribute option combo identifiers (use all to get all items)

Filter (filter)

The filter parameter defines which dimensions should be used as filters for the data retrieved in the analytics query. Any number of filters can be specified. The filter parameter should be repeated for each filter to use in the query. A filter differs from a dimension in that the filter dimensions will not be part of the query response content, and that the aggregated values in the response will be collapsed on the filter dimensions. In other words, the data in the response will be aggregated on the filter dimensions, but the filters will not be included as dimensions in the actual response.

Constructing Queries

# To include a list dimensions for data elements id, dataset ids
dx %.d% c('dimension-id-1', 'dimension-id-2')

pe %.d% 'LAST_YEAR'

ou %.d% 'USER_ORGUNIT'

# showing in the analytics
get_analytics(
    dx %.d% c('lYsfXxCw6Qi', 'vTRrNdOOT9g', 'GxlrIgMyEf4'),
    pe %.d% 'LAST_YEAR',
    ou %.d% c('W6sNfkJcXGC')
)

# Using the startDate and endDate with organisation unit keyword 'USER_ORGUNIT'
get_analytics(
    dx %.d% c('lYsfXxCw6Qi', 'vTRrNdOOT9g', 'GxlrIgMyEf4'),
    ou %.d% 'USER_ORGUNIT',
    pe %.d% 'all',
    startDate = '2023-07-01',
    endDate = '2023-12-31'
)
# Filter by period
pe %.f% 'LAST_YEAR'

# Filter by organisation unit
ou %.f% 'USER_ORGUNIT'

# showing in the analytics. filter by organisation unit with id 'W6sNfkJcXGC'
# and period 'LAST_YEAR'
get_analytics(
    dx %.d% c('lYsfXxCw6Qi', 'vTRrNdOOT9g', 'GxlrIgMyEf4'),
    pe %.f% 'LAST_YEAR',
    ou %.f% 'W6sNfkJcXGC'
)

Data quality

Alongside the aggregated values themselves, DHIS2 exposes a few endpoints for checking the quality of reported data:

khisr function Retrieves
get_complete_data_set_registrations() Raw completeness records — who marked a data set complete, and when.
get_analytics_outliers() Data values flagged as statistical outliers.
get_validation_results() Violated validation rules for an org unit/period range.
get_data_value_audits() Change history for a data value.
# Completeness registrations for a data set at a province and everything
# below it, for a single period
get_complete_data_set_registrations(
    data_sets = 'VEM58nY22sO',
    org_units = 'W6sNfkJcXGC',
    children = TRUE,
    periods = '202301'
)

get_analytics_outliers() and get_validation_results() require the authenticated user to have the corresponding DHIS2 authority (outlier detection or validation analysis); without it, DHIS2 returns an authorisation error rather than empty results.

Tracker data

Alongside aggregate analytics, DHIS2 also stores case-based, person-level data through its Tracker API. khisr provides get_tracked_entities(), get_enrollments(), and get_events() for reading it, plus tracked_entity_filter() for filtering tracked entities by attribute value. See Tracker Data for a full guide.

# Tracked entities enrolled in a program, at an org unit and everything below it
get_tracked_entities(
    program = 'PREnRHSp3be',
    org_units = 'IWp9dQGM0bS',
    org_unit_mode = 'DESCENDANTS'
)

System & utilities

A handful of functions cover the DHIS2 instance itself, rather than its health data:

khisr function Retrieves
get_system_info() DHIS2 version, build, and server info.
get_geo_features() Organisation unit coordinates/boundaries, for mapping.
get_sql_views()/get_sql_view_data() Predefined SQL views, and their data.
get_data_store_namespaces()/get_data_store_keys()/get_data_store_value() The system or user key/value data store.
get_file_resources() Metadata (not contents) of files stored in the instance.
get_system_info()$version

# Coordinates/boundaries for every province (level 2)
get_geo_features(org_units = 'LEVEL-2')

# Reading the key/value data store
namespaces <- get_data_store_namespaces()
namespaces
get_data_store_keys(namespaces[1])

get_sql_view_data() requires the authenticated user to be authorised to read the specific SQL view; DHIS2 returns an error rather than empty results if not.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.