Shiny App R - 不响应输入变量变化的反应变量

时间:2015-04-19 07:25:24

标签: r input output shiny

我正在制作一个应用程序,可以预测NFL在1800多个冲球码的赛季后跑回来的冲刺次数和冲球码数。我使用滑块输入作为冲码和尝试的#,它通过lm()和predict()运行并返回明年尝试和冲码的估计(我知道它根本不是一个很好的预测器,但这只是制作闪亮应用程序的练习。这是我的excel文件中的数据,然后是代码。

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

# server.R

library(UsingR)
library(xlsx)
rawdata <- read.xlsx("RushingYards.xlsx", sheetIndex=1)

data <- rawdata[c(2:21),]
rownames(data) <- NULL

# Att
set.seed(1)
fitAtt <- lm(Att.Next.Yr ~ Yards + Attempts, data)

# Yds
set.seed(1)
fitYds <- lm(Yards.Next.Yr ~ Yards + Attempts, data)

shinyServer(
        function(input, output) {

            output$newPlot <- renderPlot({
                        iYards <- input$Yards
                        iAttempts <- input$Attempts

                        test <- data.frame(iYards,iAttempts)
                        names(test) <- c("Yards", "Attempts")
                        predictAtt <- predict(fitAtt, test)
                        predictYds <- predict(fitYds, test)

                        qplot(data=data, x=Attempts, y=Yards) + 
                            geom_point(aes(x=predictAtt, y=predictYds, color="Estimate"))

                        output$renderYds <- renderPrint({predictYds})

                        output$renderAtt <- renderPrint({predictAtt})

                  })


        }
)

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

        )
))

我遇到的问题是我似乎无法输出情节(明年的估计用红色绘制,以及跑垒的历史表现&gt; 1800冲码)以及明年估计的冲码和尝试的文字同时。我可以根据我放置这些语句的位置来显示一个或另一个。如果我把

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

在输出$ newPlot之外(但仍然在函数(输入,输出)之内)行我可以得到显示的情节,并且随着输入的改变,明年的估计值会发生变化,但是我得到的错误消息< / p> 文本的

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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被重写为使用输入变量在渲染函数内部调用的普通函数。