【问题标题】:Shiny App R - Reactive variables not responding to change in Input variablesShiny App R - 反应变量不响应输入变量的变化
【发布时间】:2015-06-25 22:43:30
【问题描述】:

我正在制作一个应用程序,可以预测 NFL 跑卫在 1800 多个冲球码数后的冲球尝试次数和冲球码数。我将滑块输入用于# of rushing 码和尝试,它通过 lm() 和 predict() 并返回对明年尝试和 rush 码的估计(我知道这根本不是一个很好的预测器,但这只是制作闪亮应用程序的练习)。这是我的 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

服务器.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")

        )
))

我遇到的问题是我似乎无法同时输出情节(明年的估计值以红色绘制,与跑卫 > 1800 冲球码的历史表现对比)和明年估计冲球码数和尝试的文本同时。我可以根据我放置这些语句的位置来显示其中一个。如果我把

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

在 output$newPlot 之外(但仍在函数(输入,输出)行内),我可以显示该图,并且随着输入的更改,明年的估计点会发生变化,但我收到错误消息

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

有人可以帮忙吗?

【问题讨论】:

    标签: r input output shiny


    【解决方案1】:

    我更改了 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 被重写为使用输入变量在渲染函数内部调用的普通函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多