R/mod_npvAtRisk.R

Defines functions mod_npvAtRisk_server mod_npvAtRisk_ui

#' npvAtRisk UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_npvAtRisk_ui <- function(id){
  ns <- NS(id)
  shiny::fluidPage(
    shiny::fluidRow(
      tags$h5(tags$span(style = "color:aqua;font-style: italic;font-size:0.8em", "Explore the generated forward curves from your inputs")),
      tags$br(),
      plotly::plotlyOutput(ns("fwdCurves"), height = "600px")),
    shiny::fluidRow(
      align = "center",
      shiny::column(6,
                    tags$h5(tags$span(style = "color:aqua;font-style: italic;font-size:0.8em", "NPV Distributions for assets and portfolio")),
                    plotly::plotlyOutput(ns("histAll"), height = "600px")),
      shiny::column(6,
                    tags$h5(tags$span(style = "color:aqua;font-style: italic;font-size:0.8em", "Value of Take or Pay Commitment in $ per bbl")),
                    shiny::numericInput(ns("topesc"),"TOP annual escalation rate (%)", min = 0,max = 4, value = 2.25,step = .25),
                    shiny::tableOutput(ns("res")))
    )
  )

}

#' npvAtRisk Server Functions
#'
#' @noRd
mod_npvAtRisk_server <- function(id, r){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    sim <- npv <- asset <- NULL

    output$fwdCurves <- plotly::renderPlotly({
      r$payoffSummary %>%
        plotly::plot_ly(x = ~t, y = ~value, color = ~asset, type = "box") %>%
        plotly::layout(
          title = list(text = "Boxplot of Generated Forward Curves", x = 0),
          xaxis = list(title = "time forward (monthly periods)"),
          yaxis = list(title = "$ per bbl of variable cost economics")
        )
    })

    output$histAll <- plotly::renderPlotly({
      r$npvSummary %>%
        plotly::plot_ly(x = ~asset, y = ~npv, type = "violin", color = ~asset, side = "positive", meanline = list(visible = TRUE)) %>%
        plotly::layout(
          title = list(text = "NPVs for Selected Assets and Portfolio", x = 0.05),
          xaxis = list(title = "Assets)"),
          yaxis = list(title = "$ per 1 barrel Take of Pay Commitment of ")
        )
    })

    output$res <- shiny::renderTable({
      #browser()
      tmp <- r$npvSummary %>%
        dplyr::group_by(asset) %>%
        dplyr::summarise(npv = mean(npv)) %>%
        dplyr::filter(asset != "all")

      escalation <- (1 + input$topesc/100)^(seq(from = 0, to = r$term/12, by = 1/12) %/% 1)
      discounts <- stats::spline(RTL::usSwapCurves$times,RTL::usSwapCurves$discounts, xout = c(0,1:(r$term)/12))$y
      # npv = top * (escalation / discount) ; top = npv * discounts / escalation
      tmp$top <- tmp$npv/sum(discounts * escalation)
      tmp
    })



  })
}

## To be copied in the UI
# mod_npvAtRisk_ui("npvAtRisk_1")

## To be copied in the server
# mod_npvAtRisk_server("npvAtRisk_1")
risktoollib/RTLappStrat documentation built on Sept. 11, 2022, 10:27 p.m.