library(shiny)
library(dplyr)
library(ggplot2)
library(shinythemes)
# Define the data source
# Define the price plot module
pricePlotModule <- function(id){
moduleServer(id, function(input, output, session){
# Define the asset IDs
asset_ids <- reactive({
input$asset_ids
})
# Define the date range
date_range <- reactive({
input$date_range
})
# Filter the data based on the user inputs
filtered_data <- reactive({
mInfo %>%
filter(
assetID %in% asset_ids(),
TradeDate >= date_range()[1],
TradeDate <= date_range()[2]
)
})
# Create the plot
output$price_plot <- renderPlot({
ggplot(filtered_data(), aes(x = TradeDate, y = Price, color = assetID)) +
geom_line() +
labs(title = "Price",
x = "",
y = "") +
PlotThemeShinyTS
})
})
}
oasPlotModule <- function(id){
moduleServer(id, function(input, output, session){
# Define the asset IDs
asset_ids <- reactive({
input$asset_ids
})
# Define the date range
date_range <- reactive({
input$date_range
})
# Filter the data based on the user inputs
filtered_data <- reactive({
mInfo %>%
filter(
assetID %in% asset_ids(),
TradeDate >= date_range()[1],
TradeDate <= date_range()[2]
)
})
# Create the plot
output$oas_spread_plot <- renderPlot({
ggplot(filtered_data(), aes(x = TradeDate, y = OASpread, color = assetID)) +
geom_line() +
labs(title = "OAS Spread",
x = "",
y = "") +
PlotThemeShinyTS
})
})
}
durPlotModule <- function(id){
moduleServer(id, function(input, output, session){
# Define the asset IDs
asset_ids <- reactive({
input$asset_ids
})
# Define the date range
date_range <- reactive({
input$date_range
})
# Filter the data based on the user inputs
filtered_data <- reactive({
mInfo %>%
filter(
assetID %in% asset_ids(),
TradeDate >= date_range()[1],
TradeDate <= date_range()[2]
)
})
# Create the plot
output$duration_plot <- renderPlot({
ggplot(filtered_data(),aes(x = TradeDate, y = ModOAD, color = assetID)) +
geom_line() +
labs(title = "Duration",
x = "",
y = "") +
PlotThemeShinyTS
})
})
}
convPlotModule <- function(id){
moduleServer(id, function(input, output, session){
# Define the asset IDs
asset_ids <- reactive({
input$asset_ids
})
# Define the date range
date_range <- reactive({
input$date_range
})
# Filter the data based on the user inputs
filtered_data <- reactive({
mInfo %>%
filter(
assetID %in% asset_ids(),
TradeDate >= date_range()[1],
TradeDate <= date_range()[2]
)
})
# Create the plot
output$convex_plot <- renderPlot({
ggplot(filtered_data(),aes(x = TradeDate, y = ModOAC, color = assetID)) +
geom_line() +
labs(title = "Convexity",
x = "",
y = "") +
PlotThemeShinyTS
})
})
}
outamountModule <- function(id){
moduleServer(id, function(input, output, session){
# Define the asset IDs
asset_ids <- reactive({
input$asset_ids
})
# Define the date range
date_range <- reactive({
input$date_range
})
# Filter the data based on the user inputs
filtered_data <- reactive({
mOutAmount %>%
filter(
assetID %in% asset_ids(),
TradeDate >= date_range()[1],
TradeDate <= date_range()[2]
)
})
output$outamount <- renderPlot({
ggplot(filtered_data(),aes(x = TradeDate, y = OutstandingAmountM, color = assetID)) +
geom_line() +
labs(title = "Outstanding amount",
x = "",
y = "") +
PlotThemeShinyTS
})
})
}
# Define the UI
OSCUI <- tabItem("osc",
fluidPage(shinythemes::themeSelector(),
# Add the picker input for asset IDs
pickerInput(inputId = "asset_ids",
label = "Select Asset IDs",
choices = unique(mInfo$assetID),
multiple = TRUE,
options = list(`actions-box` = TRUE,
`selected-text-format` = "count > 3")),
# Add the date range input
dateRangeInput(inputId = "date_range",
label = "Select Date Range",
start = Sys.Date() - 365,
end = Sys.Date()),
fluidRow(
column(width = 4,
plotOutput("priceplot", height = "400px")),
column(width = 4,
plotOutput("oasplot", height = "400px")),
column(width = 4,
plotOutput("durplot", height = "400px"))
),
fluidRow(
column(width = 4,
plotOutput("convplot", height = "400px")),
column(width = 4,
plotOutput("outplot", height = "400px"))
)
)
)
server1 <- function(id){
pricePlotModule("priceplot")
oasPlotModule("oasplot")
durPlotModule("durplot")
convPlotModule("convplot")
outamountModule("outplot")
}
#####Customize app
#Add logo
title_logo <- tags$a(href = 'https://www.jyskebank.dk/presse/foto/logoerfoto',
tags$img(src = 'https://www.jyskebank.dk/wps/wcm/connect/jfo/0df5fc4a-b754-4b54-aad4-6b2f851d50ef/JB-logo_web.png?MOD=AJPERES&CVID=mAGm.6G', height= 25,width= 160))
#Specify the header
HeaderSize <-tags$head(tags$style(HTML(
'.myClass {
line-height: 50px;
text-align: center;
font-family: "Effra";
padding: 0 15px;
color: white;
font-size: 1.2vw;
}
')))
#Text in header
HeaderText <-tags$script(HTML('$(document).ready(function() {
$("header").find("nav").append(\'<div class="myClass"> Fixed Income Research - Key Figures </div>\');
})'))
#Colours in header and backgrounds
header_col <- tags$head(tags$style(HTML('
.skin-blue .main-header .logo { background-color: #FFFFFF;}
.skin-blue .main-header .navbar {background-color: #005c3c}
.content-wrapper {background-color: #ededed;}'))) # White background color
ui <- dashboardPage(
dashboardHeader(
title = title_logo , titleWidth = 300),
dashboardSidebar(width = 300,
sidebarMenu(
#Key figures tab
menuItem("OSC", tabName = "osc", icon = icon("file")
)
)),
dashboardBody(
#Add CSS Style to Dashboard
HeaderSize,
HeaderText,
header_col,
tabItems(
##Front page
OSCUI
)
)
)
server <- function(input, output, session){
server1("osc")
}
# Define the app
shinyApp(ui, server)
The above returns the app with no problem and it returns the pickerInput and dateRangeinput. However, it does not show any plots. I think the communication between module and server is wrong.