Monday, February 15, 2021

R Shiny App: Annuity & Amortization Schedule Calculator

Annuity & Amortization Schedule Calculator (Shiny App)

Introduction

In December, I decided I wanted to try and see if I could figure out how to build a Shiny App that would allow me to either calculate a Loan Amoritzation schedule (e.g. auto, mortgage) or would allow me to determine the future value of an investment. I looked around online to see if anyone else had tried this and the closest example I could find was done was a loan amortization calculator by Guangming Lang of MasterR.org (https://masterr.org/r/calculate-mortgage-payment-schedule/). I decided to use some of his code as a starting point for the loan portion of of the Shiny application we’ll build today. I then added the annuity portion and the pie chart. If you want to learn more about the calculations I used for the annuity portion of the code here is a a reference I used to come up with the calculations.

The Shiny Web Application

Before we get started, I just want to mention that I’m not going to explain how I’m going the build the app, rather I’m just going to share the code. I covered the basics of building the code on RStudio.cloud and hosting a Shiny Application on ShinyApps.io back in Lesson 9. If you want more information on how to setup and host a Shiny App, please go do that lesson first before you attempt this example.

Build the Application

I like to use a single RScript file format that combines the user interface and server information into a single file called app.R. Here’s what my finished code looks like.

# HEADER --------------------------------------------
#
# Author: RDataGuy
# Copyright (c) RDataGuy, 2019
# Email:  rdataguy@gmail.com
# 
# Date: 2020-12-20
#
# Shiny App Name: Annuity & Amortization Schedule Calculator
#
# Shiny App Description: Generate a table showing future value of an an
# investment (e.g. loan, annuity)
#
# Notes:
# I found original code on: https://masterr.org/r/calculate-mortgage-payment-schedule/
# and then decided to make modifications to it to include not just loan payments but also
# annuity growth calculations.
#
# This is a single file Shiny App.
#

# LOAD LIBRARIES ------------------------------------
library('shiny')
library('tidyverse')
library('lubridate')
library('plotly')

options(scipen = 999)

# USER INTERFACE ------------------------------------
# Define UI for application that calculates a table showing the future value of
# an investment.

ui <- fluidPage(
    
    # Application title
    titlePanel("Annuity & Amortization Schedule Calculator"),
    
    # Sidebar with inputs for the investment calculator
    sidebarLayout(
        sidebarPanel(
            
            radioButtons("type",
                         "Investment Type?",
                         choices = c('Loan','Annuity'),
                         selected = 'Loan'),
            
            numericInput("amount",
                         "Initial Amount ($):",
                         10000),
            
            numericInput("APR",
                         "Interest Rate (%):",
                         2.5),
            
            numericInput("term",
                         "Term (# of months)",
                         72),
            
            dateInput("start_date",
                      "Start Date",
                      today(),
                      format = "yyyy-mm-dd"),
            
            numericInput("down_pmt",
                         "Initial Down Payment (if Loan)",
                         2000),
            
            numericInput("dep",
                         "Monthly Deposit (if Annuity)",
                         0),
          
            br(), # adds a blank line
            actionButton("do", "Generate!"),
            width = 3
        ),
        
        # Show a table of investment using the Server's output called "schedule"
        mainPanel(
            splitLayout( tableOutput("schedule"),
                         plotlyOutput("pie_chart")
            ),
            width = 9
        )
    )
)

# SERVER INFORMATION --------------------------------
# Define server logic required to display table & pie chart
server <- function(input, output) {
    
    amortize = function(loan_amt, down_pmt, start_date, term, APR) {
        # calculate simple monthly rate 
        monthly_rate = APR / 12
        # start_date = as.Date(start_date, "%Y-%m-%d")
        
        # calculate (constant) contractual monthly payment amount
        # derived from the present value formula for annuities
        # https://en.wikipedia.org/wiki/Annuity
        r = (1 + monthly_rate) ^ term - 1
        pay = (loan_amt-down_pmt) * monthly_rate * (r + 1) / r
        
        # initialize output variables
        interest = principal = payment = balance = vector("numeric", term)
        
        # calc amortization schedule
        outstanding_principal = loan_amt - down_pmt
        for (i in 1:term) {
          
            intr = outstanding_principal * monthly_rate
            if(outstanding_principal < payment[i]){
                prnp = outstanding_principal
                outstanding_principal = 0
                final = TRUE
                
            }else{
              
                prnp = pay - intr
                outstanding_principal = outstanding_principal - prnp
                final = FALSE
            }
            
            interest[i]  = round(intr,2)
            principal[i] = round(prnp,2)
            payment[i] = round((prnp + intr),2)
            balance[i] = round(outstanding_principal,2)
        }
        
        data.frame('Month' = 1:term, 'Date' = ymd(start_date) %m+% months(1:term), 'Interest' = interest, 'Principal' = principal, 'Payment' = payment, 'Balance' = balance) 
    }
    
    annuity = function(initial_amt, start_date, dep, term, APR) {
        # calculate simple monthly rate 
        monthly_rate = APR / 12
        
        # calculate (constant) contractual monthly payment amount
        # derived from the present value formula for annuities
        # https://en.wikipedia.org/wiki/Annuity

        deposit = dep
        
        # initialize output variables
        interest = deposit = balance = vector("numeric", term)
        
        # calc amortization schedule
        outstanding_principal = initial_amt
        for (i in 1:term) {
            intr = outstanding_principal * monthly_rate
            prnp = dep + intr
            outstanding_principal = outstanding_principal + prnp
            
            interest[i]  = round(intr,2)
            deposit[i] = round(dep,2)
            balance[i] = round(outstanding_principal,2)
        }
        
        data.frame('Month' = 1:term, 'Date' = seq(as.Date(start_date), length.out = term, by="months"), 'Interest' = interest, 'Deposit' = deposit, 'Balance' = balance) 
    }

    # Use an action button as an event to generate the list of random numbers
    schedule_data <- eventReactive(input$do, {  
        
        # Determine if loan or annuity and then calcluate
        if (input$type == "Loan"){
          
            sched <- amortize(loan_amt = input$amount, down_pmt = input$down_pmt, start_date = input$start_date, term = input$term, APR = input$APR/100)
            sched$Date <- format(sched$Date,'%Y-%m-%d')
            return(sched)
            
        }else{
          
            sched <- annuity(initial_amt = input$amount, start_date = input$start_date, dep = input$dep, term = input$term, APR = input$APR/100)
            sched$Date <- format(sched$Date,'%Y-%m-%d')
            return(sched)
        }
    })
    
    pie_data <- eventReactive(input$do, {  
        
        # Determine if loan or annuity and then calcluate
        if (input$type == "Loan"){
          
            sched <- amortize(loan_amt = input$amount, down_pmt = input$down_pmt, start_date = input$start_date, term = input$term, APR = input$APR/100)
            sched$Date <- format(sched$Date,'%Y-%m-%d')
            name = c("Principal", "Down Payment","Interest")
            start_bal = input$amount
            final_bal = sched$Balance[input$term]
            principal = sum(sched$Principal)
            interest = sum(sched$Interest)
            values = c(principal , input$down_pmt, interest )
            dat = data.frame(cbind(name, values))
            dat$values <- as.numeric(dat$values)
            total <- sum(dat$values)
            dat$percents <- 100*(dat$values / total)
            dat = dat %>% dplyr::arrange(desc(percents)) 
            
            return(dat)

        }else{
          
            sched <- annuity(initial_amt = input$amount, start_date = input$start_date, dep = input$dep, term = input$term, APR = input$APR/100)
            sched$Date <- format(sched$Date,'%Y-%m-%d')
            name = c("Initial Amount", "Contributions", "Growth")
            start_bal = input$amount
            final_bal = sched$Balance[nrow(sched)]
            contrib = sum(sched$Deposit)
            growth = final_bal - start_bal - contrib
            values = c(input$amount, contrib , growth )
            dat = data.frame(cbind(name, values))
            dat$values <- as.numeric(dat$values)
            total <- sum(dat$values)
            dat$percents <- 100*(dat$values / total)
            dat = dat %>% dplyr::arrange((percents)) 
            return(dat)
        }
    })
    
    # Output the schedule only AFTER the "Generate!" button is pressed
    output$schedule <- renderTable({
        schedule_data()
    }, rownames = FALSE, colnames = TRUE)
    
    output$pie_chart <- renderPlotly({
        # store rendered pie_data() as a tibble
        data <- tibble(pie_data())
        fig <- plot_ly(data, labels = ~name, values = ~values, type = 'pie')
        fig <- fig %>% layout(title = paste0(input$type, ' Details', sep=""),
                              xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                              yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
    })
}

# RUN APPLICATION ----------------------------------
shinyApp(ui = ui, server = server)

Test our Web Application

Okay! Let’s test it! To test your application, click on the “Run App” button. Run App Button

You should see a web page pop up. Try clicking on the “Generate!” button.

If you Generated a Loan Schedule, you should see output similar to this: Loan Example

If you generated an Annuity Schedule, you should see output similar to this: Annuity Example

Deploy our Web Application

Alright! Time to deploy this baby! Rather than re-hash the steps to deploy this application, check out my previous example on how to setup a Shinyapps.io account and deploy your application. Skip down to “Step #5: Deploy our Web Application”

After you deploy your application the browser window should pop up with your newly deployed web application. Here’s a link to my final Annuity & Amortization Schedule Calculator App for you check out.

Conclusion

Overall, I was pretty happy with this Shiny App, it does exactly what I planned it would do. One idea I had for a possible improvement was to create a way to export the schedule & chart to a PDF document. I had developed the code but ran into issues with creating the PDF while hosting the app on Shinyapps.io and I can’t afford to run RStudio Connect where you get that capability. Apparently, you cannot render PDF documents because you cannot write temporary files to Shinyapps.io. If you figure out a way to do this, I would be interested in seeing your solution!

I really enjoyed building this application, I hope you have some fun building it as well.

Have fun coding!

RDataGuy

Get the complete code for this app on GitHub (Click link)!

Sunday, May 17, 2020

Lesson 10: Send Email or Text by R-Script using RDCOMClient

Lesson 10 - Send Emails by R-Script using RDCOMClient

Have you ever wanted to get a text message or an email when your script finished? I have many times! Well, look no farther because if you use Microsoft Outlook desktop application, here is a solution that will work perfectly! This solution uses an R Package called RDCOMClient maintained by http://www.omegahat.net. RDCOMClient is a very useful package because it can be used to interact with COM Objects associated with Microsoft Office applications such as Outlook, Word, Excel, Access, and Power Point. If you've written any Visual Basic scripts, you've likely used COM Objects at some point. Basically, this means you can programatically interact with these applications from R, opening up a whole list of possiblities for you! It's a very useful package and I use it daily!

STEP #1 - The first thing you'll need to do is download the lastest Java SE 8 Runtime Environment (JRE) for your Operating System (OS). You might be asking "Why Java SE 8?" Well, the simple answer is because that's what I know has worked for myself and my friends. Make sure if you're running 64-bit R that you download the 64-bit version of the JRE. You can find the JRE for your OS here.

STEP #2 - After you've successfully installed the JRE for your OS, the next thing you'll need to do is install the rJava library package. I've found the easiest way to do this is right from your console

install.packages("rJava")
library(rJava)

Honestly, rJava can be a bit of a hassle to get setup, but if you run into issues, go checkout stackoverflow.com. I used this site to help troubleshoot my issues with my installation.

STEP #3 - Once you have the JRE and a working rJava library, the next thing you need to do is get a copy of the the RDCOM Client library package. You can get it from one of two places. Either from the CRAN or you can navigate here and download the package version that matches your R version. Personally, I prefer downloading from the Omegahat site and then install it from the zip file. The name of the file will be "RDCOMClient_0.93-0.zip" for R 3.5 or RDCOMClient_0.94-0.zip for R 4.0. For a version that will work with an older version of R, navigate to the "old" directory.

Assuming you download the file from Omegahat, installation can be done by using the "Tools" menu "Install Packages..."



Then you'll want to browse to the location where you saved the RDCOMclient zip-file, select it, and click "Install".


STEP #4 - Once RDCOMClient has finished installing, you can run this code! Use an email address or use a phone number followed by carrier text message domains for sending text message(ATT is @mms.att.net, Sprint is @messaging.sprintpcs.com, and Verizon is @vtext.com, etc.)

# HEADER --------------------------------------------
#
# Author: Your Name
# Copyright (c) Your Name, 2019
# Email:  your_email@your_domain.com
# 
# Date: Today's Date
#
# Script Name: Email / Text Message with Microsoft Outlook
#
# Script Description: Create and send an email using Microsoft Outlook and RDCOMClient
#
#
# Notes:
# RDCOMClient is used to interact with COM Objects associated with Microsoft Office applications
# such as Outlook, Word, Excel, Access, and Powerpoint.
#

# Load Libraries
library(rJava)
library(RDCOMClient)

# Script to send text/email message
# access the Outlook application
OutApp <- COMCreate("Outlook.Application")

# create an email
outMail = OutApp$CreateItem(0)

# configure  email parameter
#carrier suffixes for sending to TEXT @mms.att.net, OR @messaging.sprintpcs.com, OR # @vtext.com
outMail[["To"]] = "your_email@someplace.com"

outMail[["subject"]] = "Testing! - An Email Message from R"

outMail[["body"]] = "Hey, it worked!!"

# unremark the next line to add attachment
# path_to_file = "some_file.pdf"
# outMail[["Attachments"]]$Add(path_to_file)

# unremark to change "from" (send from secondary mailbox)
# outMail[["SentOnBehalfOfName"]] = "your_secondary@someplace.com"

# send email or text
cat("Email message sent.\n\n")
outMail$Send()

RDCOMClient can do a whole lot more than just send emails, I encourage you to check it out and learn more about it!

Enjoy!
Have fun coding!

RdataGuy

Download the code for this lesson below!