在这里,我想编写一个应用程序来计算债券时间表并将其显示在表格中。但是以下问题总是会出现错误,我不知道如何解决。
错误消息:警告:as.POSIXlt.default中的错误:不知道如何将“ x”转换为类“ POSIXlt”。警告:表示错误:尝试复制“ closure”类型的对象
下面是我的代码。谢谢!!
library(shiny)
library(tidyverse)
library(DT)
library(lubridate)
ui <- fluidPage(
fluidPage(sidebarLayout(
sidebarPanel("This is a Bond Calculator"),
mainPanel("Suppose par value is $100")
)),
dateInput("sd", "Start Date", Sys.Date()),
numericInput("t", "Tenor(in year)", 0.5),
numericInput("r", "Coupon Rate", 0.05),
numericInput("freq", "Coupon Frequency", 10),
numericInput("ytm", "Yield To Maturity",0.05),
#plotOutput("p1"),
tableOutput("dt1")
)
server <- function(input, output, session) {
coupon <- reactiveVal(0)
observeEvent(input$r, coupon(100*(input$r)*(input$t)))
npv <- reactiveVal(0)
calc_npv <- function(){
sum <- 0
for(i in 0:input$freq)
sum <- sum + coupon()/(1+ytm*input$t)^(i*input$t)
npv(sum)
}
cnt <- reactiveVal(0)
observeEvent(input$freq, cnt(seq(1,input$freq)))
temp <- reactiveVal(0) #construct payment dates
observeEvent(input$sd, temp(rep(input$sd,input$freq)))
observeEvent(input$freq, temp(rep(input$sd,input$freq)))
pd <- reactiveVal(0)
observeEvent(pd, pd(month(temp())+cnt()*12)) #add the tenor respectively
observeEvent(cnt, pd(month(temp())+cnt()*12))
payment <- reactiveVal(0) #construct payments
observeEvent(payment(input$freq), payment(c(rep(coupon(),input$freq-1),coupon()+100))) #1 to freq-1 combined with the last payment
df <- reactiveVal(0)
observeEvent(pd, df(tibble(Date = pd(), Payment = payment())))
observeEvent(payment, df(tibble(Date = pd(), Payment = payment())))
#output$p1 <- renderPlot({
#})
output$dt1 <- renderDataTable(df(), options = list(pageLength = 5))
}
shinyApp(ui, server)