knitr::opts_chunk$set( collapse = TRUE, comment = "#>", echo = TRUE, warning = FALSE, message = FALSE, fig.path = "man/figures/README-", error = TRUE, out.width = "100%" )
whobcnapp
is an R package containing utilities for both (a) transforming data per the specifications of the "Data Assessment Report" and and (b) visualizing that data in the form of a production-ready shiny web application.
You can install the most recent version of whobcnapp
from GitHub with the following:
# install.packages("devtools") devtools::install_github('databrew/whobcnapp')
Alternatively, one can build the package locally by first cloning the https://github.com/databrew/whobcnapp repository, then running the build_package.R
script.
The functions for "cleaning" data are customized for the formats currently used by the office. In general terms, the work flow is as follows:
For further details on the rationale and formats of these transformations, see the "UHC Policy Watch Platform Design: DATA ASSESSMENT REPORT".
One starts by preparing the workspace with the necessary packages:
library(tidyr) library(dplyr) library(lubridate) library(readxl) library(whobcnapp)
Next, one must define the input file to be transformed. For the purpose of this example, we'll use BUL_Appendix_tables.xlsx
which is available at https://github.com/databrew/whobcnapp/blob/master/data-raw/BUL_Appendix_tables.xlsx.
input_file <- 'data-raw/BUL_Appendix_tables.xlsx'
Having defined the path to the data, one uses the function conformity_check
to ensure that the data to be read conforms to the requirements of the ETL process:
ok <- conformity_check(input_file)
If the file's format is correct, the function will return a boolean TRUE
, and print a message saying:
The file conforms. OK to go on.
Otherwise, the function will return the nature of the non-conformity. For example:
conformity_check('xyz_tables.xlsx')
Error in conformity_check("xyz_tables.xlsx") : The file should be named "ISO_Appendix_tables.xlsx", where "ISO" is the 3 letter ISO code of the country in question.
Having confirmed that the file to be read is of the correct format, one can use the read_data
function to load each sheet of the Excel workbook into memory:
data <- read_data(input_file)
The result of the read_data
function is a list of dataframes, in which the name of each element in the list reflects the figure for which the data corresponds.
cat(paste0(names(data), collapse = '\n'))
Data is structured uniformly into a "long" format, following the principles of "tidy data". All dataframe share the following columns:
Whereas some dataframes have additional columns such as:
Below are the column names and first 3 rows of each dataset as an example:
for(i in 1:length(data)){ print(names(data)[i]) print(head(data[[i]], 3)) }
Having transformed data to a tidy, machine-readable format, standardized visualizations can be constructed. In the whobcnapp
library, this functionality is indicated by functions with the plot_
prefix. These functions take a data
object (as generated by read_data
) and return a ggplot2()
object.
First, we'll transform data so as to prepare it for plotting (covered in previous section).
# Define the path to the input file input_file <- 'data-raw/BUL_Appendix_tables.xlsx' # Confirm that the data is correctly formatted ok <- conformity_check(input_file) # Transform the data to a tidy/long format data <- read_data(input_file)
Now, we can pass the data
object to the plot_...
function as such:
plot_fig1(data = data)
plot_fig2(data = data)
plot_fig3(data = data)
plot_fig4(data = data)
plot_fig5(data = data)
plot_t2(data = data)
In a development environment, an engineer will likely carry out the following work flow:
https://github.com/databrew/whobcnapp
cd
into it cd whobcnapp
R/app.R
(for the shiny application itself) or in R/...
(for upstream function code) cd
into dev
and run Rscript run_dev.R
to quickly re-build and serve the application locally.To run the web application (under construction), reproduce the following code in an R session:
library(whobcnapp) whobcnapp::run_app()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.