# Basic Data Analysis with R (Scripts, Markdown & Dashboard)

**URL:** <https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544>\
**Category:** Showcase\
**Created:** [May 13, 2021, 11:05am UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544 "2021-05-13T11:05:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrissyhroberts](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chrissyhroberts/32/27575_2.png) [@chrissyhroberts](https://forum.getodk.org/u/chrissyhroberts)\
**Post date:** [May 13, 2021, 11:05am UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/1 "2021-05-13T11:05:00Z")

</div>

During the [@TAB](https://forum.getodk.org/groups/tab) meeting on [2021-05-12](https://docs.google.com/document/d/1KdUeiryViiPyiG8ajDC_snxSzDBBb-kwNauHvD_chfA/edit#heading=h.5yr63434neqd) we had some discussions about how to perform basic analysis in R, a free software for statistical analysis, visualisation and report generation.

[R](https://www.r-project.org/) is incredibly powerful and new users should take a look at these resources to get a more in depth view of how to get started

- [R for clinical Epidemiology](https://rpubs.com/michaelmarks/R-Clin-Epi) by @dr_michaelmarks
- [R4Epis](https://www.r4epi.com/) from the [R4Epis](https://r4epis.netlify.app/) project

You'll need to install a copy of [R](https://www.r-project.org/) and the GUI [Rstudio](https://www.rstudio.com/) in order to use the examples below.

You'll also need this toy ODK data set in CSV format.  
[data.csv](https://forum.getodk.org/uploads/short-url/3sj6zCzOL04czdOeLCnRrQRQJzz.csv) (570.1 KB)

What people want to do with their ODK data is pretty varied, but in the first instance most people will want some summaries and a couple of bar charts.

Here I provide a basic example of how to do this in R.

There are two scripts that you need to download in this zip file

[R\_Analysis\_Scripts.zip](https://forum.getodk.org/uploads/short-url/ibG0LcLhbsVJTvYTeY1YscqEj3O.zip) (2.1 KB)

The easiest way to work these is to open RStudio, then click the `file` menu and select `New Project`. Save the project in a new folder, then open that folder. Paste the data set and the scripts. you downloaded above in to this folder, then click the .Rproj file to open the project.

In the Rstudio `File` menu you can then use `open file` to load the scripts.

To run them you simply press the `source` button.

**Basic Analysis**

For most people, the `Basic.Data.Summaries.in.R` file provides a useful start point. This script will

- Open the data set
- Select some useful variables
- Save a summary table to the R project folder
- Save another summary, split by a user defined variable
- Save a barchart

Armed with a bit of basic knowledge from the training resources above and you'll be up and running with your own analysis pretty quickly.

**R Markdown**

Many users want to share a summary report to their stakeholders. R Markdown is a nice way to make reports in html or pdf format. You can also put these online as a form of basic dashboard.

The `Basic.Data.Summaries.in.Rmd` script does the same things as the basic analysis script, but makes a PDF report. You'll see that the key difference here is that you can window dress the documentation with text, show the code that was used to create the tables and charts and other things.

Simply open the .Rmd file in Rstudio and click `Knit` to create the PDF in your R project folder.

R Markdown is very versatile and can export a whole bunch of different formats.

**Dashboards**

There's a lot of ways to make interactive dashboards in R. [Some](https://shiny.rstudio.com/) are very sophisticated, but are also a bit harder to use. Perhaps the simplest way is to use R Markdown to create an html page or site.

Open the `Basic.Data.Summaries.in.Rmd` file and change line 3  
from  
`output: pdf_document`

to

`output: rmdformats::readthedown`

This will create an attractive html report that can be served from pretty much any website (I generally use [github.io](http://github.io) sites for this, see [example here](https://chrissyhroberts.github.io/csedk/phase1.html)).

Hopefully this post will provide a start point for people who have so far used excel for their analysis. Using a professional package for statistical analysis can seem daunting and does come with a pretty steep learning curve, but is worth it in the long run.

Example output files  
[Example\_Outputs.zip](https://forum.getodk.org/uploads/short-url/wMjTYSqG6A7r0gD5SSO96AU5Cgb.zip) (1.0 MB)

---

<div class="post-metadata">

**Author:** ![paul\_macharia](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/paul_macharia/32/8094_2.png) [@paul\_macharia](https://forum.getodk.org/u/paul_macharia)\
**Post date:** [May 14, 2021, 1:04pm UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/2 "2021-05-14T13:04:35Z")

</div>

Great!

Will check this out.

Paul

---

<div class="post-metadata">

**Author:** ![Florian\_May](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/florian_may/32/7758_2.png) [@Florian\_May](https://forum.getodk.org/u/Florian_May)\
**Post date:** [May 15, 2021, 7:49am UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/3 "2021-05-15T07:49:52Z")

</div>

Great resources, thanks for posting these!

To get data directly from ODK Central into R, you can use the R package [ruODK](https://docs.ropensci.org/ruODK/).

Installing R, RStudio (or any other IDE), ruODK and its dependencies can be a hassle on corpo-bricked Windows machines. [urODK](https://github.com/dbca-wa/urODK), the companion package to [ruODK](https://docs.ropensci.org/ruODK/), provides a pre-built RStudio Server at [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/dbca-wa/urODK/master?urlpath=rstudio).

ruODK provides a template RMarkdown workbook with the basic "download and parse my data" workflow:

```auto
rmarkdown::draft("my_example.Rmd", "odata", package="ruODK")

```

The workbook contains instructions to configure ODK Central credentials, to download and parse your own data, and some initial insight and visualisations as a starting point for your own analysis.

In addition to the resources posted by @chrissyhroberts, [R for applied epidemiology and public health](https://epirhandbook.com/) looks useful.

Spatial data needs some special (spatial?) steps to visualise. Follow the worked examples of the [ruODK vignette "Spatial data"](https://docs.ropensci.org/ruODK/articles/spatial.html) to go from the three ODK types (geopoint, geotrace, geoshape) to native spatial objects in R.

If you write a paper or publication using ruODK, you can find the citation for ruODK [here](https://docs.ropensci.org/ruODK/authors.html) and for ODK [in this thread](https://forum.getodk.org/t/how-to-reference-odk/3893).

---

<div class="post-metadata">

**Author:** ![Dushime](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@Dushime](https://forum.getodk.org/u/Dushime)\
**Post date:** [November 2, 2021, 7:53am UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/4 "2021-11-02T07:53:54Z")

</div>

It means if you don't know how to use R you can not have a dashboard from ODK.

---

<div class="post-metadata">

**Author:** ![chrissyhroberts](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chrissyhroberts/32/27575_2.png) [@chrissyhroberts](https://forum.getodk.org/u/chrissyhroberts)\
**Post date:** [November 2, 2021, 10:58am UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/5 "2021-11-02T10:58:41Z")

</div>

Hi @Dushime

This is just one option of many for creating dashboards.

You could also use PowerBi, HTML, Python/Panda or a very basic excel, word or PDF document on a github page.

I prefer R because it is open source, relatively easy to learn and has integrated pipelines for turning an analysis in to an html page as described above

---

<div class="post-metadata">

**Author:** ![Dushime](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@Dushime](https://forum.getodk.org/u/Dushime)\
**Post date:** [November 2, 2021, 12:00pm UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/6 "2021-11-02T12:00:10Z")

</div>

Good,  
I am using excel and the challenge that I am facing is that instead of getting labels I am getting label valued. For example. if variables sex is coded like 1=Female and 2=Male Excel displayed the data with 1 and 2 instead of displaying Female and Male.  
Do you know how to correct that in excel. Since I ama using excel

---

<div class="post-metadata">

**Author:** ![chrissyhroberts](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chrissyhroberts/32/27575_2.png) [@chrissyhroberts](https://forum.getodk.org/u/chrissyhroberts)\
**Post date:** [November 2, 2021, 12:19pm UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/7 "2021-11-02T12:19:14Z")

</div>

You should probably make that change in your ODK form. Why save data in the wrong format in the first place? My opinion is that you should always save descriptive values in your data frames, so biological sex in your example should be MALE or FEMALE.

In Excel, I think you are stuck with using find and replace, which is why you should never do analysis in Excel. Too much scope for human error. Investing in learning how to use a scripted language for data analysis will benefit you in so many ways.

This excellent open source book is a great way to get started with R. The learning curve is quite steep, but I promise you it is worth it if you are planning to spend much of your life working with data.

> **[Welcome | R for Data Science](https://r4ds.had.co.nz/)**
>
> This book will teach you how to do data science with R: You’ll learn how to get your data into R, get it into the most useful structure, transform it, visualise it and model it. In this book, you will find a practicum of skills for data science. Just...

---

<div class="post-metadata">

**Author:** ![yanokwa](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/yanokwa/32/1695_2.png) [@yanokwa](https://forum.getodk.org/u/yanokwa)\
**Post date:** [February 1, 2022, 7:21pm UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/8 "2022-02-01T19:21:06Z")

</div>

3 posts were split to a new topic: [repvisforODK: Quickly visualize your ODK data in R](https://forum.getodk.org/t/repvisforodk-quickly-visualize-your-odk-data-in-r/36564)

---

<div class="post-metadata">

**Author:** ![Thalie](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/thalie/32/15924_2.png) [@Thalie](https://forum.getodk.org/u/Thalie)\
**Post date:** [July 26, 2022, 6:41pm UTC](https://forum.getodk.org/t/basic-data-analysis-with-r-scripts-markdown-dashboard/33544/9 "2022-07-26T18:41:07Z")

</div>

@chrissyhroberts  
to complement the excellent resources you already listed, there is also the great [R for Excel Users course](https://rstudio-conf-2020.github.io/r-for-excel/) which has been developed for those who only have an Excel background and would like to incrementally incorporate R into their existing Excel workflows and ultimately move to R.
