Skip to content

Instantly share code, notes, and snippets.

@dirkschumacher dirkschumacher/rhxl-hdx.R
Last active Apr 25, 2018

Embed
What would you like to do?
# this is just a script to test the rhxl package, I just quickly looked at the data
# Ethiopia Who is doing What Where - 3W December 2017
# source: https://data.humdata.org/dataset/3w-december-2017
url <- "https://data.humdata.org/dataset/615416d2-457b-461a-8155-090f0ced0bf8/resource/f71bf111-8706-42f4-ba46-4ce3c8c949dc/download/3w_hxl.xlsx"
# load the rhxl package
# https://github.com/dirkschumacher/rhxl
library(rhxl)
download.file(url, "file.xlsx")
data <- as_hxl(readxl::read_xlsx("file.xlsx"))
# get the schema
hxl_schema(data)
# plot ongoing activities
library(ggplot2)
library(dplyr)
plot_data <- hxl_select(data, c("#org+impl", "#status", "#adm1+name"))
# set predictable column names
colnames(plot_data) <- c("org", "status", "admin1")
plot_data <- plot_data %>%
group_by(admin1, status) %>%
summarise(count_org = n())
ggplot(plot_data) +
aes(x = status, y = count_org) +
geom_bar(stat = "identity") +
facet_wrap(~admin1) +
theme(axis.text.x = element_text(angle = 90)) +
ggtitle("Project overview by admin1 - Ethiopia Who is doing What Where - 3W December 2017 ") +
ylab("#Projects") + xlab("Project status")
@dickoa

This comment has been minimized.

Copy link

commented Apr 20, 2018

That's really awesome and we can plug in into the new rhdx R package (work in progress).

library(rhdx) ## devtools::install_git("https://gitlab.com/dickoa/rhdx")
library(rhxl)
library(readxl)
library(dplyr)

name <- "3w-december-2017"
set_rhdx_config(hdx_site = "prod")

## Check the config
get_rhdx_config()
## <HDX Configuration> 
##   HDX site: prod
##   HDX site url: https://data.humdata.org/
##   HDX API key: 


data <- read_dataset("3w-december-2017") %>%
  get_resources() %>%
  first() %>% ## get the first resource
  read_session(comment = "") %>%
  as_hxl()

hxl_schema(data)
## # A tibble: 9 x 3
##   tag    attribute column_idx
##   <chr>  <chr>          <int>
## 1 org    NA                 1
## 2 org    type               2
## 3 adm1   name               3
## 4 adm2   name               4
## 5 adm3   name               5
## 6 adm3   code               6
## 7 sector NA                 7
## 8 status NA                 8
## 9 org    impl               9
@dirkschumacher

This comment has been minimized.

Copy link
Owner Author

commented Apr 21, 2018

Uhh nice. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.