← Back to team overview

dhis2-devs team mailing list archive

[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 &quot;RCurl&quot; and &quot;XML&quot;, 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&lt;-&quot;https://apps.dhis2.org/dev/api/reportTables/data.csv?de=fbfJHSPpUQD&amp;de=cYeuwXTCPkU&amp;in=Uvn6LCg7dVU&amp;ou=O6uvpzGd5pu&amp;last12Months=true&quot;
+#Lets get the response and we do not need the headers
+response&lt;-getURL(url,userpwd=&quot;admin:district&quot;,httpauth = 1L, header=FALSE)
+#Unquote the data
+data&lt;-noquote(response)
+#here is the data.
+mydata&lt;-read.table(textConnection(data),sep=&quot;,&quot;,header=T)
+head(mydata)
+</screen>
+    <para>Here, we have shown how to get some aggregate data from the DHIS2 demo database using the DHIS2&apos;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&lt;-&quot;https://apps.dhis2.org/dev/api/dataElements.xml?
+paging=false&amp;links=false&quot;
+response&lt;-getURL(url,userpwd=&quot;admin:district&quot;,
+httpauth = 1L, header=FALSE)
+#We ned to parse the result
+bri&lt;-xmlParse(response)
+#And get the root
+r&lt;-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&lt;-xmlSApply(r[[&apos;dataElements&apos;]],xmlGetAttr,&quot;name&quot;)
+de_id&lt;-xmlSApply(r[[&apos;dataElements&apos;]],xmlGetAttr,&quot;id&quot;)
+#Lets bind them together
+#but we need to be careful for missing attribute values
+foo&lt;-cbind(de_names,de_id)
+#Recast this as a data frame
+data_elements&lt;-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>