Skip to contents

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")

Ways of keeping your user credentials safe

Get user information

dhis2_play_connection$get_user_info()
#> [1] "https://play.dhis2.org/2.39.4.1/api/me?paging=false"
#>              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()
#> [1] "https://play.dhis2.org/2.39.4.1/api/resources?paging=false"
#> # A tibble: 87 × 4
#>    displayName                             singular                 plural href 
#>    <chr>                                   <chr>                    <chr>  <chr>
#>  1 Data Element Group Sets                 dataElementGroupSet      dataE… http…
#>  2 Options                                 option                   optio… http…
#>  3 Category Combos                         categoryCombo            categ… http…
#>  4 Push Analysis                           pushAnalysis             pushA… http…
#>  5 Programs                                program                  progr… http…
#>  6 Event Filters                           eventFilter              event… http…
#>  7 Sections                                section                  secti… http…
#>  8 Program Sections                        programSection           progr… http…
#>  9 Program Tracked Entity Attribute Groups programTrackedEntityAtt… progr… http…
#> 10 Proposals                               proposal                 propo… http…
#> # ℹ 77 more rows

Get metadata on Data Elements using the plural name dataElements.

dhis2_play_connection$get_metadata(endpoint = "dataElements")
#> [1] "https://play.dhis2.org/2.39.4.1/api/dataElements?paging=false&fields=name%2Cid"
#> # A tibble: 1,038 × 2
#>    name                                               id         
#>    <chr>                                              <chr>      
#>  1 # of AIDS patients_DO                              OKhG8WEnV2M
#>  2 ANC 1st visit                                      fbfJHSPpUQD
#>  3 ANC 2nd visit                                      cYeuwXTCPkU
#>  4 ANC 3rd visit                                      Jtf34kNZhzP
#>  5 ANC 4th or more visits                             hfdmMSPBgLG
#>  6 ARI treated with antibiotics (pneumonia) follow-up FHD3wiSM7Sn
#>  7 ARI treated with antibiotics (pneumonia) new       iKGjnOOaPlE
#>  8 ARI treated with antibiotics (pneumonia) referrals XTqOHygxDj5
#>  9 ARI treated without antibiotics (cough) follow-up  RF4VFVGdFRO
#> 10 ARI treated without antibiotics (cough) new        Cm4XUw6VAxv
#> # ℹ 1,028 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] "https://play.dhis2.org/2.39.4.1/api/dataElements?fields=%3Aall&paging=true&pageSize=1"
#>  [1] "access"            "aggregationLevels" "aggregationType"  
#>  [4] "attributeValues"   "categoryCombo"     "created"          
#>  [7] "createdBy"         "dataElementGroups" "dataSetElements"  
#> [10] "dimensionItem"     "dimensionItemType" "displayFormName"  
#> [13] "displayName"       "displayShortName"  "domainType"       
#> [16] "externalAccess"    "favorite"          "favorites"        
#> [19] "href"              "id"                "lastUpdated"      
#> [22] "lastUpdatedBy"     "legendSets"        "name"             
#> [25] "optionSetValue"    "publicAccess"      "sharing"          
#> [28] "shortName"         "translations"      "user"             
#> [31] "userAccesses"      "userGroupAccesses" "valueType"        
#> [34] "zeroIsSignificant"
 dhis2_play_connection$get_metadata_fields(endpoint = "organisationUnits")
#> [1] "https://play.dhis2.org/2.39.4.1/api/organisationUnits?fields=%3Aall&paging=true&pageSize=1"
#>  [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] "https://play.dhis2.org/2.39.4.1/api/indicators?fields=%3Aall&paging=true&pageSize=1"
#>  [1] "access"                             "aggregateExportCategoryOptionCombo"
#>  [3] "annualized"                         "attributeValues"                   
#>  [5] "code"                               "created"                           
#>  [7] "createdBy"                          "dataSets"                          
#>  [9] "denominator"                        "denominatorDescription"            
#> [11] "description"                        "dimensionItem"                     
#> [13] "dimensionItemType"                  "displayDenominatorDescription"     
#> [15] "displayDescription"                 "displayFormName"                   
#> [17] "displayName"                        "displayNumeratorDescription"       
#> [19] "displayShortName"                   "externalAccess"                    
#> [21] "favorite"                           "favorites"                         
#> [23] "href"                               "id"                                
#> [25] "indicatorGroups"                    "indicatorType"                     
#> [27] "lastUpdated"                        "legendSet"                         
#> [29] "legendSets"                         "name"                              
#> [31] "numerator"                          "numeratorDescription"              
#> [33] "publicAccess"                       "sharing"                           
#> [35] "shortName"                          "translations"                      
#> [37] "user"                               "userAccesses"                      
#> [39] "userGroupAccesses"

