This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
web_tool_script_3.R
343 lines (280 loc) · 13.4 KB
/
web_tool_script_3.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
devtools::load_all(quiet = TRUE)
use_r_packages()
cli::cli_h1("web_tool_script_3.R{get_build_version_msg()}")
source("0_global_functions.R")
source("0_web_functions.R")
if (!exists("portfolio_name_ref_all")) { portfolio_name_ref_all <- "TestPortfolio_Input" }
if (!exists("portfolio_root_dir")) { portfolio_root_dir <- "working_dir" }
setup_project()
set_webtool_paths(portfolio_root_dir)
set_portfolio_parameters(file_path = fs::path(par_file_path, paste0(portfolio_name_ref_all, "_PortfolioParameters.yml")))
set_project_parameters(file.path(working_location, "parameter_files",paste0("ProjectParameters_", project_code, ".yml")))
analysis_inputs_path <- set_analysis_inputs_path(twodii_internal, data_location_ext, dataprep_timestamp)
# quit if there's no relevant PACTA assets --------------------------------
total_portfolio_path <- file.path(proc_input_path, portfolio_name_ref_all, "total_portfolio.rda")
if (file.exists(total_portfolio_path)) {
total_portfolio <- readRDS(total_portfolio_path)
quit_if_no_pacta_relevant_data(total_portfolio)
} else {
warning("This is weird... the `total_portfolio.rda` file does not exist in the `30_Processed_inputs` directory.")
}
# stress test -------------------------------------------------------------
# provide parameters for stress test
invisible(set_portfolio_parameters(file_path = fs::path(par_file_path, paste0(portfolio_name_ref_all, "_PortfolioParameters.yml"))))
# set environment variable for stress test data path
options("ST_DATA_PATH" = stress_test_data_location)
# run 2dii stress test
failed_stress_test_run <- FALSE
tryCatch(
source(file.path(stress_test_path, "web_tool_stress_test.R")),
error = function(e) { failed_stress_test_run <<- TRUE; write_log("an error in web_tool_stress_test.R occurred"); }
)
# run stress test with external scenarios (IPR)
tryCatch(
source(file.path(stress_test_path, "web_tool_external_stress_test.R")),
error = function(e) { failed_stress_test_run <<- TRUE; write_log("an error in web_tool_external_stress_test.R occurred"); }
)
# fix parameters ----------------------------------------------------------
if(project_code == "PA2020FL"){
peer_group = case_when(
peer_group %in% c("other")~ "Others",
peer_group %in% c("bank", "assetmanager") ~ "Banks and Asset Managers",
peer_group %in% c("pensionfund", "insurance") ~ "Pension Funds and Insurances"
)
}
if(project_code == "GENERAL"){
language_select = "EN"
}
# create interactive report -----------------------------------------------
source(file.path(template_path, "create_interactive_report.R"))
source(file.path(template_path, "create_executive_summary.R"))
source(file.path(template_path, "useful_functions.R"))
source(file.path(template_path, "export_environment_info.R"))
report_name = select_report_template(project_report_name = project_report_name,
language_select = language_select)
exec_summary_name = select_exec_summary_template(project_report_name = project_report_name,
language_select = language_select)
template_dir <- paste0(template_path, report_name,"/_book/")
exec_summary_dir <- paste0(template_path, exec_summary_name,"/")
survey_dir <- path(user_results_path, project_code, "survey")
real_estate_dir <- path(user_results_path, project_code, "real_estate")
output_dir <- file.path(outputs_path, portfolio_name_ref_all)
if (file.exists(file.path(proc_input_path, portfolio_name_ref_all, "audit_file.rda"))){
audit_file <- readRDS(file.path(proc_input_path, portfolio_name_ref_all, "audit_file.rda"))
}else{
audit_file <- empty_audit_file()
}
# load portfolio overview
if (file.exists(file.path(proc_input_path, portfolio_name_ref_all, "overview_portfolio.rda"))) {
portfolio_overview <- read_rds(file.path(proc_input_path, portfolio_name_ref_all, "overview_portfolio.rda"))
} else {
portfolio_overview <- empty_portfolio_overview()
}
if (file.exists(file.path(proc_input_path, portfolio_name_ref_all, "emissions.rda"))){
emissions <- read_rds(file.path(proc_input_path, portfolio_name_ref_all, "emissions.rda"))
}else{
emissions <- empty_emissions_results()}
# load equity portfolio data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Equity_results_portfolio.rda"))) {
equity_results_portfolio <- read_rds(file.path(results_path, portfolio_name_ref_all, "Equity_results_portfolio.rda"))
} else {
equity_results_portfolio <- empty_portfolio_results()
}
# load bonds portfolio data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Bonds_results_portfolio.rda"))) {
bonds_results_portfolio <- read_rds(file.path(results_path, portfolio_name_ref_all, "Bonds_results_portfolio.rda"))
} else {
bonds_results_portfolio <- empty_portfolio_results()
}
# load equity company data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Equity_results_company.rda"))) {
equity_results_company <- read_rds(file.path(results_path, portfolio_name_ref_all, "Equity_results_company.rda"))
} else {
equity_results_company <- empty_company_results()
}
# load bonds company data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Bonds_results_company.rda"))) {
bonds_results_company <- read_rds(file.path(results_path, portfolio_name_ref_all, "Bonds_results_company.rda"))
} else {
bonds_results_company <- empty_company_results()
}
# load equity map data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Equity_results_map.rda"))) {
equity_results_map <- read_rds(file.path(results_path, portfolio_name_ref_all, "Equity_results_map.rda"))
} else {
equity_results_map <- empty_map_results()
}
# load bonds map data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Bonds_results_map.rda"))) {
bonds_results_map <- read_rds(file.path(results_path, portfolio_name_ref_all, "Bonds_results_map.rda"))
} else {
bonds_results_map <- empty_map_results()
}
# load equity tdm data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Equity_tdm.rds"))) {
equity_tdm <- readRDS(file.path(results_path, portfolio_name_ref_all, "Equity_tdm.rds"))
} else {
equity_tdm <- NULL
}
# load bonds tdm data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Bonds_tdm.rds"))) {
bonds_tdm <- readRDS(file.path(results_path, portfolio_name_ref_all, "Bonds_tdm.rds"))
} else {
bonds_tdm <- NULL
}
# load equity stress test data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "equity_results_stress_test.rda"))) {
equity_results_stress_test <- read_rds(file.path(results_path, portfolio_name_ref_all, "equity_results_stress_test.rda"))
} else {
equity_results_stress_test <- empty_st_results()
}
# load bonds stress test data
if (file.exists(file.path(results_path, portfolio_name_ref_all, "bonds_results_stress_test.rda"))) {
bonds_results_stress_test <- read_rds(file.path(results_path, portfolio_name_ref_all, "bonds_results_stress_test.rda"))
} else {
bonds_results_stress_test <- empty_st_results()
}
# load IPR stress test results
if (file.exists(file.path(results_path, portfolio_name_ref_all, "Stress_test_results_IPR.rds"))) {
ipr_results_stress_test <- read_rds(file.path(results_path, portfolio_name_ref_all, "Stress_test_results_IPR.rds"))
} else {
ipr_results_stress_test <- empty_ipr_st_results()
}
# load peers results both individual and aggregate
if (file.exists(file.path(analysis_inputs_path, paste0(project_code, "_peers_equity_results_portfolio.rda")))){
peers_equity_results_portfolio <- read_rds(file.path(analysis_inputs_path, paste0(project_code, "_peers_equity_results_portfolio.rda")))
}else{
peers_equity_results_portfolio <- empty_portfolio_results()
}
if(file.exists(file.path(analysis_inputs_path, paste0(project_code, "_peers_bonds_results_portfolio.rda")))){
peers_bonds_results_portfolio <- read_rds(file.path(analysis_inputs_path, paste0(project_code, "_peers_bonds_results_portfolio.rda")))
}else{
peers_bonds_results_portfolio <- empty_portfolio_results()
}
if (file.exists(file.path(analysis_inputs_path, paste0(project_code, "_peers_equity_results_portfolio_ind.rda")))){
peers_equity_results_user <- read_rds(file.path(analysis_inputs_path, paste0(project_code, "_peers_equity_results_portfolio_ind.rda")))
}else{
peers_equity_results_user <- empty_portfolio_results()
}
if(file.exists(file.path(analysis_inputs_path, paste0(project_code, "_peers_bonds_results_portfolio_ind.rda")))){
peers_bonds_results_user <- read_rds(file.path(analysis_inputs_path, paste0(project_code, "_peers_bonds_results_portfolio_ind.rda")))
}else{
peers_bonds_results_user <- empty_portfolio_results()
}
indices_equity_results_portfolio <- read_rds(file.path(analysis_inputs_path, "Indices_equity_portfolio.rda"))
indices_bonds_results_portfolio <- read_rds(file.path(analysis_inputs_path, "Indices_bonds_portfolio.rda"))
dataframe_translations <- readr::read_csv(
path(template_path, "data/translation/dataframe_labels.csv"),
col_types = cols()
)
header_dictionary <- readr::read_csv(
path(template_path, "data/translation/dataframe_headers.csv"),
col_types = cols()
)
js_translations <- jsonlite::fromJSON(
txt = path(template_path, "data/translation/js_labels.json")
)
sector_order <- readr::read_csv(
path(template_path, "data","sector_order","sector_order.csv"),
col_types = cols()
)
# combine config files to send to create_interactive_report() ------------------
portfolio_config_path <- file.path(par_file_path, paste0(portfolio_name_ref_all, "_PortfolioParameters.yml"))
project_config_path <- file.path(working_location, "parameter_files", paste0("ProjectParameters_", project_code, ".yml"))
configs <-
list(
portfolio_config = config::get(file = portfolio_config_path),
project_config = config::get(file = project_config_path)
)
# Needed for testing only
shock <- shock_year # this should come directly from the stress test.. 2030 based on current discussions in CHPA2020 case
select_scenario_auto = scenario_auto
select_scenario_other = scenario_other
select_scenario_shipping = scenario_shipping
twodi_sectors = sector_list
repo_path = template_path
file_name = "template.Rmd"
create_interactive_report(
repo_path = template_path,
template_dir = template_dir,
output_dir = output_dir,
survey_dir = survey_dir,
real_estate_dir = real_estate_dir,
language_select = language_select,
report_name = report_name,
project_name = project_name,
investor_name = investor_name,
portfolio_name = portfolio_name,
peer_group = peer_group,
start_year = start_year,
shock = shock_year,
select_scenario = select_scenario,
select_scenario_auto = scenario_auto,
select_scenario_shipping = scenario_shipping,
select_scenario_other = scenario_other,
portfolio_allocation_method = portfolio_allocation_method,
scenario_geography = scenario_geography,
twodi_sectors = sector_list,
green_techs = green_techs,
tech_roadmap_sectors = tech_roadmap_sectors,
pacta_sectors_not_analysed = pacta_sectors_not_analysed,
audit_file = audit_file,
emissions = emissions,
portfolio_overview = portfolio_overview,
equity_results_portfolio = equity_results_portfolio,
bonds_results_portfolio = bonds_results_portfolio,
equity_results_company = equity_results_company,
bonds_results_company = bonds_results_company,
equity_results_map = equity_results_map,
bonds_results_map = bonds_results_map,
indices_equity_results_portfolio = indices_equity_results_portfolio,
indices_bonds_results_portfolio = indices_bonds_results_portfolio,
peers_equity_results_portfolio = peers_equity_results_portfolio,
peers_bonds_results_portfolio = peers_bonds_results_portfolio,
peers_equity_results_user = peers_equity_results_user,
peers_bonds_results_user = peers_bonds_results_user,
equity_results_stress_test = equity_results_stress_test,
bonds_results_stress_test = bonds_results_stress_test,
dataframe_translations = dataframe_translations,
js_translations = js_translations,
ipr_results_stress_test = ipr_results_stress_test,
display_currency = display_currency,
currency_exchange_value = currency_exchange_value,
header_dictionary = header_dictionary,
sector_order = sector_order,
equity_tdm = equity_tdm,
bonds_tdm = bonds_tdm,
configs = configs,
failed_stress_test_run = failed_stress_test_run
)
if(dir.exists(exec_summary_dir)){
create_executive_summary(
file_name = "template.Rmd",
exec_summary_dir = exec_summary_dir,
output_dir = output_dir,
language_select = language_select,
project_name = "working_dir",
investor_name = investor_name,
portfolio_name = portfolio_name,
peer_group = peer_group,
start_year = start_year,
select_scenario = scenario,
portfolio_allocation_method = portfolio_allocation_method,
scenario_geography = scenario_geography,
twodi_sectors = sector_list,
green_techs = green_techs,
tech_roadmap_sectors = tech_roadmap_sectors,
alignment_techs = alignment_techs,
equity_results_portfolio = equity_results_portfolio,
bonds_results_portfolio = bonds_results_portfolio,
peers_equity_results_portfolio = peers_equity_results_portfolio,
peers_bonds_results_portfolio = peers_bonds_results_portfolio,
peers_equity_results_user = peers_equity_results_user,
peers_bonds_results_user = peers_bonds_results_user
)
}else{
es_dir <- file.path(output_dir, "executive_summary")
if(!dir.exists(es_dir)){dir.create(es_dir, showWarnings = F, recursive = T)}
# this is required for the online tool to know that the process has been completed.
invisible(file.copy(file.path("data", "blank_pdf_do_not_delete.pdf"), es_dir))
}