Last active
June 26, 2018 23:30
-
-
Save erikgregorywebb/4eb3625835c481c5b38694f1e8f3ca7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://unboxed-analytics.shinyapps.io/the-potential-for-growth/ | |
# http://unboxed-analytics.com/life-hacking/fundamentals-of-investing/ | |
library(FinCal) | |
library(ggplot2) | |
library(tidyr) | |
library(shinythemes) | |
library(scales) | |
# Define UI for application that draws a histogram | |
ui <- shinyUI(fluidPage(theme = shinytheme("spacelab"), | |
# Application title | |
titlePanel("The Potential for Growth: Two Case Studies"), | |
p('An interactive tool to accompany the ', a(href = 'http://unboxed-analytics.com/life-hacking/fundamentals-of-investing/', '"Fundamentals of Investing"'), 'post.'), | |
# Sidebar with a slider input for number of bins | |
sidebarLayout( | |
sidebarPanel( | |
numericInput( | |
inputId = "rate", | |
label = "Yearly Rate of Return", | |
value = .06, | |
min = .01, | |
max = .15, | |
step = .01 | |
), | |
p("Represented as a decimal."), | |
p(".06 = 6%"), | |
numericInput( | |
inputId = "years", | |
label = "Number of Years", | |
value = 43, | |
min = 3, | |
max = 50, | |
step = 1 | |
), | |
numericInput( | |
inputId = "pv", | |
label = "Initial Outlay", | |
value = 2000, | |
min = 1000, | |
max = 100000, | |
step = 1000. | |
), | |
numericInput( | |
inputId = "pmt", | |
label = "Monthly Contribution", | |
value = 0, | |
min = 0, | |
max = 10000, | |
step = 100 | |
), | |
numericInput( | |
inputId = "type", | |
label = "Payment Type", | |
value = 0, | |
min = 0, | |
max = 1, | |
step = 1 | |
), | |
p("Indicates if payments occur at the end of each period (Payment Type = 0) or if payments occur at the beginning of each period (Payment Type = 1).") | |
), | |
mainPanel(plotOutput("finPlot"), | |
p("The grey line represents PRINCIPAL and the blue line represents PRINCIPAL + INTEREST."), | |
p("Case 1: Suppose you have $2,000 to invest. You use that money to purchase a low-cost, tax-efficient, diversified mutual fund offering an approximate yearly return of 6%."), | |
p("You purchase the mutual fund on your 22nd birthday and don’t check your account until your 65th birthday on the day of retirement. After 43 years, you would have over $26,200! Your money has doubled four times."), | |
p("Case 2: Suppose again you have $2,000 today to invest. You purchase shares of the same mutual and now, in addition to the initial investment, purchase $100 of additional shares each month. [Adjust the 'Monthly Contribution' sidebar parameter]"), | |
p("How much would you have at the end of 43 years? Over $268,400! You have passively created wealth through the market.")) | |
) | |
)) | |
# Define server logic required to draw a histogram | |
server <- shinyServer(function(input, output) { | |
output$finPlot <- renderPlot({ | |
# processing | |
total <- fv(r = input$rate/12, n = 0:(12*input$years), pv = -input$pv, pmt = -input$pmt, type = input$type) | |
principal <- seq(input$pv, input$pv + (input$years*12)*(input$pmt+.000000001), input$pmt + .000000001) | |
interest <- total - principal | |
df <- data.frame(period = 0:(12*input$years), total, principal) | |
# plotting | |
ggplot(df, aes(x = period)) + | |
geom_line(aes(y = total), col = "blue", size = .85) + | |
geom_line(aes(y = principal), col = "black", size = .85) + | |
labs(x = "Period", | |
y = "") + | |
scale_y_continuous(labels = dollar) + | |
theme(legend.position="bottom") + | |
theme(legend.title = element_blank()) + | |
theme_minimal() | |
}) | |
}) | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment