knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(stringr)
To begin, we can use gho_indicators()
to begin to explore all data available in the GHO.
library(ghost) gho_indicators()
If we want the data for AIR_10
, we could now just quickly access the data frame using gho_data()
.
gho_data("AIR_10")
From here, standard methods of data manipulation (e.g. base R, the tidyverse) could be used to select variables, filter rows, and explore the data. However, we could also provide OData queries as desired, filtering on different dimensions of the data. Let's first have a quick look at available dimensions.
gho_dimensions()
Let's say we want to filter by COUNTRY
, then we can explore explore the possible values the SpatialDim COUNTRY
dimension can take.
gho_dimension_values("COUNTRY")
If we wanted to only extract AIR_10
data on Burundi from the GHO, then we can now implement an OData query using the code we've identified above. While ghost doesn't implement complex checks on your OData queries due to their complexity, it allows you to type them with spaces and checks that each query begins with the required "$filter=..."
.
gho_data("AIR_10", "$filter=SpatialDim eq 'BDI'")
And we can get data from the GHO on multiple indicators in one call, with the output data frames already merged together.
gho_data(c("AIR_10", "AIR_11", "AIR_12"), "$filter=SpatialDim eq 'BDI'")
We can even provide different filters for each indicator separately, such as Burundi for AIR_10
, Uganda for AIR_11
, and South Africa for AIR_12
.
gho_data(c("AIR_10", "AIR_11", "AIR_12"), c("$filter=SpatialDim eq 'BDI'", "$filter=SpatialDim eq 'UGA'", "$filter=SpatialDim eq 'ZAF'"))
Of course, the reality is that it's likely easier for us to work outside the OData filtering framework and directly in R, so here's a final more complex example using dplyr and stringr alongside ghost to automatically download all indicators with the word "drug" in the indicator name (case insensitive).
library(dplyr) library(stringr) gho_indicators() %>% filter(str_detect(str_to_lower(IndicatorName), "drug")) %>% pull(IndicatorCode) %>% gho_data()
And once we have that data, we can then filter, explore, and analyze the data with our standard R workflow, or even export the downloaded data to Excel or other analytical tools for further use.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.