Skip to content

Commit

Permalink
dynamic box height
Browse files Browse the repository at this point in the history
  • Loading branch information
bergalli committed Feb 26, 2024
1 parent e261210 commit 7034acb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 82 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ COPY --chown=shiny:shiny rhino.yml ./
COPY --chown=shiny:shiny app app/

COPY --chown=shiny:shiny docker/shiny-server.conf /etc/shiny-server/
# RUN mkdir -p /var/run/s6 && chown -R shiny:shiny /var/run/s6
# USER shiny

# TODO HOW TO USE SHINY USER AND STILL SEE ENV VARIABLE
# RUN mkdir -p /var/run/s6 && chown -R shiny:shiny /var/run/s6
# USER shiny
6 changes: 3 additions & 3 deletions app/logic/plots/trisk_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ draw_trisk_line_plot <- function(
# ggplot2::geom_point(size = 0.1) +
ggplot2::scale_y_continuous(labels = scales::percent_format(scale = 1)) +
ggplot2::scale_linetype_manual(values = c(
"production_baseline_scenario" = "dashed",
"production_target_scenario" = "solid",
"production_shock_scenario" = "dotted"
"production_baseline_scenario" = "dotted",
"production_target_scenario" = "dashed",
"production_shock_scenario" = "solid"
)) +
ggplot2::labs(
x = "Year",
Expand Down
3 changes: 2 additions & 1 deletion app/logic/renamings.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ RENAMING_ANALYSIS_COLUMNS <- c(
"expected_loss_portfolio" = "Expected Loss",
"expected_loss_baseline" = "Expected Loss (Baseline)",
"expected_loss_shock" = "Expected Loss (Shock)",
"pd_difference" = "Difference in PD"
"pd_difference" = "PDs Difference (shock-baseline)",
"expected_loss_difference" = "Expected losses difference (shock-baseline)"
)


Expand Down
9 changes: 4 additions & 5 deletions app/view/crispy_loans.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ server <- function(id, perimeter, backend_trisk_run_folder, possible_trisk_combi
"term",
"exposure_value_usd",
"loss_given_default",
"pd_baseline",
"expected_loss_baseline",
"pd_shock",
"expected_loss_shock"
"pd_difference",
"expected_loss_difference"
)
editable_columns_names_loans <- c("exposure_value_usd", "loss_given_default")
colored_columns_names_loans <- c("pd_baseline", "pd_shock")
colored_columns_names_loans <- c("pd_difference")

out <- portfolio_analysis$server(
"portfolio_analysis",
Expand All @@ -79,3 +77,4 @@ server <- function(id, perimeter, backend_trisk_run_folder, possible_trisk_combi
)
})
}

Check warning on line 80 in app/view/crispy_loans.R

View workflow job for this annotation

GitHub Actions / Run linters and tests