Getting metadata fields beyond ID and Name

dataElements

 dhis2_play_connection$get_metadata(endpoint = "dataElements", fields = c("name","id","aggregationType"))
#> [1] "https://play.dhis2.org/2.39.4.1/api/dataElements?paging=false&fields=name%2Cid%2CaggregationType"
#> # A tibble: 1,038 × 3
#>    name                                               aggregationType id        
#>    <chr>                                              <chr>           <chr>     
#>  1 # of AIDS patients_DO                              SUM             OKhG8WEnV…
#>  2 ANC 1st visit                                      SUM             fbfJHSPpU…
#>  3 ANC 2nd visit                                      SUM             cYeuwXTCP…
#>  4 ANC 3rd visit                                      SUM             Jtf34kNZh…
#>  5 ANC 4th or more visits                             SUM             hfdmMSPBg…
#>  6 ARI treated with antibiotics (pneumonia) follow-up SUM             FHD3wiSM7…
#>  7 ARI treated with antibiotics (pneumonia) new       SUM             iKGjnOOaP…
#>  8 ARI treated with antibiotics (pneumonia) referrals SUM             XTqOHygxD…
#>  9 ARI treated without antibiotics (cough) follow-up  SUM             RF4VFVGdF…
#> 10 ARI treated without antibiotics (cough) new        SUM             Cm4XUw6VA…
#> # ℹ 1,028 more rows

organisationUnits

dhis2_play_connection$get_metadata(endpoint = "organisationUnits",
                                                        fields =  c("name","id", "level"))
#> [1] "https://play.dhis2.org/2.39.4.1/api/organisationUnits?paging=false&fields=name%2Cid%2Clevel"
#> # A tibble: 1,332 × 3
#>    name                        id          level
#>    <chr>                       <chr>       <int>
#>  1 " Panderu MCHP"             ueuQlqb8ccl     4
#>  2 "Adonkia CHP"               Rp268JB6Ne4     4
#>  3 "Afro Arab Clinic"          cDw53Ej8rju     4
#>  4 "Agape CHP"                 GvFqTavdpGE     4
#>  5 "Ahamadyya Mission Cl"      plnHVbJR6p4     4
#>  6 "Ahmadiyya Muslim Hospital" BV4IomHvri4     4
#>  7 "Air Port Centre, Lungi"    qjboFI0irVu     4
#>  8 "Alkalia CHP"               dWOAzMcK2Wt     4
#>  9 "Allen Town Health Post"    kbGqmM6ZWWV     4
#> 10 "Approved School CHP"       eoYV2p74eVz     4
#> # ℹ 1,322 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")
#> [1] "https://play.dhis2.org/2.39.4.1/api/periodTypes?paging=false&fields=%3Aall"
#> # A tibble: 19 × 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 QuarterlyNov    P3M         yyyyNovQn               91
#> 12 SixMonthly      P6M         yyyySn                 182
#> 13 SixMonthlyApril P6M         yyyyAprilSn            182
#> 14 SixMonthlyNov   P6M         yyyyNovSn              182
#> 15 Yearly          P1Y         yyyy                   365
#> 16 FinancialApril  P1Y         yyyyApril              365
#> 17 FinancialJuly   P1Y         yyyyJuly               365
#> 18 FinancialOct    P1Y         yyyyOct                365
#> 19 FinancialNov    P1Y         yyyyNov                365

indicators

dhis2_play_connection$get_metadata(endpoint = "indicators")
#> [1] "https://play.dhis2.org/2.39.4.1/api/indicators?paging=false&fields=name%2Cid"
#> # A tibble: 77 × 2
#>    name                                 id         
#>    <chr>                                <chr>      
#>  1 ANC 1 Coverage                       Uvn6LCg7dVU
#>  2 ANC 1-3 Dropout Rate                 ReUHfIn0pTQ
#>  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
#> # ℹ 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")