forked from e-clegg/ODA_research_and_innovation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script 7 - Output 2 (country active project lists).R
130 lines (100 loc) · 4.72 KB
/
Script 7 - Output 2 (country active project lists).R
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
# --------------------------------------------------------------- #
# Script 7
# Output Excel sheet of active projects by country #
# --------------------------------------------------------------- #
### 1) Set up ---
# Define countries for reports
## East Africa
# country_list <- c("Burundi", "Comoros", "Djibouti", "Ethiopia", "Eritrea", "Kenya",
# "Madagascar", "Malawi", "Mauritius", "Mozambique", "Réunion", "Rwanda",
# "Seychelles", "Somalia", "Somaliland", "Tanzania", "Uganda", "Zambia", "Zimbabwe")
## ASEAN countries
# country_list <- c("Cambodia", "Indonesia", "Laos", "Malaysia", "Myanmar", "Philippines",
# "Singapore", "Thailand", "Vietnam")
## IIOD countries
# country_list <- c("India", "Nepal", "Bangladesh", "Bhutan", "Sri Lanka", "Maldives")
## Colombia
# country_list <- c("Ukraine")
# West and South Africa
country_list <- c("Nigeria","South Africa","Ghana","Madagascar","Mozambique","Sierra Leone","Botswana",
"Senegal","Malawi", "Ivory Coast", "Zimbabwe", "Zambia")
# Define strings for use in output file name
string <- "Ukraine"
date <- "Feb22"
# Read in datasets
tableau_projects_tidied <- readRDS("Outputs/tableau_projects_tidied.rds")
# Abbrieviate/format organisation names
tableau_projects_tidied <- tableau_projects_tidied %>%
mutate(Funder = case_when(
Funder == "Foreign, Commonwealth and Development Office" ~ "FCDO",
Funder == "Department of Health and Social Care" ~ "DHSC",
Funder == "Department for Business, Energy and Industrial Strategy" ~ "BEIS"
))
# Define Excel styling
# Set header style (bold)
header_st <- createStyle(textDecoration = "Bold",
fontSize = 8, fontName = "Arial",
halign = "center")
# Set table style
table_st <- createStyle(fontSize = 8, fontName = "Arial",
wrapText = TRUE,
valign = "top")
### 2) Output reports for each country ----
wb <- openxlsx::createWorkbook()
for(i in 1:length(country_list)) {
print(paste0(i, " - ", country_list[i]))
# Extract project data for selected country
country_project_ids <- tableau_projects_tidied %>%
filter(Country == country_list[i]) %>%
mutate(Start = as.numeric(as.character(format(as.Date(start_date), format = "%Y"))),
End = as.numeric(as.character(format(as.Date(end_date), format = "%Y"))),
link = coalesce(link, ""))
output_report <- country_project_ids %>%
mutate(lead_org_name = coalesce(lead_org_name, extending_org)) %>%
select(Funder, Fund, Programme = funder_programme,
Title = title, Start, End, Description = abstract,
`Beneficiary Country` = recipient_country,
`Lead Organisation` = lead_org_name, `Partner Organisations` = partner_org_name,
`Value` = amount,
`Web Link` = link,
Currency = currency) %>%
group_by(Title, Funder) %>%
slice(1) %>%
ungroup() %>%
unique()
# Summarise funders on co-funded projects
co_funded_projects <- output_report %>%
group_by(Title) %>%
summarise(n = n(),
comb_funder = paste(coalesce(Funder, ""), collapse = ", ")) %>%
filter(n > 1) %>%
select(-n)
# Add summarised funders to main report
output_report <- output_report %>%
left_join(co_funded_projects, by = "Title") %>%
mutate(Funder = coalesce(comb_funder, Funder)) %>%
group_by(Title) %>%
# Keep one row per project title
slice(1) %>%
select(Funder, Fund, Programme, Title, Start, End, Description, `Beneficiary Country`,
`Lead Organisation`, `Partner Organisations`, Currency, `Value`, `Web Link`)
# Format numerical value column
class(output_report$`Value`) <- "comma"
# Add country dataset to output list
openxlsx::addWorksheet(wb, sheetName = country_list[i])
openxlsx::writeData(wb, sheet = i, x = output_report,
headerStyle = header_st,
borderStyle = "thin")
# Add font style
addStyle(wb, sheet = i, table_st, rows = 2:500, cols = 1:13, gridExpand = TRUE, stack = TRUE)
# Identify titles and hyperlinks
hyperlinks <- output_report$`Web Link`
names(hyperlinks) <- output_report$`Web Link`
class(hyperlinks) <- "hyperlink"
# Write hyperlinks
writeData(wb, sheet = i, x = hyperlinks, startRow = 2, startCol = 13, colNames = FALSE)
# Set column widths
setColWidths(wb, sheet = i, cols = 1:13, widths = c(10,25,30,40,6,6,60,30,30,30,10,10,50))
}
# Save Excel file
saveWorkbook(wb, paste0("Outputs//", string, " active ODA projects - ", date, ".xlsx"), overwrite = TRUE)