file=app/view/crispy_loans.R,line=80,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.
94 changes: 31 additions & 63 deletions app/view/modules/plots_loans.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ box::use(
ui <- function(id) {
ns <- NS(id)
shiny::fluidRow(
# style = "height: 1300px;",
shiny::uiOutput(ns("dynamicHeightBox_pd_term_plot")), # Dynamic UI for the box
shiny::uiOutput(ns("dynamicHeightBox_expected_loss_plot")) # Dynamic UI for the box
semantic.dashboard::box(title = "PD Difference", width = 8, collapsible = FALSE,
plotOutput(ns("pd_term_plot_output"), height = "100%")
),
semantic.dashboard::box(title = "Exposure Change", width = 8, collapsible = FALSE,
plotOutput(ns("exposure_change_plot"), height = "100%")
)
)

}

####### Server
Expand All @@ -27,13 +29,11 @@ ui <- function(id) {
server <- function(id, analysis_data_r, crispy_data_agg_r, max_trisk_granularity) {
moduleServer(id, function(input, output, session) {


base_height_per_facet <- 200 # height in pixels # TODO GO IN CONF

base_height_per_facet <- 150 # height in pixels # TODO GO IN CONF

# PD PLOT

observeEvent(crispy_data_agg_r(), {
observeEvent(c(crispy_data_agg_r(), analysis_data_r()), {
if (nrow(crispy_data_agg_r()) > 0) {

granul_levels <- dplyr::intersect(colnames(crispy_data_agg_r()), names(max_trisk_granularity))
Expand All @@ -42,21 +42,13 @@ server <- function(id, analysis_data_r, crispy_data_agg_r, max_trisk_granularity
num_facets <- length(unique(crispy_data_agg_r()[[granul_top_level]]))


# Dynamically create the box with adjusted height
output$dynamicHeightBox_pd_term_plot <- shiny::renderUI({
# Assuming plot height plus some margin
calculated_height <- (num_facets * base_height_per_facet) + 50
semantic.dashboard::box(
title = "PD Difference",
width = 8,
collapsible = FALSE,
style = paste0("height: ", calculated_height, "px;"), # Setting height dynamically
plotOutput(session$ns("pd_term_plot_output")) # this id value is used in the server, just below
)
})
# crispy_data_agg_user_filtered <- crispy_data_agg_r() |>
# dplyr::inner_join(
# analysis_data_r() |> dplyr::distinct_at(granul_top_level)
# )


pd_term_plot <- pipeline_pd_term_plot(
pd_term_plot <- pipeline_pd_term_plot(
crispy_data_agg=crispy_data_agg_r(),
facet_var=granul_top_level
)
Expand All @@ -69,53 +61,29 @@ server <- function(id, analysis_data_r, crispy_data_agg_r, max_trisk_granularity
}
})

# EXPECTED LOSS PLOT

observeEvent(analysis_data_r(), {
if (nrow(analysis_data_r()) > 0) {

# Then, prepare and render the plot
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]))]

# EXPECTED LOSS PLOT


observeEvent(analysis_data_r(), {
if (nrow(analysis_data_r()) > 0) {

# Then, prepare and render the plot
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]))]

num_facets <- length(unique(analysis_data_r()[[granul_top_level]]))
num_facets <- length(unique(analysis_data_r()[[granul_top_level]]))

# Dynamically create the box with adjusted height
output$dynamicHeightBox_expected_loss_plot <- shiny::renderUI({
# Assuming plot height plus some margin
calculated_height <- (num_facets * base_height_per_facet) + 50

# Return box with dynamic height set via inline CSS
semantic.dashboard::box(
title = "Expected Loss Baseline and Shock",
width = 8,
collapsible = FALSE,
style = paste0("height: ", calculated_height, "px;"), # Setting height dynamically
plotOutput(session$ns("expected_loss_plot_output")))

})


expected_loss_plot <- pipeline_expected_loss_plot(
analysis_data_r(),
facet_var = granul_top_level
expected_loss_plot <- pipeline_expected_loss_plot(
analysis_data_r(),
facet_var = granul_top_level
)
output$expected_loss_plot_output <- shiny::renderPlot({
expected_loss_plot
},
height = num_facets * base_height_per_facet
)
output$expected_loss_plot_output <- shiny::renderPlot({
expected_loss_plot
},
height = num_facets * base_height_per_facet
)



}
}

)})}
}
})
})}



Expand Down
3 changes: 1 addition & 2 deletions app/view/modules/plots_trajectories.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ ui <- function(id) {
title = "Production Trajectories",
width = 16,
collapsible = FALSE,
# height = "900px",
plotOutput(ns("trisk_line_plot_output"))
plotOutput(ns("trisk_line_plot_output"), height = "100%")
)
)
}
Expand Down
13 changes: 7 additions & 6 deletions app/view/portfolio/portfolio_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ generate_analysis_data <- function(portfolio_data_r, crispy_data_r, portfolio_as
portfolio_data_r(),
crispy_data_r(),
by = granularity
) |>
dplyr::mutate(
crispy_perc_value_change = NA,
crispy_value_loss = NA,
pd_difference = NA
)
)
# |>
# dplyr::mutate(
# crispy_perc_value_change = NA,
# crispy_value_loss = NA,
# pd_difference = NA
# )
}

# Aggregate Crispy data without portfolio with stress.test.plot.report fun
Expand Down

0 comments on commit 7034acb

Please sign in to comment.