R/mod_charts.R

Defines functions mod_charts_server mod_charts_ui

#' charts UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_charts_ui <- function(id){
  ns <- NS(id)
  tagList(
    shiny::textOutput(ns("topValue"))  %>%
      tagAppendAttributes(style= 'color:yellow;font-size:16pt;text-align:center'),
    shiny::tags$br(),
    shiny::plotOutput(ns("path"),height = 600),
    shiny::tags$br(),
    shiny::plotOutput(ns("hist"))
  )
}

#' charts Server Functions
#'
#' @noRd
mod_charts_server <- function(id, r) {
  moduleServer(id,
               function(input, output, session) {

                 ggtextCSS <- ggplot2::theme(plot.title = ggtext::element_markdown(size = 16,face = "bold"),
                                             plot.subtitle = ggtext::element_markdown(size = 12,face = "bold"),
                                             axis.title.x = ggplot2::element_text(size = 12),
                                             axis.title.y = ggplot2::element_text(size = 12))

                 output$path <- shiny::renderPlot({
                   arb <- sim <- toll <- NULL
                   #browser()
                   ggplot2::ggplot(
                     data = r$res[, 1:100] %>% tidyr::pivot_longer(-t, names_to = "sim", values_to = "arb"),
                     ggplot2::aes(x = t, y = arb, col = sim)
                   ) +
                     ggplot2::geom_line(alpha = 0.3) +
                     ggplot2::theme(legend.position = "none") +
                     ggplot2::labs(
                       title = "Simulations for Variable Cost Arb",
                       subtitle = "<span style = 'color:blue'><em>Forward Arb<em></span> , <span style = 'color:red'><em>TOP with Escalation<em></span>",
                       x = "Time in Years from Start",
                       y = "Arb Value"
                     ) +
                     ggplot2::geom_line(data = r$mu,
                                        ggplot2::aes(x = t, y = mr), col = "blue") +
                     ggplot2::geom_line(data = r$tolls,
                                        ggplot2::aes(x = t, y = toll),
                                        col = "red") +
                     ggtextCSS

                 })

                 output$hist <- shiny::renderPlot({
                    TOP <- mr <- ..density.. <- NULL
                   dplyr::tibble(TOP = r$topall) %>%
                     ggplot2::ggplot(ggplot2::aes(x = TOP)) +
                     ggplot2::geom_histogram(ggplot2::aes(y = ..density..), bins = 100, fill = "blue") +
                     ggplot2::geom_vline(xintercept = r$top, col = "red") +
                     ggplot2::labs(title = "Distribution of TOP equivalents",
                                   subtitle = paste("Average TOP value is",scales::dollar(r$top))) +
                     ggtextCSS
                 })

                 output$topValue <- shiny::renderText(paste("The TOP value is:",scales::dollar(r$top)))

               })
}

## To be copied in the UI
# mod_charts_ui("charts_1")

## To be copied in the server
# mod_charts_server("charts_1")
risktoollib/RTLappPipe documentation built on May 14, 2022, 4:52 p.m.