From a8a6d43d67acf591f87328f54888f2a7eb7bf70b Mon Sep 17 00:00:00 2001 From: Asitav Sen Date: Fri, 18 Feb 2022 11:10:36 +0100 Subject: [PATCH] Data upload, download, display, modals and modules added. --- .Rproj.user/178A6739/sources/prop/48F98022 | 4 +- .Rproj.user/178A6739/sources/prop/A1AE5A83 | 4 +- .../sources/session-99529da2/58AD392C | 10 +- .../session-99529da2/58AD392C-contents | 126 ++++++++-------- .../sources/session-99529da2/FE6BB309 | 8 +- .../session-99529da2/FE6BB309-contents | 5 +- .../sources/session-99529da2/FEF97A26 | 24 +++ .../session-99529da2/FEF97A26-contents | 141 ++++++++++++++++++ app.R | 5 +- mod_basic.R | 141 ++++++++++++++++++ modals.R | 104 +++++++++++++ panel.R | 126 ++++++++-------- 12 files changed, 555 insertions(+), 143 deletions(-) create mode 100644 .Rproj.user/178A6739/sources/session-99529da2/FEF97A26 create mode 100644 .Rproj.user/178A6739/sources/session-99529da2/FEF97A26-contents create mode 100644 mod_basic.R create mode 100644 modals.R diff --git a/.Rproj.user/178A6739/sources/prop/48F98022 b/.Rproj.user/178A6739/sources/prop/48F98022 index a0ebf5d..c86caeb 100644 --- a/.Rproj.user/178A6739/sources/prop/48F98022 +++ b/.Rproj.user/178A6739/sources/prop/48F98022 @@ -1,6 +1,6 @@ { "source_window_id": "", "Source": "Source", - "cursorPosition": "50,5", - "scrollLine": "25" + "cursorPosition": "50,4", + "scrollLine": "12" } \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/prop/A1AE5A83 b/.Rproj.user/178A6739/sources/prop/A1AE5A83 index 14ac949..56d238a 100644 --- a/.Rproj.user/178A6739/sources/prop/A1AE5A83 +++ b/.Rproj.user/178A6739/sources/prop/A1AE5A83 @@ -1,6 +1,6 @@ { "source_window_id": "", "Source": "Source", - "cursorPosition": "36,2", - "scrollLine": "0" + "cursorPosition": "140,7", + "scrollLine": "121" } \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/session-99529da2/58AD392C b/.Rproj.user/178A6739/sources/session-99529da2/58AD392C index c3083a0..bdf502c 100644 --- a/.Rproj.user/178A6739/sources/session-99529da2/58AD392C +++ b/.Rproj.user/178A6739/sources/session-99529da2/58AD392C @@ -3,7 +3,7 @@ "path": "~/Projects/LoanRisk/panel.R", "project_path": "panel.R", "type": "r_source", - "hash": "814836357", + "hash": "2913767970", "contents": "", "dirty": false, "created": 1686047069391.0, @@ -12,15 +12,15 @@ "properties": { "source_window_id": "", "Source": "Source", - "cursorPosition": "50,5", - "scrollLine": "25" + "cursorPosition": "50,4", + "scrollLine": "12" }, "folds": "", - "lastKnownWriteTime": 1686047216, + "lastKnownWriteTime": 1686047587, "encoding": "UTF-8", "collab_server": "", "source_window": "", - "last_content_update": 1686047216552, + "last_content_update": 1686047587539, "read_only": false, "read_only_alternatives": [] } \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/session-99529da2/58AD392C-contents b/.Rproj.user/178A6739/sources/session-99529da2/58AD392C-contents index b05bbf0..542a99f 100644 --- a/.Rproj.user/178A6739/sources/session-99529da2/58AD392C-contents +++ b/.Rproj.user/178A6739/sources/session-99529da2/58AD392C-contents @@ -48,67 +48,67 @@ panel1<- ") ) ), - ))#, - # tags$hr(), - # fluidRow( - # column( - # width = 6, - # fluidRow( - # h3("Data"), - # br(), - # p("This section shows the data (uploaded or inbuilt sample).") - # ), - # br(), - # withSpinner(dataTableOutput("up_data"),type = 7, - # color = "black"), - # br(), - # actionButton("uploadnew", "Upload New Data", class="btn-light") - # ), - # column(width = 6, - # br(), - # basicstatUI("nofloans")) - # - # ), - # br(), - # br(), - # fluidRow( - # column( - # width = 6, - # h3("With Macroeconomic data"), - # p( - # "In this section, we add macroeconomic data. Please select the country of the asset and click on fetch button. If download fails, click again or try after sometime. Please note that GDP data has been discontinued by IMF recently. Hence, using Industrial output temporarily." - # ), - # column( - # width = 6, - # selectInput( - # "country", - # "Country", - # selected = "India", - # choices = countries$Country - # ) - # ), - # column( - # width = 6, - # br(), - # actionButton("fetchimf", "Fetch IMF data", class = "glow") - # ), - # withSpinner( - # dataTableOutput("fulldata"), - # type = 7, - # color = "black" - # ) - # ), - # column(width = 6, - # uiOutput("ac_button"), - # uiOutput("expcalcu")) - # ), - # br(), - # br(), - # fluidRow(column(width = 11, - # uiOutput("scenario_opts"))), - # br(), - # fluidRow(column(width = 11, - # uiOutput("credit_loss"))), - # fluidRow(column(width = 11, - # uiOutput("dlmanager"))) + )), + tags$hr(), + fluidRow( + column( + width = 6, + fluidRow( + h3("Data"), + br(), + p("This section shows the data (uploaded or inbuilt sample).") + ), + br(), + withSpinner(dataTableOutput("up_data"),type = 7, + color = "black"), + br(), + actionButton("uploadnew", "Upload New Data", class="btn-light") + ), + column(width = 6, + br(), + basicstatUI("nofloans")) + + ), + br(), + br(), + fluidRow( + column( + width = 6, + h3("With Macroeconomic data"), + p( + "In this section, we add macroeconomic data. Please select the country of the asset and click on fetch button. If download fails, click again or try after sometime. Please note that GDP data has been discontinued by IMF recently. Hence, using Industrial output temporarily." + ), + column( + width = 6, + selectInput( + "country", + "Country", + selected = "India", + choices = countries$Country + ) + ), + column( + width = 6, + br(), + actionButton("fetchimf", "Fetch IMF data", class = "glow") + ), + withSpinner( + dataTableOutput("fulldata"), + type = 7, + color = "black" + ) + ), + column(width = 6, + uiOutput("ac_button"), + uiOutput("expcalcu")) + ), + br(), + br(), + fluidRow(column(width = 11, + uiOutput("scenario_opts"))), + br(), + fluidRow(column(width = 11, + uiOutput("credit_loss"))), + fluidRow(column(width = 11, + uiOutput("dlmanager"))) ) \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309 b/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309 index 04e88c3..2842199 100644 --- a/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309 +++ b/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309 @@ -12,15 +12,15 @@ "properties": { "source_window_id": "", "Source": "Source", - "cursorPosition": "36,2", - "scrollLine": "0" + "cursorPosition": "140,7", + "scrollLine": "121" }, "folds": "", - "lastKnownWriteTime": 1686047140, + "lastKnownWriteTime": 1686049083, "encoding": "UTF-8", "collab_server": "", "source_window": "", - "last_content_update": 1686047140357, + "last_content_update": 1686049083340, "read_only": false, "read_only_alternatives": [] } \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309-contents b/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309-contents index 565b542..8c768be 100644 --- a/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309-contents +++ b/.Rproj.user/178A6739/sources/session-99529da2/FE6BB309-contents @@ -20,11 +20,12 @@ library(stringr) options(shiny.reactlog = TRUE, appDir = getwd()) +source("mod_basic.R") source("panel.R") -# source("mod_basic.R") + # source("secretary.R") # source("forplumber.R") -# source("modals.R") +source("modals.R") # Adding initial data diff --git a/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26 b/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26 new file mode 100644 index 0000000..50d01f9 --- /dev/null +++ b/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26 @@ -0,0 +1,24 @@ +{ + "id": "FEF97A26", + "path": "~/Projects/LoanRisk/mod_basic.R", + "project_path": "mod_basic.R", + "type": "r_source", + "hash": "1266759796", + "contents": "", + "dirty": false, + "created": 1686047786220.0, + "source_on_save": false, + "relative_order": 3, + "properties": { + "source_window_id": "", + "Source": "Source" + }, + "folds": "", + "lastKnownWriteTime": 1637757473, + "encoding": "UTF-8", + "collab_server": "", + "source_window": "", + "last_content_update": 1637757473, + "read_only": false, + "read_only_alternatives": [] +} \ No newline at end of file diff --git a/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26-contents b/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26-contents new file mode 100644 index 0000000..4bb203f --- /dev/null +++ b/.Rproj.user/178A6739/sources/session-99529da2/FEF97A26-contents @@ -0,0 +1,141 @@ +# Module for a graph + +# Module to show some stats +basicstatUI <- function(id) { + ns <- NS(id) + fluidRow( + h3("No of Assets"), + br(), + p( + "This section shows some basic information about the portfolio." + ), + plotOutput(ns("statplot")), + verbatimTextOutput(ns("balancetext")) + ) +} + +basicstatServer <- function(id, dt) { + moduleServer(id, + function(input, output, session) { + df <- reactive({ + dt %>% + ungroup() %>% + arrange(id, report_date) %>% + group_by(id) %>% + slice_max(report_date, n = 1) %>% + mutate(yr = lubridate::year(origination_date)) + }) + + + output$statplot <- renderPlot({ + req(!is.null(df)) + withProgress(message = "Plotting some graphs", + detail = "Won't take long", + value = 0, + { + setProgress(value = 1, message = "1 of 3..") + + p1 <- + df() %>% + group_by(yr, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot(aes( + x = yr, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + )) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by year", + x = "Year", + y = "#") + + theme_bw() + + theme(legend.position = "none") + setProgress(value = 2, message = "2 of 3..") + + p2 <- + df() %>% + group_by(asset_type, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot(aes( + x = asset_type, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + )) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by asset type", + x = "Asset Type", + y = "#") + + theme_bw() + + theme(legend.position = "none") + + setProgress(value = 3, message = "3 of 3..") + + p3 <- + df() %>% + group_by(customer_type, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot( + aes( + x = customer_type, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + ) + ) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by customer type", + x = "Customer Type", + y = "#") + + theme_bw() + + theme(legend.position = "bottom", + legend.title = element_blank()) + + setProgress(value = 4, message = "Patching..") + + p4 <- p1 / (p2 | p3) + + setProgress(value = 5, message = "Done..") + + }) + + + p4 + + }) + + output$balancetext <- renderText({ + paste0( + "Total assets (no.): ", + length(unique(df()$id)) , + "\n", + "Total balance outstanding: ", + round(sum(df()$balance) / 1000000, 2), + " M" + + ) + }) + + + }) + +} \ No newline at end of file diff --git a/app.R b/app.R index 565b542..8c768be 100644 --- a/app.R +++ b/app.R @@ -20,11 +20,12 @@ library(stringr) options(shiny.reactlog = TRUE, appDir = getwd()) +source("mod_basic.R") source("panel.R") -# source("mod_basic.R") + # source("secretary.R") # source("forplumber.R") -# source("modals.R") +source("modals.R") # Adding initial data diff --git a/mod_basic.R b/mod_basic.R new file mode 100644 index 0000000..4bb203f --- /dev/null +++ b/mod_basic.R @@ -0,0 +1,141 @@ +# Module for a graph + +# Module to show some stats +basicstatUI <- function(id) { + ns <- NS(id) + fluidRow( + h3("No of Assets"), + br(), + p( + "This section shows some basic information about the portfolio." + ), + plotOutput(ns("statplot")), + verbatimTextOutput(ns("balancetext")) + ) +} + +basicstatServer <- function(id, dt) { + moduleServer(id, + function(input, output, session) { + df <- reactive({ + dt %>% + ungroup() %>% + arrange(id, report_date) %>% + group_by(id) %>% + slice_max(report_date, n = 1) %>% + mutate(yr = lubridate::year(origination_date)) + }) + + + output$statplot <- renderPlot({ + req(!is.null(df)) + withProgress(message = "Plotting some graphs", + detail = "Won't take long", + value = 0, + { + setProgress(value = 1, message = "1 of 3..") + + p1 <- + df() %>% + group_by(yr, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot(aes( + x = yr, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + )) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by year", + x = "Year", + y = "#") + + theme_bw() + + theme(legend.position = "none") + setProgress(value = 2, message = "2 of 3..") + + p2 <- + df() %>% + group_by(asset_type, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot(aes( + x = asset_type, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + )) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by asset type", + x = "Asset Type", + y = "#") + + theme_bw() + + theme(legend.position = "none") + + setProgress(value = 3, message = "3 of 3..") + + p3 <- + df() %>% + group_by(customer_type, loan_status) %>% + summarise(no_of_loans = n()) %>% + ungroup() %>% + ggplot( + aes( + x = customer_type, + y = no_of_loans, + label = no_of_loans, + fill = factor( + loan_status, + levels = c("0", "1"), + labels = c("Good", "Bad") + ) + ) + ) + + geom_col(position = "dodge") + + geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) + + labs(title = "No. of assets by customer type", + x = "Customer Type", + y = "#") + + theme_bw() + + theme(legend.position = "bottom", + legend.title = element_blank()) + + setProgress(value = 4, message = "Patching..") + + p4 <- p1 / (p2 | p3) + + setProgress(value = 5, message = "Done..") + + }) + + + p4 + + }) + + output$balancetext <- renderText({ + paste0( + "Total assets (no.): ", + length(unique(df()$id)) , + "\n", + "Total balance outstanding: ", + round(sum(df()$balance) / 1000000, 2), + " M" + + ) + }) + + + }) + +} \ No newline at end of file diff --git a/modals.R b/modals.R new file mode 100644 index 0000000..2fb119d --- /dev/null +++ b/modals.R @@ -0,0 +1,104 @@ + +# 2nd modal +modal2<-modalDialog( + title = "File formatting", + fluidRow( + + column( + width = 6, + p("Transactions file"), + varSelectInput("transid","Select id column", NULL), + varSelectInput("reportdate","Select report date column", NULL), + varSelectInput("origindate","Select origin date column", NULL), + varSelectInput("maturitydate","Select maturity date column", NULL), + varSelectInput("assettype","Select asset classifier column", NULL), + varSelectInput("customertype","Select customer classifier column", NULL), + varSelectInput("otherfact","Select any other classifier column", NULL, multiple = T) + ), + column( + width = 6, + p("Transactions file"), + varSelectInput("bureauscore","Select bureau score column", NULL), + varSelectInput("balance","Select asset balance column", NULL), + varSelectInput("status","Select loan status column", NULL), + varSelectInput("defaultflag","Select default flag column", NULL), + radioButtons("dateformat","Select date format",choices = c("dmy","ymd"), selected = "ymd", inline = T), + p("Collateral File"), + varSelectInput("collateralid","Select id column", NULL), + varSelectInput("collateralvalue","Select Collateral value column", NULL) + ) + ), + easyClose = F, + size = "l", + footer = tagList( + actionButton("confirmupload", "Confirm"), + actionButton("closemodal2","Close") + ) +) + +modal1<- modalDialog( + title = "Upload files", + fluidRow( + column( + width = 6, + p("Add Transaction file"), + fileInput("transaction", "Choose CSV File of transactions", + multiple = F, + accept = c("text/csv", + "text/comma-separated-values,text/plain", + ".csv")), + p("Add Collateral file"), + fileInput("collaterals", "Choose CSV File of collaterals", + multiple = F, + accept = c("text/csv", + "text/comma-separated-values,text/plain", + ".csv")), + checkboxInput("header", "Header", TRUE), + fluidRow( + column( + width = 6, + radioButtons("sep", "Separator", + choices = c(Comma = ",", + Semicolon = ";", + Tab = "\t"), + selected = ",") + ), + column( + width = 6, + radioButtons("quote", "Quote", + choices = c(None = "", + "Double Quote" = '"', + "Single Quote" = "'"), + selected = '"') + ) + ) + ), + column( + width = 6, + fluidRow( + tags$div( + "Following columns are required in the transaction file", + tags$ul( + tags$li("A unique identifier of asset (id)"), + tags$li("Date on which last report was generated. The last reporting date of all the assets must be same."), + tags$li("Origination Date"), + tags$li("Maturity Date"), + tags$li("Default flag i.e. whether previously defaulted"), + tags$li("Bureau/internal credit score"), + tags$li("Current loan status (1 is bad and 0 is good)"), + tags$li("Asset classification"), + tags$li("Customer classification"), + tags$li("Asset balance") + ), + "The collateral file should contain present value of the collateral/possible price of sales of the asset/sales of hypothecated asset etc." + ) + ) + ) + ), + easyClose = F, + size = "l", + footer = tagList( + actionButton("uploadfiles", "Proceed"), + actionButton("closemodal1","Close") + ) +) \ No newline at end of file diff --git a/panel.R b/panel.R index b05bbf0..542a99f 100644 --- a/panel.R +++ b/panel.R @@ -48,67 +48,67 @@ panel1<- ") ) ), - ))#, - # tags$hr(), - # fluidRow( - # column( - # width = 6, - # fluidRow( - # h3("Data"), - # br(), - # p("This section shows the data (uploaded or inbuilt sample).") - # ), - # br(), - # withSpinner(dataTableOutput("up_data"),type = 7, - # color = "black"), - # br(), - # actionButton("uploadnew", "Upload New Data", class="btn-light") - # ), - # column(width = 6, - # br(), - # basicstatUI("nofloans")) - # - # ), - # br(), - # br(), - # fluidRow( - # column( - # width = 6, - # h3("With Macroeconomic data"), - # p( - # "In this section, we add macroeconomic data. Please select the country of the asset and click on fetch button. If download fails, click again or try after sometime. Please note that GDP data has been discontinued by IMF recently. Hence, using Industrial output temporarily." - # ), - # column( - # width = 6, - # selectInput( - # "country", - # "Country", - # selected = "India", - # choices = countries$Country - # ) - # ), - # column( - # width = 6, - # br(), - # actionButton("fetchimf", "Fetch IMF data", class = "glow") - # ), - # withSpinner( - # dataTableOutput("fulldata"), - # type = 7, - # color = "black" - # ) - # ), - # column(width = 6, - # uiOutput("ac_button"), - # uiOutput("expcalcu")) - # ), - # br(), - # br(), - # fluidRow(column(width = 11, - # uiOutput("scenario_opts"))), - # br(), - # fluidRow(column(width = 11, - # uiOutput("credit_loss"))), - # fluidRow(column(width = 11, - # uiOutput("dlmanager"))) + )), + tags$hr(), + fluidRow( + column( + width = 6, + fluidRow( + h3("Data"), + br(), + p("This section shows the data (uploaded or inbuilt sample).") + ), + br(), + withSpinner(dataTableOutput("up_data"),type = 7, + color = "black"), + br(), + actionButton("uploadnew", "Upload New Data", class="btn-light") + ), + column(width = 6, + br(), + basicstatUI("nofloans")) + + ), + br(), + br(), + fluidRow( + column( + width = 6, + h3("With Macroeconomic data"), + p( + "In this section, we add macroeconomic data. Please select the country of the asset and click on fetch button. If download fails, click again or try after sometime. Please note that GDP data has been discontinued by IMF recently. Hence, using Industrial output temporarily." + ), + column( + width = 6, + selectInput( + "country", + "Country", + selected = "India", + choices = countries$Country + ) + ), + column( + width = 6, + br(), + actionButton("fetchimf", "Fetch IMF data", class = "glow") + ), + withSpinner( + dataTableOutput("fulldata"), + type = 7, + color = "black" + ) + ), + column(width = 6, + uiOutput("ac_button"), + uiOutput("expcalcu")) + ), + br(), + br(), + fluidRow(column(width = 11, + uiOutput("scenario_opts"))), + br(), + fluidRow(column(width = 11, + uiOutput("credit_loss"))), + fluidRow(column(width = 11, + uiOutput("dlmanager"))) ) \ No newline at end of file