dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #17548
[Branch ~dhis2-documenters/dhis2/dhis2-docbook-docs] Rev 527: Use of R with the Web API.
------------------------------------------------------------
revno: 527
committer: Jason P. Pickering <jason.p.pickering@xxxxxxxxx>
branch nick: dhis2-docbook-docs
timestamp: Wed 2012-05-23 16:54:01 +0200
message:
Use of R with the Web API.
modified:
src/docbkx/en/dhis2_r.xml
src/docbkx/en/dhis2_user_manual_en.xml
--
lp:~dhis2-documenters/dhis2/dhis2-docbook-docs
https://code.launchpad.net/~dhis2-documenters/dhis2/dhis2-docbook-docs
Your team DHIS 2 developers is subscribed to branch lp:~dhis2-documenters/dhis2/dhis2-docbook-docs.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-documenters/dhis2/dhis2-docbook-docs/+edit-subscription
=== modified file 'src/docbkx/en/dhis2_r.xml'
--- src/docbkx/en/dhis2_r.xml 2012-05-21 13:22:42 +0000
+++ src/docbkx/en/dhis2_r.xml 2012-05-23 14:54:01 +0000
@@ -439,4 +439,44 @@
</screenshot>
<para>In this simple example, we have shown how to use PL/R with the DHIS2 database and web interface to display some summary statistics using R to perform the calculations. </para>
</section>
+ <section id="dhis2_r_web_api">
+ <title>Using this DHIS2 Web API with R</title>
+ <para>DHIS2 has a powerful Web API which can be used to integrate applications together. In this section, we will illustrate a few trivial examples of the use of the Web API, and how we can retrieve data and metada for use in R. The Web API uses basic HTTP authentication (as described in the Web API section of this document). Using two R packages "RCurl" and "XML", we will be able to work with the output of the API in R. In the first example, we will get some metadata from the database. </para>
+ <screen>#We are going to need these two libraries
+require(RCurl)
+require(XML)
+#Just a URL with some data
+url<-"https://apps.dhis2.org/dev/api/reportTables/data.csv?de=fbfJHSPpUQD&de=cYeuwXTCPkU&in=Uvn6LCg7dVU&ou=O6uvpzGd5pu&last12Months=true"
+#Lets get the response and we do not need the headers
+response<-getURL(url,userpwd="admin:district",httpauth = 1L, header=FALSE)
+#Unquote the data
+data<-noquote(response)
+#here is the data.
+mydata<-read.table(textConnection(data),sep=",",header=T)
+head(mydata)
+</screen>
+ <para>Here, we have shown how to get some aggregate data from the DHIS2 demo database using the DHIS2's Web API.</para>
+ <para>In the next code example, we will retrieve some metadata, namely a list of data elements and their unique identifiers.</para>
+ <screen>#Get the list of data elements. Turn off paging and links
+url<-"https://apps.dhis2.org/dev/api/dataElements.xml?
+paging=false&links=false"
+response<-getURL(url,userpwd="admin:district",
+httpauth = 1L, header=FALSE)
+#We ned to parse the result
+bri<-xmlParse(response)
+#And get the root
+r<-xmlRoot(bri)
+#Parse out what we need explicitly, in this case from the first node
+#Just get the names and ids as separate arrays
+de_names<-xmlSApply(r[['dataElements']],xmlGetAttr,"name")
+de_id<-xmlSApply(r[['dataElements']],xmlGetAttr,"id")
+#Lets bind them together
+#but we need to be careful for missing attribute values
+foo<-cbind(de_names,de_id)
+#Recast this as a data frame
+data_elements<-as.data.frame(foo,stringsAsFactors=FALSE,row.names=1:nrow(foo))
+head(data_elements)
+</screen>
+ <para>Note that the values which we are interested in are stored as XML attributes and were parsed into two seperate matrices and then combined together into a single data frame. </para>
+ </section>
</chapter>
=== modified file 'src/docbkx/en/dhis2_user_manual_en.xml'
--- src/docbkx/en/dhis2_user_manual_en.xml 2012-05-21 13:22:42 +0000
+++ src/docbkx/en/dhis2_user_manual_en.xml 2012-05-23 14:54:01 +0000
@@ -61,6 +61,7 @@
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="dhis2_r.xml" encoding="UTF-8" xpointer="dhis2_r_maps"/>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="dhis2_r.xml" encoding="UTF-8" xpointer="dhis2_r_google_vis"/>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="dhis2_r.xml" encoding="UTF-8" xpointer="dhis2_r_plr"/>
+ <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="dhis2_r.xml" encoding="UTF-8" xpointer="dhis2_r_web_api"/>
</appendix>
<appendix>
<title>DHIS 2 Workbook</title>