thank you @andresrcs. Below I provide code of my shiny app. In this code I use some user-defined functions to get suitable data from SQL server and plot them. Code is too long and if you have not time, don't review it. I guess, the problem is that I should import data every time from SQL every time and it is time-consuming. Is it solvable?
ui <- fluidPage(
theme = shinytheme("slate"),
tabsetPanel(
tabPanel(h4("TOP N' financial"),
fluidRow(
column(3,
selectInput("bank", "Choose Bank", choices = unique(BANKS$BANK)),
numericInput("Number", "Choose top N borrower", value = 10, min = 1),
tags$style(type='text/css', ".selectize-input { font-size: 10px; line-height: 1px;}
.selectize-dropdown { font-size: 10px; line-height: 10px; }"),
selectizeInput("excl","SELECT Columns", colname, multiple=T)
),
column(9,
p("You can draw table that contains information about the largest companies by
total assets. Select the bank, the number of loans and the desired variables
from the left-side input fields.", style="color : white"),
div(dataTableOutput("topNtable"), style = "font-size: 75%; width: 75%")
)
),
br(),
code("Following section analyse individual borrower; Choose CLIENT_ID and 2 variable to calculate some ratio;
Right table show this coefficient historically (last 6 months), while left-side plot draws it visually."),
fluidRow(style = "background-color:#4d3a7d;",
h4("Ratio Calculation and plotting"),
column(7,
style = "border: 4px ridge #01B392;",
fluidRow(
textInput("client_ID", "Insert Client ID", value = '204885044'),
uiOutput("ID_table")
),
fluidRow(style = "border: 4px ridge #FF8902;",
plotlyOutput('coef_plot'))
),
column(5,
style = "border: 4px double red;",
h4('simple calculator'),
fluidRow(
tags$style(type='text/css', ".selectize-input { font-size: 10px; line-height: 1px;}
.selectize-dropdown { font-size: 10px; line-height: 10px; }"),
column(6,
selectInput("num", "Numerator", num_cols, selected = 'TOTAL_ASSETS')),
column(6,
selectInput("denum", "denumerator", num_cols, selected = 'TOTAL_LIABILITIES'))
),
fluidRow(
column(6, align = "center",
div(tableOutput("coefficient"))#,style = "font-size: 75%; width: 75%")
)
)
)
),
br(),
em("Following table (right) provides more information about CLIENT you choose above. While plots visualize PD/LGD/ECL changes historically"),
fluidRow(tags$head(tags$style('p {color:black;}')),
h4("ECL Section"),
style = "background-color:#FF9F2F;",
column(7, align = "center",
plotOutput('ecl_pl')
),
column(5, align = "center",
selectInput('ind_date','Choose Date', choices = NULL),
tableOutput('ind_table') #div()#,style = "font-size: 80%; width: 80%", align = "center")
)
)
),
tabPanel(h4("Retail"),
sidebarLayout(
sidebarPanel(
dateInput("start", label = "Data start date",
Sys.Date()-72,
min = ymd(20210501)),
dateInput("end", label = "Data end date",
Sys.Date()-42,
min = ymd(20210601)),
numericInput("N", "Number of observation:", value = 1000000, min = 500000, step = 50000),
selectInput("stage", "Choose stage", c("1", "2", "3")),
h4("The left graph analyzes the PD and LGD parameters. The first column shows the relationship between PTI and PD,
both at the bank level and aggregated. The upper right graph shows the PD boxplot banks, while the lower right
graph shows the connection between LTV and LGD for the first N observations.",
style = "font-family: 'times'; font-si16pt; color:white")
),
mainPanel(
div(
style = "display: flex; flex-wrap: wrap; justify-content: center",
div(plotOutput("reg_pl"), style = "width: 50%; border: solid;"),
div(plotOutput("Box_pd"), style = "width: 50%; border: solid;"),
div(plotOutput("PTI_PD"), style = "width: 50%; border: solid;"),
div(plotOutput("LTV_LGD"), style = "width: 50%; border: solid;")
)
)
),
br(),
h3("This section analysis clients who have similar liabilities in different banks", align = "center", style = "color:white"),
fluidRow(
column(4,
dateInput("start", label = "Data start date",
Sys.Date()-60,
min = ymd(20210501)),
dateInput("end", label = "Data end date",
Sys.Date()-30,
min = ymd(20210601)),
numericInput("N", "Number of observation:", value = 500, min = 100, step = 50)
),
column(8,
plotlyOutput("PD_Client_scatter")
)
),
br(),
h3("brush observation on plot and see more detailly info about those clients on right table", align = "center"),
fluidRow(
column(6,
plotOutput("PD_scat", brush="brush")),
column(6,
dataTableOutput("brush_tbl")
)
)
)
)
)
server <- function(input, output, session){
react_fin <- reactive(get_top_N_financial(input$bank,input$Number, input$excl)%>%
group_by(CLIENT_ID)%>%
filter(DATE == max(DATE))%>%
head(n=input$Number))
output$topNtable <- renderDataTable(react_fin())
#CALCULATOR
calc_d <- reactive(get_top_N_financial(input$bank,input$Number, c(num_cols,'DATE')))
coef_d <- reactive({
req(input$client_ID)
filter(calc_d(), CLIENT_ID == input$client_ID)
})
coef_df <- reactive({
req(input$num)
req(input$denum)
ratio <- round(coef_d()[[input$num]]/coef_d()[[input$denum]], 4)
data.frame(cbind(ratio, coef_d()$DATE))%>%
rename('Date'=V2)%>%
arrange(desc(Date))
})
output$coefficient <- renderTable(coef_df()%>%
slice_head(n = 6))
output$coef_plot <-renderPlotly(plot_ly(coef_df(), x = ~Date,
y = ~ratio, type = 'scatter', mode = 'lines',
height = 300, width = 500))
Client <- reactive((get_name(input$bank, input$client_ID)))
output$ID_table <- renderUI(Client()$CLIENT_NAME)
pd_dt <- reactive(get_debt(input$client_ID))
output$ecl_pl <- renderPlot(plot_pd_lgd(pd_dt()))
## create ind_table
observeEvent(pd_dt(), {
choices <- unique(pd_dt()$DATE)
updateSelectInput(inputId = "ind_date", choices = choices)
})
output$ind_table <-function()(ind_sum_tbl(pd_dt(), input$ind_date))
################# PAGE2 #################
d_PD_PTI <- reactive(get_PD_PTI(input$N, input$start, input$end, input$stage))
output$reg_pl <- renderPlot(PD_PTI_Bank(d_PD_PTI()))
d_PD_Box <- reactive(get_PD_Box(input$N,input$stage))
output$Box_pd <- renderPlot(PD_Box(d_PD_Box()))
output$PTI_PD <- renderPlot(PD_PTI_heat(d_PD_PTI()))
d_LGD_LTV <- reactive(get_LGD_LTV(input$N, input$start, input$end,input$stage))
output$LTV_LGD <- renderPlot(LGD_LTV_heat(d_LGD_LTV()))
output$PD_Client_scatter <- renderPlotly(PD_diff_banks_scatter(input$start, input$end, input$N))
df <- reactive(PD_CLIENT_DF(input$start, input$end,input$N))
output$PD_scat <- renderPlot(PD_CLIENT_plot(df()))
output$brush_tbl <-renderDataTable({
brushedPoints(df(), input$brush)
})
}
shinyApp(ui = ui, server = server)