Getting started with dhis2r

library(dhis2r)

Connect to the DHIS2 instance using Dhis2r$new()

dhis2_play_connection <- Dhis2r$new(base_url = "https://play.dhis2.org/", 
                                    username = "admin",
                                    password = "district",
                                    api_version = "2.39.0.1",
                                    api_version_position = "before")

Get user information

dhis2_play_connection$get_user_info()
#>              name             email 
#>     "John Traore" "dummy@dhis2.org"

Get information about the account

dhis2_play_connection$account_info
#>                   created 
#> "2013-04-18T17:15:08.407"

Get access rights information about the account

dhis2_play_connection$access_rights
#>      manage externalize       write        read      update      delete 
#>        TRUE       FALSE        TRUE        TRUE        TRUE        TRUE

Get metadata

Get all resources on which you can get metadata on.

 dhis2_play_connection$get_metadata()
#> # A tibble: 87 × 4
#>    displayName              singular              plural                 href   
#>    <chr>                    <chr>                 <chr>                  <chr>  
#>  1 Program Sections         programSection        programSections        https:…
#>  2 Program Data Elements    programDataElement    programDataElements    https:…
#>  3 Organisation Unit Levels organisationUnitLevel organisationUnitLevels https:…
#>  4 Indicator Group Sets     indicatorGroupSet     indicatorGroupSets     https:…
#>  5 Map Views                mapView               mapViews               https:…
#>  6 Tracked Entity Types     trackedEntityType     trackedEntityTypes     https:…
#>  7 Relationship Types       relationshipType      relationshipTypes      https:…
#>  8 Min Max Data Elements    minMaxDataElement     minMaxDataElements     https:…
#>  9 Program Indicator Groups programIndicatorGroup programIndicatorGroups https:…
#> 10 Sections                 section               sections               https:…
#> # … with 77 more rows

Get metadata on Data Elements using the plural name dataElements.

dhis2_play_connection$get_metadata(endpoint = "dataElements")
#> # A tibble: 1,046 × 2
#>    name                                      id         
#>    <chr>                                     <chr>      
#>  1 Accute Flaccid Paralysis (Deaths < 5 yrs) FTRrcoaog83
#>  2 Acute Flaccid Paralysis (AFP) follow-up   P3jJH5Tu5VC
#>  3 Acute Flaccid Paralysis (AFP) new         FQ2o8UBlcrS
#>  4 Acute Flaccid Paralysis (AFP) referrals   M62VHgYT2n0
#>  5 Additional medication                     WO8yRIZb7nb
#>  6 Additional notes related to facility      uF1DLnZNlWe
#>  7 Admission Date                            eMyVanycQSC
#>  8 Age in years                              qrur9Dvnyt5
#>  9 Age of LLINs                              JuTpJ2Ywq5b
#> 10 Albendazole given at ANC (2nd trimester)  hCVSHjcml9g
#> # … with 1,036 more rows

The code above only returns it’s name and code/ID used on DHIS2, we shall use the code to get analytics.

Get all possible fields of a metadata of a resource

You can get more fields beyond ID and name

NOTE All resources have different fields

 dhis2_play_connection$get_metadata_fields(endpoint = "dataElements")
#>  [1] "access"            "aggregationLevels" "aggregationType"  
#>  [4] "attributeValues"   "categoryCombo"     "code"             
#>  [7] "created"           "createdBy"         "dataElementGroups"
#> [10] "dataSetElements"   "dimensionItem"     "dimensionItemType"
#> [13] "displayFormName"   "displayName"       "displayShortName" 
#> [16] "domainType"        "externalAccess"    "favorite"         
#> [19] "favorites"         "href"              "id"               
#> [22] "lastUpdated"       "legendSets"        "name"             
#> [25] "optionSetValue"    "publicAccess"      "sharing"          
#> [28] "shortName"         "translations"      "url"              
#> [31] "user"              "userAccesses"      "userGroupAccesses"
#> [34] "valueType"         "zeroIsSignificant"
 dhis2_play_connection$get_metadata_fields(endpoint = "organisationUnits")
#>  [1] "access"                 "ancestors"              "attributeValues"       
#>  [4] "children"               "code"                   "created"               
#>  [7] "dataSets"               "dimensionItem"          "dimensionItemType"     
#> [10] "displayFormName"        "displayName"            "displayShortName"      
#> [13] "externalAccess"         "favorite"               "favorites"             
#> [16] "href"                   "id"                     "lastUpdated"           
#> [19] "leaf"                   "legendSets"             "level"                 
#> [22] "name"                   "openingDate"            "organisationUnitGroups"
#> [25] "parent"                 "path"                   "programs"              
#> [28] "sharing"                "shortName"              "translations"          
#> [31] "userAccesses"           "userGroupAccesses"      "users"
 dhis2_play_connection$get_metadata_fields(endpoint = "indicators")
#>  [1] "access"                        "annualized"                   
#>  [3] "attributeValues"               "code"                         
#>  [5] "created"                       "createdBy"                    
#>  [7] "dataSets"                      "denominator"                  
#>  [9] "denominatorDescription"        "description"                  
#> [11] "dimensionItem"                 "dimensionItemType"            
#> [13] "displayDenominatorDescription" "displayDescription"           
#> [15] "displayFormName"               "displayName"                  
#> [17] "displayNumeratorDescription"   "displayShortName"             
#> [19] "externalAccess"                "favorite"                     
#> [21] "favorites"                     "href"                         
#> [23] "id"                            "indicatorGroups"              
#> [25] "indicatorType"                 "lastUpdated"                  
#> [27] "legendSets"                    "name"                         
#> [29] "numerator"                     "numeratorDescription"         
#> [31] "publicAccess"                  "sharing"                      
#> [33] "shortName"                     "translations"                 
#> [35] "url"                           "user"                         
#> [37] "userAccesses"                  "userGroupAccesses"

