R/mod_context.R

Defines functions mod_context_server mod_context_ui

#' context UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_context_ui <- function(id){
  ns <- NS(id)
  tagList(
    tags$h5(tags$span(style = "color:aqua;font-style: italic;font-size:0.8em", "This tab shows the geographic context of assets.")),
    tags$ul(
      tags$li(tags$span(style = "color:aqua;font-size:0.8em", "Click on the circles to see historical spreads.")),
      tags$li(tags$span(style = "color:aqua;font-size:0.8em", "Expand the Google Maps to explore details of pipelines and locations."))
    ),
    leaflet::leafletOutput(ns("map"),width = "90%",height = "700px")
  )
}

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

    output$map <- leaflet::renderLeaflet({
      hub <- icons <- crudepipelines <- labelOptions <- p1 <- pnt <-  width <-height <-  NULL
      # popup chart dimensions
      width = 200
      height = 200

      icons <- leaflet::awesomeIcons(
        icon = c("database","database","database","industry","ship"),
        iconColor = 'black',
        library = "fa",
        markerColor = c("lightgreen","lightgreen","lightblue","lightblue","lightgreen")
      )

      leaflet::leaflet() %>%
        leaflet::addTiles() %>%
        #setView(lng = -106, lat = 45, zoom = 3.49) %>%
        leaflet::fitBounds(lng1 = -105,lat1 = 57,lng2 = -105,lat2 = 30) %>%
        leaflet::addPolylines(
          data = RTLappStrat::crudepipelines,
          color = "blue",
          weight = 0.8,
          popup = ~ Pipename,
          group = "oil"
        ) %>%
        leaflet::addAwesomeMarkers(
          data = RTL::tradeHubs %>% dplyr::filter(!hub %in% c("Mont Belvieu")),
          icon = icons,
          lng = ~ long,
          lat = ~ lat,
          popup = ~ hub,
          label = ~ hub,
          labelOptions = leaflet::labelOptions(noHide = T, textOnly = TRUE,textsize = "12px")
        ) %>%
        leaflet::addCircleMarkers(data = RTLappStrat::econsCharts[["wc"]][[2]], group = "wc", radius = 15) %>%
        leafpop::addPopupGraphs(list(RTLappStrat::econsCharts[["wc"]][[1]]), group = "wc", width = width, height = height) %>%
        leaflet::addCircleMarkers(data = RTLappStrat::econsCharts[["edm"]][[2]], group = "edm", radius = 15) %>%
        leafpop::addPopupGraphs(list(RTLappStrat::econsCharts[["edm"]][[1]]), group = "edm", width = width, height = height) %>%
        leaflet::addCircleMarkers(data = RTLappStrat::econsCharts[["hdy"]][[2]], group = "hdy", radius = 15) %>%
        leafpop::addPopupGraphs(list(RTLappStrat::econsCharts[["hdy"]][[1]]), group = "hdy", width = width, height = height) %>%
        leaflet::addCircleMarkers(data = RTLappStrat::econsCharts[["cushing"]][[2]], group = "cus", radius = 15) %>%
        leafpop::addPopupGraphs(list(RTLappStrat::econsCharts[["cushing"]][[1]]), group = "cus", width = width, height = height) %>%
        leaflet::addCircleMarkers(data = RTLappStrat::econsCharts[["houston"]][[2]], group = "hou", radius = 15) %>%
        leafpop::addPopupGraphs(list(RTLappStrat::econsCharts[["houston"]][[1]]), group = "hou", width = width, height = height) %>%
        leaflet::addLegend(
          "topleft",
          colors = c("red", "black", "blue"),
          labels = c("Refinery", "Refinery w/Coker", "Oil Pipe"),
          opacity = 0.8
        ) %>%
        leaflet::addCircleMarkers(
          data = RTLappStrat::refineries,
          color = ~ dplyr::case_when(Cokin_Mbpd > 0 ~ "black", TRUE ~ "red"),
          radius =  ~ AD_Mbpd / 100,
          popup = ~ paste0(Corp, "-", Site),
          group = "oil"
        )
    })
  })
}

## To be copied in the UI
# mod_context_ui("context_1")

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