为什么一直显示:
"类型'关闭的对象'不是子集表格"
library(shiny)
ui<-fluidPage(
numericInput(inputId = "S0",label = "price at time 0",value = 20),
numericInput(inputId = "k",label = "exercise price",value = 20),
numericInput(inputId = "N",label = "steps",value = 50),
numericInput(inputId = "r",label = "riskfree rate",value = 0.03),
numericInput(inputId = "t",label = "time to exercise data",value = 1),
plotOutput(outputId = "v")
)
server<-function(input,output)
{
for(i in 2:246) # 246 days data
zsp$r[i]=log(zsp$ZSP[i]/zsp$ZSP[i-1])
bzc=sd(zsp$r)
sigma=bzc*sqrt(246)#volatility
ve<-reactive({vector(mode="numeric",length=input$N+1)})
u<-reactive({exp(sigma*sqrt(input$t/input$N)) })
d<-reactive({1/u})
p<-reactive({(exp(input$r*input$t/input$N)-d)/(u-d)})
ve<-reactive({rep(input$N,9)})
for(i in 1:51) #origin vector for options at time 1
{
ve[i]<-reactive({max(0,(input$S0*d^(i-1)*u^(input$N-i+1)-input$k))})
}
for(m in 50:1)
{
for(j in 1:m)
{
ve[j]<-reactive({(ve[j]*p+ve[j+1]*(1-p))*exp(-input$r*input$t/input$N)})
}
}
output$v<-renderPlot({plot(ve())})
}
shinyApp(ui=ui,server=server)
答案 0 :(得分:0)
你的for loops
必须被包裹在被动状态。所有服务器端代码都必须包含在shiny
函数中,例如reactive
,renderPlot
等。
这部分代码应为:
output$v<-renderPlot({
for(i in 1:51) #origin vector for options at time 1
{
ve[i]<-max(0,(input$S0*d^(i-1)*u^(input$N-i+1)-input$k))
}
for(m in 50:1)
{
for(j in 1:m)
{
ve[j]<-(ve[j]*p+ve[j+1]*(1-p))*exp(-input$r*input$t/input$N)
}
}
plot(ve())
})