我心血来潮写了一个可以简易计算储蓄险的function
require(ggplot2)
#year <- 20 #你要缴纳几年
#restyear <- 40 #缴完后还可以活多久
#yearget <- 12 #一年得到多少钱
#yearpay <- 10 #一年缴多少钱
#rate <- 0.02 #你认为的市场利率
npvplot <- function(yearpay=10,yearget=12,rate=0.02,year=20,restyear=40
){
npv<-function(yearpay=10,yearget=12,rate=0.02,year=20,restyear=40){
benefit<- sum( yearget/ ( (1+rate)^seq(year-1, year+restyear-1) ) )
cost <- sum ( yearpay/ ( (1+rate)^seq(0, year-1 ) ) )
return( benefit-cost )
}
benefit <- vector("numeric",length = 50)
for(i in 1:50){
benefit[i]<- npv(yearpay=10,yearget=12,rate=0.02,year=20,i)
}
restlife <- c(1:50)
dat <- data.frame(restlife,benefit)
qplot(restlife,benefit,geom = "line")
}
npvplot()
这个function会画出 缴费完后每多活一年,NPV的净值会如何变化的图
但是我想把它做成shiny
但是不知道为何没又error但是画出来的图形都是一条水平线
library(plotly)
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel(title=h4("简易储蓄险NPV计算()", align="center")),
actionButton("go", "Go"),
numericInput("yearpay", "一年要缴多少钱(万)", 10),
numericInput("year", "要缴几年", 20),
numericInput("yearget", "期满后一年可得的报酬(万)", 12),
numericInput("restyear", "期满后预期可活几年", 40),
numericInput("rate", "你认为的通货膨胀率", 0.02),
plotOutput("plot")
)
server <- function(input, output) {
npvplot <- function(yearpay,yearget,rate,year,restyear){
npv<-function(yearpay,yearget,rate,year,restyear){
benefit<- sum( yearget/ ( (1+rate)^seq(year-1, year+restyear-1) ) )
cost <- sum ( yearpay/ ( (1+rate)^seq(0, year-1 ) ) )
return( benefit-cost )
}
benefit <- vector("numeric",length = 50)
for(i in 1:50){
benefit[i]<- npv(yearpay,yearget,rate,year,i)
}
restlife <- c(1:50)
dat <- data.frame(restlife,benefit)
return(dat )
#qplot(restlife,benefit,geom = "line")
}
value<- eventReactive(input$go, {
npvplot(input$yearpay,input$year,input$yearget,input$restyear,input$rate)
})
output$plot <- renderPlot({
ggplot(value(),aes(x=restlife,y=benefit))+geom_line()
})
}
shinyApp(ui, server)
想请问各位大神是哪里出错了
附上改正 https://pastebin.com/K6NA8DUL
目前是local可以动 但是无法发布 出现 error