-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
210 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
pipeline_pd_term_plot <- function(crispy_data_agg, facet_var="ald_sector"){ | ||
Check warning on line 2 in app/logic/plots/pd_term_plot.R GitHub Actions / Run linters and tests
Check warning on line 2 in app/logic/plots/pd_term_plot.R GitHub Actions / Run linters and tests
|
||
data_pd_term <- prepare_for_pd_term_plot( | ||
crispy_data_agg=crispy_data_agg, | ||
Check warning on line 4 in app/logic/plots/pd_term_plot.R GitHub Actions / Run linters and tests
|
||
facet_var=facet_var | ||
) | ||
pd_term_plot <- draw_pd_term_plot( | ||
data_pd_term=data_pd_term, | ||
facet_var=facet_var | ||
) | ||
|
||
return(pd_term_plot) | ||
} | ||
|
||
|
||
|
||
|
||
prepare_for_pd_term_plot <- function(crispy_data_agg, facet_var){ | ||
data_pd_term <- crispy_data_agg |> | ||
tidyr::pivot_longer( | ||
cols = tidyr::starts_with("pd_"), | ||
names_to = "pd_type", | ||
values_to = "pd_value", | ||
names_prefix = "pd_" | ||
) |> | ||
dplyr::mutate( | ||
pd_type=factor(.data$pd_type, levels = c("baseline", "shock", "difference")) | ||
)|> | ||
dplyr::select_at(c(facet_var, "term", "pd_type", "pd_value")) | ||
|
||
return(data_pd_term) | ||
} | ||
|
||
|
||
|
||
draw_pd_term_plot <- function(data_pd_term, facet_var){ | ||
|
||
red_hex_color <- r2dii.colours::palette_1in1000_plot |> | ||
dplyr::filter(.data$label == "red") |> | ||
dplyr::pull(.data$hex) | ||
green_hex_color <- r2dii.colours::palette_1in1000_plot |> | ||
dplyr::filter(.data$label == "green") |> | ||
dplyr::pull(.data$hex) | ||
grey_hex_color <- r2dii.colours::palette_1in1000_plot |> | ||
dplyr::filter(.data$label == "grey") |> | ||
dplyr::pull(.data$hex) | ||
|
||
|
||
pd_term_plot <- ggplot2::ggplot(data_pd_term, ggplot2::aes(x = as.factor(term), y = pd_value, fill = pd_value)) + | ||
ggplot2::geom_bar(stat = "identity", position = ggplot2::position_dodge()) + | ||
ggplot2::facet_grid(stats::as.formula(paste(paste(facet_var, collapse = "+"), "~ pd_type")), scales = "free_y") + | ||
ggplot2::scale_fill_gradient2( | ||
low = green_hex_color, | ||
high = red_hex_color, | ||
mid = grey_hex_color, | ||
midpoint = 0, | ||
limit = c(min(data_pd_term$pd_value), max(data_pd_term$pd_value)), | ||
space = "Lab") + | ||
r2dii.plot::theme_2dii() + | ||
ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) + | ||
ggplot2::labs(x = "Term", y = "PD Value", fill = "PD Type", title = "PD Values by Term, Type, and Business Unit") | ||
|
||
return(pd_term_plot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
box::use( | ||
shiny[moduleServer, NS, plotOutput, renderPlot, observeEvent, tags] | ||
) | ||
|
||
box::use( | ||
app/logic/plots/pd_term_plot[pipeline_pd_term_plot], | ||
app/logic/plots/crispy_npv_change_plot[pipeline_crispy_npv_change_plot] | ||
) | ||
|
||
|
||
####### UI | ||
|
||
ui <- function(id) { | ||
ns <- NS(id) | ||
shiny::fluidRow( | ||
semantic.dashboard::box(title = "PD Difference", width = 8, plotOutput(ns("pd_term_plotoutput"))), | ||
semantic.dashboard::box(title = "Exposure Change", width = 8, plotOutput(ns("exposure_change_plot"))) | ||
) | ||
} | ||
|
||
####### Server | ||
|
||
|
||
|
||
server <- function(id, analysis_data_r, crispy_data_agg_r, max_trisk_granularity) { | ||
moduleServer(id, function(input, output, session) { | ||
observeEvent(analysis_data_r(), { | ||
if (nrow(analysis_data_r()) > 0) { | ||
granul_levels <- dplyr::intersect(colnames(analysis_data_r()), names(max_trisk_granularity)) | ||
granul_top_level <- names(max_trisk_granularity[granul_levels])[which.max(unlist(max_trisk_granularity[granul_levels]))] | ||
|
||
# browser() | ||
|
||
crispy_user_filtered <- crispy_data_agg_r() |> | ||
dplyr::inner_join( | ||
analysis_data_r() |> dplyr::distinct_at(granul_top_level), | ||
by=granul_top_level | ||
) | ||
|
||
|
||
prepare_for_el_plot( | ||
analysis_data=analysis_data_r(), | ||
x_var=granul_top_level | ||
) | ||
|
||
|
||
pd_term_plot <- pipeline_pd_term_plot( | ||
crispy_data_agg=crispy_user_filtered, | ||
facet_var=granul_top_level | ||
) | ||
output$pd_term_plotoutput <- renderPlot({ | ||
pd_term_plot | ||
}) | ||
|
||
# crispy_npv_change_plot <- pipeline_crispy_npv_change_plot(analysis_data_r(), x_var = granul_top_level) | ||
# output$crispy_npv_change_plot <- renderPlot({ | ||
# crispy_npv_change_plot | ||
# }) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
|
||
|
||
prepare_for_el_plot <- function(analysis_data, x_var) { | ||
data_expected_loss <- analysis_data |> | ||
tidyr::pivot_longer( | ||
cols = tidyr::starts_with("expected_loss_"), | ||
names_to = "el_type", | ||
values_to = "el_value", | ||
names_prefix = "expected_loss_" | ||
) |> | ||
dplyr::select_at(c(x_var, "exposure_value_usd", "el_type", "el_value")) | ||
return(data_expected_loss) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.