forked from hazimtimimi/tb_profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_tab_finance.R
174 lines (122 loc) · 7.78 KB
/
build_tab_finance.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
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
161
162
163
164
165
166
167
168
169
170
171
172
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Build output for the sixth tab (financing charts and tables)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Add call out to footnote if finance is an aggregate group
output$finance_heading <- renderText({ ifelse(check_entity_type(input$entity_type) == "group",
paste0(ltxt(plabs(), "funding"),"***"),
ltxt(plabs(), "funding")
) })
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 1. Expenditure (received funding)
# and, if this is a country-level profile, the budget (committed funding) table
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Combine the data with the row headers manually and render for output
output$funding_table <- renderTable({
# Make sure there are data to display
req(pdata()$profile_properties)
# build the data frame manually, but only if dc_finance_display is true
# 1. Country-level version showing expenditure and budget
if (isTRUE(pdata()$profile_properties[, "dc_finance_display"]) & check_entity_type(input$entity_type) != "group"){
data.frame(c(paste0(ltxt(plabs(), "funding"), ", ", dcyear-1, " ", ltxt(plabs(), "usd_millions")),
paste0("- ", ltxt(plabs(), "fund_domestic")),
paste0("- ", ltxt(plabs(), "fund_international")),
paste0(ltxt(plabs(), "ntp_budget"), ", ", dcyear, " ", ltxt(plabs(), "usd_millions")),
paste0("- ", ltxt(plabs(), "funding_source"), ", ", ltxt(plabs(), "source_domestic")),
paste0("- ", ltxt(plabs(), "funding_source"), ", ", ltxt(plabs(), "source_international")),
paste0("- ", ltxt(plabs(), "source_unfunded"))
),
c(rounder(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"])),
# calculate pct_domestic and international for expenditure (received funding)
display_cap_pct(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]),
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"])),
display_cap_pct(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"]),
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"])),
rounder(pdata()$profile_data[, "tot_req"]),
# calculate pct_domestic, international and unfunded for budget (committed funding)
display_cap_pct(pdata()$profile_data[, "tot_domestic"],
pdata()$profile_data[, "tot_req"]),
display_cap_pct(pdata()$profile_data[, "tot_international"],
pdata()$profile_data[, "tot_req"]),
display_cap_pct(pdata()$profile_data[, "tot_gap"],
pdata()$profile_data[, "tot_req"])
)
)
}
# 1. Aggregate version showing expenditure only
else if (isTRUE(pdata()$profile_properties[, "dc_finance_display"]) & check_entity_type(input$entity_type) == "group") {
data.frame(c(paste0(ltxt(plabs(), "funding"), ", ", dcyear-1, " ", ltxt(plabs(), "usd_millions")),
paste0("- ", ltxt(plabs(), "fund_domestic")),
paste0("- ", ltxt(plabs(), "fund_international"))
),
c(rounder(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"])),
# calculate pct_domestic and international for expenditure (received funding)
display_cap_pct(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]),
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"])),
display_cap_pct(NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"]),
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "a_domestic_funds"]) +
NZ(pdata()$funding_timeseries[pdata()$funding_timeseries$year == dcyear-1, "b_international_funds"]))
)
)
}
},
striped = TRUE,
hover = TRUE,
width = "100%",
# right-align the data column
align = "lr",
# suppress column headers
colnames = FALSE,
na="")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 2. Expenditure (committed funding) chart
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Move heading and subheading out of ggplot2
# because ggplot2 headings don't wrap when space is restricted
# Add callout to footnote if finance is an aggregate group
output$funding_chart_head <- renderText({ ifelse(check_entity_type(input$entity_type) == "group",
paste0(ltxt(plabs(), "funding"),"***"),
ltxt(plabs(), "funding")
) })
output$funding_chart_subhead <- renderText({ ltxt(plabs(), "usd_millions") })
output$funding_chart <- renderPlot({
# Make sure there are data to plot
req(pdata()$funding_timeseries)
# First make sure there are some data to display
# There will only be the year column if no data, so check number of columns
ndata_cols <- ncol(pdata()$funding_timeseries) - 1
# Only plot the data if have at least one year with data
if (ndata_cols > 0){
plotobj <- pdata()$funding_timeseries %>%
# Convert to long format
pivot_longer(cols = -year,
names_to = "funding",
values_to = "fund_amount",
# drop empty values
values_drop_na = TRUE) %>%
ggplot(aes(x=year, y=fund_amount, fill = funding)) +
geom_col(position = position_stack(reverse = TRUE)) +
profile_theme() +
scale_fill_manual("",
values = funding_palette(),
labels = c("a_domestic_funds" = ltxt(plabs(), "source_domestic"),
"b_international_funds" = ltxt(plabs(), "source_international"))) +
scale_x_continuous(name="", seq(dcyear-5, dcyear-1))
} else {
plotobj <- NA
}
return(plotobj)
})
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 3. Add footnote for aggregated finance
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
output$foot_aggfin <- renderText({ ifelse(check_entity_type(input$entity_type) == "group",
HTML(paste("***<i>",
ltxt(plabs(), "foot_aggfin_lmc"),
ltxt(plabs(), "foot_aggfin_ghs"),
"</i>")),
"") })