Hi,
I have a calculation result I want to render into a table ( through a data frame) but it doesn't work i don't why. I googled and haven't find anything similar. maybe somebody can help?
library(shiny)
Define UI ----
ui <- fluidPage(
titlePanel("Basic widgets"),
fluidRow(
# column(3,
# h3("Buttons"),
# actionButton("action", "Action"),
# br(),
# br(),
# submitButton("Submit")),
# column(3,
# h3("Single checkbox"),
# checkboxInput("checkbox", "Choice A", value = TRUE)),
column(3,selectInput("Fixed", "Fixed:",
c("30 year" = "30",
"15 year" = "15",
"10 year" = "10")),
tableOutput("data")),
# column(3,
# dateInput("date",
# h3("Date input"),
# value = "2014-01-01"))
),
fluidRow(
# column(3,
# dateRangeInput("dates", h3("Date range"))),
#
# column(3,
# fileInput("file", h3("File input"))),
column(3,
numericInput("housePrice",
h3("Housing Price"),
value = 100000)),
column(3,
numericInput("percentageDown",
h3("Percentage Down"),
value = 0.2),min=0,max=1) ,
column(3, numericInput("mortgageYield", h3("Mortgage Yield"), value=0.05, min = 0, max = 0.1, step = NA,
width = NULL)),
),
fluidRow(
column(3, h3("Mortgage Payment"), verbatimTextOutput("mortgagepayment"))),
fluidRow(column(3,h3("Amortization Table"),verbatimTextOutput("AmortizationTable")))
#DT::dataTableOutput("AmortizationTable")
)
Define server logic ----
server <- function(input, output) {
output$mortgagepayment <- renderText({
# prevent this block from trying to calculate when other fields are
# empty or invalid
req(input$housePrice, input$percentageDown, input$mortgageYield, input$Fixed)
# message("calculate!") # just advises when this block fires
housePrice = input$housePrice
downPayment = input$percentageDown
mortgageYield = input$mortgageYield/12
mortgageAmount = housePrice*(1-downPayment)
years = as.numeric(input$Fixed)
mortgagePayment = (mortgageAmountmortgageYield)/(1-1/(1+mortgageYield)^(12years))
})
# now calculate the amortization table (Principal + interest at each stage )
output$AmortizationTable <- renderTable({
# prevent this block from trying to calculate when other fields are
# empty or invalid
req(input$housePrice, input$percentageDown, input$mortgageYield, input$Fixed)
# message("calculate!") # just advises when this block fires
years = as.numeric(input$Fixed)
housePrice = input$housePrice
downPayment = input$percentageDown
mortgageYield = input$mortgageYield/12
mortgageAmount = housePrice*(1-downPayment)
mortgagePayment = (mortgageAmountmortgageYield)/(1-1/(1+mortgageYield)^(12years))
years = as.numeric(input$Fixed)
numofPayments =seq(1:(years12))
principalOutStanding = (1+mortgageYield)^(numofPayments-1)
t= (1+mortgageYield)^(numofPayments-1)
t=(t-1)/(1-1/(1+mortgageYield)^(12years))
principalOutStanding = mortgageAmount * (principalOutStanding - t )
interestpaid = rep(0, years12)
interestpaid [2:length(interestpaid)]= principalOutStanding[1:(length(principalOutStanding)-1)]mortgageYield
res<-data.frame(month=seq(1:years12),payment = rep(mortgagePayment,years12),Principal_outstanding=principalOutStanding, interest_paid = interestpaid )
})
}
Run the app ----
shinyApp(ui = ui, server = server)
I want the table to display the res dataframe