使用闪亮来创建一个期权定价应用程序,为什么它一直显示"对象类型'关闭'不是子集表格"

时间:2016-11-28 04:15:43

标签: shiny closures

为什么一直显示:

  

"类型'关闭的对象'不是子集表格"

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)

1 个答案:

答案 0 :(得分:0)

你的for loops必须被包裹在被动状态。所有服务器端代码都必须包含在shiny函数中,例如reactiverenderPlot等。

这部分代码应为:

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())

})