Getting metadata fields beyond ID and Name

dataElements

 dhis2_play_connection$get_metadata(endpoint = "dataElements", fields = c("name","id","aggregationType"))
#> # A tibble: 1,046 × 3
#>    name                                      aggregationType id         
#>    <chr>                                     <chr>           <chr>      
#>  1 Accute Flaccid Paralysis (Deaths < 5 yrs) SUM             FTRrcoaog83
#>  2 Acute Flaccid Paralysis (AFP) follow-up   SUM             P3jJH5Tu5VC
#>  3 Acute Flaccid Paralysis (AFP) new         SUM             FQ2o8UBlcrS
#>  4 Acute Flaccid Paralysis (AFP) referrals   SUM             M62VHgYT2n0
#>  5 Additional medication                     NONE            WO8yRIZb7nb
#>  6 Additional notes related to facility      SUM             uF1DLnZNlWe
#>  7 Admission Date                            AVERAGE         eMyVanycQSC
#>  8 Age in years                              AVERAGE         qrur9Dvnyt5
#>  9 Age of LLINs                              SUM             JuTpJ2Ywq5b
#> 10 Albendazole given at ANC (2nd trimester)  SUM             hCVSHjcml9g
#> # … with 1,036 more rows

organisationUnits

dhis2_play_connection$get_metadata(endpoint = "organisationUnits",
                                                        fields =  c("name","id", "level"))
#> # A tibble: 1,334 × 3
#>    name                        id          level
#>    <chr>                       <chr>       <int>
#>  1 "Adonkia CHP"               Rp268JB6Ne4     4
#>  2 "Afro Arab Clinic"          cDw53Ej8rju     4
#>  3 "Agape CHP"                 GvFqTavdpGE     4
#>  4 "Ahamadyya Mission Cl"      plnHVbJR6p4     4
#>  5 "Ahmadiyya Muslim Hospital" BV4IomHvri4     4
#>  6 "Air Port Centre, Lungi"    qjboFI0irVu     4
#>  7 "Alkalia CHP"               dWOAzMcK2Wt     4
#>  8 "Allen Town Health Post"    kbGqmM6ZWWV     4
#>  9 "Amhara Health Office "     t5C2hAOPhYF     2
#> 10 "Approved School CHP"       eoYV2p74eVz     4
#> # … with 1,324 more rows

You can use ":all" in the fields to get all fields of metadata associated with a resources

periodTypes

dhis2_play_connection$get_metadata(endpoint = "periodTypes", fields = ":all")
#> # A tibble: 18 × 4
#>    name            isoDuration isoFormat   frequencyOrder
#>    <chr>           <chr>       <chr>                <int>
#>  1 Daily           P1D         yyyyMMdd                 1
#>  2 Weekly          P7D         yyyyWn                   7
#>  3 WeeklyWednesday P7D         yyyyWedWn                7
#>  4 WeeklyThursday  P7D         yyyyThuWn                7
#>  5 WeeklySaturday  P7D         yyyySatWn                7
#>  6 WeeklySunday    P7D         yyyySunWn                7
#>  7 BiWeekly        P14D        yyyyBiWn                14
#>  8 Monthly         P1M         yyyyMM                  30
#>  9 BiMonthly       P2M         yyyyMMB                 61
#> 10 Quarterly       P3M         yyyyQn                  91
#> 11 SixMonthly      P6M         yyyySn                 182
#> 12 SixMonthlyApril P6M         yyyyAprilSn            182
#> 13 SixMonthlyNov   P6M         yyyyNovSn              182
#> 14 Yearly          P1Y         yyyy                   365
#> 15 FinancialApril  P1Y         yyyyApril              365
#> 16 FinancialJuly   P1Y         yyyyJuly               365
#> 17 FinancialOct    P1Y         yyyyOct                365
#> 18 FinancialNov    P1Y         yyyyNov                365

indicators

dhis2_play_connection$get_metadata(endpoint = "indicators")
#> # A tibble: 77 × 2
#>    name                                 id         
#>    <chr>                                <chr>      
#>  1 ANC 1-3 Dropout Rate                 ReUHfIn0pTQ
#>  2 ANC 1 Coverage                       Uvn6LCg7dVU
#>  3 ANC 2 Coverage                       OdiHJayrsKo
#>  4 ANC 3 Coverage                       sB79w2hiLp8
#>  5 ANC => 4 Coverage                    AUqdhY4mpvp
#>  6 ANC IPT 1 Coverage                   dwEq7wi6nXV
#>  7 ANC IPT 2 Coverage                   c8fABiNpT0B
#>  8 ANC LLITN coverage                   Tt5TAvdfdVK
#>  9 ANC TT2 coverage                     puykO1tbcdi
#> 10 ANC visits per clinical professional Lzg9LtG1xg3
#> # … with 67 more rows

Get analtyics

# dhis2_play_connection$get_analytics(analytic = "s46m5MS0hxu",
#                                     org_unit = c("O6uvpzGd5pu", "fdc6uOvgoji"),
#                                     period = "202101",
#                                     output_scheme = "NAME")
# dhis2_play_connection$get_analytics(analytic = "FTRrcoaog83", #Accute Flaccid Paralysis (Deaths < 5 yrs)
#                                     org_unit =   c("ImspTQPwCqd"), #Sierra Leone (National level)
#                                     period = "LAST_12_MONTHS",
#                                     output_scheme = "NAME")