2015-04-19 68 views
2

我正在制作一款应用程序,它可以预测NFL跑回的尝试次数以及在1800+以上的紧急码后的紧急码数。我使用滑块输入作为冲突场地和尝试的次数,这些输入通过lm()和predict()运行,并返回下一年度的尝试和抢运场地的估算值(我知道它根本不是一个很好的预测变量,但这只是一个制作Shiny应用程序的练习)。这里是我的excel文件中的数据,然后是代码。Shiny App R - 反应变量对输入变量中的变化没有反应

Player Yr. Team Attempts Att.Next.Yr Yards Yards.Next.Yr YPC YPC.Next.Yr 
1 Adrian Peterson 2012 MIN 348 279 2097 1266 6.0 4.5 
2 Chris Johnson 2009 TEN 358 316 2006 1364 5.6 4.3 
3 LaDainian Tomlinson 2006 SD 348 315 1815 1474 5.2 4.7 
4 Shaun Alexander 2005 SEA 370 252 1880 896 5.1 3.6 
5 Tiki Barber 2005 NYG 357 327 1860 1662 5.2 5.1 
6 Jamal Lewis 2003 BAL 387 235 2066 1006 5.3 4.3 
7 Ahman Green 2003 GB 355 259 1883 1163 5.3 4.5 
8 Ricky Williams 2002 MIA 383 392 1853 1372 4.8 3.5 
9 Terrell Davis 1998 DEN 392 67 2008 211 5.1 3.1 
10 Jamal Anderson 1998 ATL 410 19 1846 59 4.5 3.1 
11 Barry Sanders 1997 DET 335 343 2053 1491 6.1 4.3 
12 Barry Sanders 1994 DET 331 314 1883 1500 5.7 4.8 
13 Eric Dickerson 1986 RAM 404 60 1821 277 4.5 4.6 
14 Eric Dickerson 1984 RAM 379 292 2105 1234 5.6 4.2 
15 Eric Dickerson 1983 RAM 390 379 1808 2105 4.6 5.6 
16 Earl Campbell 1980 HOU 373 361 1934 1376 5.2 3.8 
17 Walter Payton 1977 CHI 339 333 1852 1395 5.5 4.2 
18 O.J. Simpson 1975 BUF 329 290 1817 1503 5.5 5.2 
19 O.J. Simpson 1973 BUF 332 270 2003 1125 6.0 4.2 
20 Jim Brown 1963 CLE 291 280 1863 1446 6.4 5.2 

Server.R

​​

UI.R

# ui.R 

shinyUI(pageWithSidebar(
     headerPanel("Rushing Projections"), 
     sidebarPanel(
       sliderInput('Yards', 'How many yards rushed for this season', 
          value=1700, min=1500, max=2500, step=25,), 
       sliderInput('Attempts', 'How many attempts this season', 
          value=350, min=250, max=450, step=5,), 
       submitButton('Submit') 

     ), 
     mainPanel(
       plotOutput('newPlot'), 
       h3('Predicted rushing yards next year: '), 
       verbatimTextOutput("renderYds"), 
       h3('Predict attempts next year: '), 
       verbatimTextOutput("renderAtt") 

     ) 
)) 

我遇到的问题是我似乎不能输出这两个图(明年估计在绘制红色与跑步背景的历史表现相比> 1800码)以及明年预计冲刺场地和尝试的文字同时出现。根据我在哪里陈述这些陈述,我可以得到一个或另一个显示。如果我把输出$ newPlot的

output$renderYds <- renderPrint({predictYds}) 
output$renderAtt <- renderPrint({predictAtt}) 

之外(但仍在里面的功能(输入,输出))线改变输入时,我可以得到的情节出现了,明年估计变更点但我得到的文本错误信息为

object 'predictYds' not found'object 'predictAtt' not found。如果我把这两行写入function(input, output)行(正如我在上面的代码中),那么这两个文本数字显示正确的值,但不会生成绘图。

任何人都可以帮忙吗?

回答

0

我改变了Server.R的结构,现在它工作。

shinyServer(function(input, output) { 

      predictYds <- function(Y, A){ 
       test <- data.frame(Y, A) 
       names(test) <- c("Yards", "Attempts") 
       predict(fitYds, test) 
      } 

      predictAtt <- function(Y, A){ 
       test <- data.frame(Y, A) 
       names(test) <- c("Yards", "Attempts") 
       predict(fitAtt, test) 
      } 

      output$newPlot <- renderPlot({ 

         newYards <- predictYds(input$Yards, input$Attempts) 
         newAttempts <- predictAtt(input$Yards, input$Attempts) 
         qplot(data=data, x=Attempts, y=Yards) + 
          geom_point(aes(x=newAttempts, y=newYards, color="Estimate")) 

        }) 

      output$renderYds <- renderPrint({predictYds(input$Yards, input$Attempts)}) 

      output$renderAtt <- renderPrint({predictAtt(input$Yards, input$Attempts)}) 
     } 
) 

基本上PredictYds和PredictAtt被重写为使用输入变量在渲染函数内调用的正常函数。