-
Notifications
You must be signed in to change notification settings - Fork 0
/
extraord_report.Rmd
160 lines (134 loc) · 5.31 KB
/
extraord_report.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
title: Americas Extraordinary Report
subtitle: "`r format(Sys.Date(), '%d %B %Y')`"
date: Regional Bureau for the Americas
output:
pagedown::html_paged:
css: ["unhcr-generic-fonts.css", "unhcr-generic-page.css", "unhcr-generic.css"]
front_cover: cover_grey.jpg
back_cover: unhcr-backcover.svg
toc: true
toc_depth: 2
number_sections: false
toc-title: Contents
knit: pagedown::chrome_print
---
\newpage
```{r setup, include=FALSE}
library(tidyverse)
library(lubridate)
library(httr)
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
```
```{r data}
data <-
GET("https://kobo.unhcr.org/api/v2/assets/aLWoR3ZMFogWzChxaX28cP/data.json",
add_headers(Authorization = glue::glue("Token {Sys.getenv('KOBO_API_KEY')}"))) %>%
content(as = "text") %>%
jsonlite::fromJSON() %>%
pluck("results") %>%
as_tibble()
data <-
data %>%
filter(`grRepOp/exReport`=="yes") %>% # keep only extraordinary reports
select(id = `_id`,
year = `_submission_time`,
week = `grRepOp/week`,
country = `grRepOp/country`,
context.narrative = `grContext/conDesGr/context`,
context.confidential = `grContext/conDesGr/confContext`,
poptrends.narrative = `grPopulationTrends/popDesGr/popTrends`,
poptrends.confidential = `grPopulationTrends/popDesGr/confPop`,
protection.narrative = `grProtectionConcerns/protecDesGr/protCon`,
protection.confidential = `grProtectionConcerns/protecDesGr/confProt`,
operations.narrative = `grOperation/opeDesGr/operInfo`,
operations.confidential = `grOperation/opeDesGr/confOperation`,
interagency.narrative = `grInteragencial/interDesGr/InteragencyInfo`,
interagency.confidential = `grInteragencial/interDesGr/confInter`,
aob.narrative = `grAoB/aobDesGr/AoBInfo`,
aob.confidential = `grAoB/aobDesGr/confAoB`)
data <-
data %>%
mutate(date = date(year),
hour = hms::as_hms(ymd_hms(year)),
year = year(year),
week = parse_number(week)) %>%
pivot_longer(-c(id, date, hour, year, week, country), names_pattern = "(.+)\\.(.+)", names_to = c("section", "dimension")) %>%
pivot_wider(names_from = "dimension", values_from = "value")
data <-
data %>%
mutate(country = case_when(country == "CostaRica" ~ "Costa Rica",
country == "DominicanRepublic" ~ "Dominican Rep.",
country == "NorthernCaribbean" ~ "Northern Caribbean",
country == "TrinidadTobago" ~ "Trinidad and Tobago",
country == "UnitedStates" ~ "United States of America",
country == "Bolivia" ~ "Bolivia (Plurinational State of)",
country == "Curaçao" ~ "Curacao ",
country == "Venezuela" ~ "Venezuela (Bolivarian Republic of)",
is.na(country) ~ "The Americas",
TRUE ~ country),
section = c(aob = "A.O.B",
context = "Context",
interagency = "Inter-Agency",
operations = "Operations",
poptrends = "Population Trends",
protection = "Protection Concerns")[section],
narrative = replace_na(narrative, "Nothing to report."))
data <-
data %>%
left_join(popdata::pd_countries %>% select(country = name, region)) %>%
mutate(region = case_when(country == "Northern Caribbean" ~ "Caribbean",
country == "The Americas" ~ "Regional Overview",
TRUE ~ region))
data <-
data %>%
mutate(region = region %>% factor() %>% fct_relevel("Regional Overview", after = 0),
country = factor(country),
section = factor(section, levels = c("Context", "Population Trends", "Protection Concerns",
"Operations", "Inter-Agency", "A.O.B")))
```
```{r renderers}
render_section <- function(data, .section) {
cat(glue::glue("### {.section}"), sep = "\n")
data <- data %>% filter(section == .section)
if (any(!is.na(data$confidential) & data$confidential == "yes"))
cat("<strong style='color: #EF4A60'>CONFIDENTIAL</strong>", sep = "\n")
cat("\n")
cat(data$narrative, sep = "\n")
cat("\n")
}
render_country <- function(data, .country) {
cat(glue::glue("## {.country}"), sep = "\n")
data <- data %>% filter(country == .country)
walk(levels(fct_drop(data$section)), ~render_section(data, .))
}
render_region <- function(data, .region) {
cat(glue::glue("# {.region}"), sep = "\n")
data <- data %>% filter(region == .region)
walk(levels(fct_drop(data$country)), ~render_country(data, .))
}
```
```{r pipeline, results='asis'}
diff_day <- 7
data <- data %>% filter(date >= (today()-diff_day))
walk(levels(fct_drop(data$region)), ~render_region(data, .))
```
::: {.back-cover}
::: {.back-header}
<h1 class="back-title"></h1>
<h1 class="back-subtitle"></h1>
:::
::: {.back-footer}
For more information please contact:
::: {.back-contact}
[Carmen Asenjo]{.back-name}
Information Management Officer
[asenjo@unhcr.org](mailto:asenjo@unhcr.org)
:::
::: {.back-contact}
[Matheus Soldi Hardt]{.back-name}
Associate Information Management Officer
[matheus@unhcr.org](mailto:matheus@unhcr.org)
:::
:::
:::