2016-11-28 1 views
0

Warum zeigt er es hält:Mit glänzendem einem Optionspreis App zu erstellen, warum es „nicht subsettable ist Objekt des Typs‚Schließung‘“ hält zeigt

„Objekt vom Typ‚Schließung‘nicht subsettable ist“

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

Da u & d Reaktive sie verwenden mit(), wenn sie setzen in Ausdrücke wie u() & d() bei der Zuweisung von d & p. Dies ist, was ich sofort bemerkt habe, vielleicht gibt es ein paar zusätzliche Probleme ... –

+0

Vielen Dank für den Rat, ich habe die Änderung und immer noch Fehler, vielleicht ist es die Schleife? – Lee

Antwort

0

Ihre for loops muss in einem reaktiven gewickelt werden. Alle serverseitigen Code muss wie reactive in shiny Funktionen gewickelt werden, renderPlot usw.

Dieser Teil des Codes sollte lauten:

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

}) 
+0

Vielen Dank! Ich verstehe endlich wie es funktioniert – Lee

Verwandte Themen