Skip to content

Instantly share code, notes, and snippets.

@ericpgreen
Created November 23, 2015 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ericpgreen/bb7fcb55efd8c93d3451 to your computer and use it in GitHub Desktop.
Save ericpgreen/bb7fcb55efd8c93d3451 to your computer and use it in GitHub Desktop.
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# dhis api access
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# https://www.dhis2.org/doc/snapshot/en/developer/html/apas07.html
# https://github.com/jason-p-pickering/datim-validation
# http://www.r-fiddle.org/#/fiddle?id=wHglXleC&version=1
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# setup =======================================================================
require(RCurl)
require(XML)
require(httr)
require(rjson)
require(plyr)
pass <- "admin:district"
username <- "admin"
password <- "district"
url1 <- "https://play.dhis2.org/demo/api/"
url3 <- ".xml?paging=false&links=false"
# specify targets =============================================================
targets <- c("dataElements",
"indicators",
"dataSets",
"programIndicators",
"eventReports",
"organisationUnits")
# get metadata ================================================================
for (i in 1:length(targets)) {
url2 <- targets[i]
url <- paste0(url1, url2, url3)
response <- getURL(url,
userpwd=pass,
httpauth=1L,
header=FALSE,
ssl.verifypeer=FALSE)
# parse the result
bri <- xmlParse(response)
# get the root
r <- xmlRoot(bri)
# parse out names and IDs
name <- xmlSApply(r[[targets[i]]], xmlGetAttr, "name")
id <- xmlSApply(r[[targets[i]]], xmlGetAttr, "id")
# bind
temp <- cbind(name, id)
# recast as a data frame
df <- as.data.frame(temp,
stringsAsFactors=FALSE,
row.names=1:nrow(temp))
assign(targets[i], df)
remove(df)
}
# get the data ================================================================
# monthly periods
periods <- paste(paste0("2015",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
paste0("2014",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
paste0("2013",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
paste0("2012",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
paste0("2011",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
paste0("2010",
sprintf("%02d", seq(from=1, to=12, by=1)),
collapse=";"),
sep=";")
# specify the indicators
ind <- c("ANC 1-3 Dropout Rate")
indShort <- c("anc13drop")
# get
for (i in 1:length(ind)) {
# build the url
indID <- indicators$id[indicators$name==ind[i]]
urlA <- "https://play.dhis2.org/demo/api/analytics.json?"
urlB <- paste0("dimension=dx:", indID)
urlC <- paste0("dimension=pe:", periods)
urlD <- paste0("dimension=ou:", organisationUnits$id[1:5])
urlE <- "displayProperty=NAME&skipMeta=false"
# URLencode
url <- URLencode(paste(paste0(urlA, urlB),
urlC, urlD, urlE,
sep="&"))
r <- httr::GET(url, httr::authenticate(username,password),
httr::timeout(60))
r <- httr::content(r, "text")
# Convert the JSON to an R data structure
d <- jsonlite::fromJSON(r, flatten=TRUE)
# Get the metadata map
metadata <- as.data.frame(unlist(d$metaData$names), stringsAsFactor=F)
names(metadata)<-"name"
metadata$uid <- rownames(metadata)
# Get the base data
data <- as.data.frame(d$rows,stringsAsFactors=FALSE)
# Use the original response to get the names of the columns
names(data) <- d$headers$column
# Remap the UIDs in the response to something human readable
data$Data<-plyr::mapvalues(data$Data, metadata$uid,
as.character(metadata$name), warn_missing=FALSE)
data$ou <- plyr::mapvalues(data[,c("Organisation unit")],
metadata$uid,
as.character(metadata$name),
warn_missing=FALSE)
# Change the column types
data$Period <- as.numeric(as.character(data$Period))
data$Value <- as.numeric(as.character(data$Value))
assign(paste("df", indShort[i], sep="."), data)
remove(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment