部署闪亮的应用

时间:2015-04-30 11:01:16

标签: r shiny

我正在处理我的第一个闪亮的应用程序,并且遇到了一个问题,即shinyapps.io没有收集用于呈现我的数据表的数据。

应用程序在我的控制台中运行良好,但是当我部署它时,应用程序会在浏览器中打开,并显示错误:object' Pitchers'没找到,投手'是我的数据对象之一。

我发现one suggestion表示将您的数据放在应用程序文件夹中的文件夹中,但仍无效。

这是我当前的服务器.R代码:

shinyServer(function(input, output) {

  Pitchers <- read.csv("data/Pitchers_Edge.csv", header=TRUE, check.names = FALSE)
  Batters <- read.csv("data/Batters_Edge.csv", header=TRUE, check.names = FALSE)

  output$table1 <- renderDataTable({

    if (input$Year != "All"){
      Pitchers <- Pitchers[Pitchers$Year == input$Year,]
    } 
    Pitchers 
  })

  output$table2 <- renderDataTable({

    if (input$Year != "All"){
      Batters <- Batters[Batters$Year == input$Year,]
    } 
    Batters
  })

})

这是ui.R代码:

shinyUI(fluidPage(
  titlePanel('Edge%: 2010-Present'),
    fluidRow(
      column(12,

                    p("Provides current and historical data on the percentage of pitches thrown to different parts of the strike zone by pitchers and to batters"),
                    p("Created and maintained by Bill Petti",
                      a("(@BillPetti)", href = "https://twitter.com/billpetti")),
                    p("Data last updated",Sys.time()))
      ),
  fluidRow(
    column(5, 
           selectInput("Year", 
                       "Year:", 
                       c("All", 
                         unique(as.character(Pitchers$Year)))))
    ),
  mainPanel(
      tabsetPanel(
        tabPanel("Pitchers", dataTableOutput(outputId = 'table1')),
        tabPanel("Batters", dataTableOutput(outputId = 'table2')),
        tabPanel("About", 
             br(), h1("About Edge%"), 
             br(), p("A few years ago, Jeff Zimmerman and I created a metric to represent how often a pitcher threw to the edges of the strike zone compared to the heart of the strike zone. The result was Edge%. The metric has evolved to include separate metrics for different edges (upper, lower, etc.). In the image below, the brown shaded areas represent the horizontal edges of the strike zone, the blue the top, and the green the bottom edges. You will notice the horizontal edges differ by batter handedness, given how umpires actually call balls and strikes."), 
             br(), img( src = "edge_image.png", height = 350, width = 700)), 
            br(), p("Edge% is useful in a number of contexts. For example, we know that as pitchers age they lose velocity and therefore need to avoid throwing to the heart of the plate to be successful. Edge% provides a quick look at who is adjusting to lower velocity and who isn't. It can also be used to see how pitchers are adjusting to hitters as they age (i.e. as hitters improve, pitchers may avoid the heart of the plate more, or as hitters decline they may begin challenge them more."), 
            br(), p("For more information on Edge%, check out these articles:", 
            br(), br(), a("Introduction to Edge%", href = "http://www.fangraphs.com/blogs/the-difference-pitching-on-the-edge-makes/"), 
            br(), br(), a("Collection of Articles Using and Expanding on Edge%", href = "http://www.fangraphs.com/blogs/category/edge/"),
            br(), br(), a("Most Recent Version", href = "http://www.hardballtimes.com/expanding-the-edges-of-the-strike-zone/") 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   )
                 )
)
)
)

我正在从Windows PC部署。

非常感谢任何建议!

更新

我接受了科林的建议并更新了我的代码,但我仍遇到同样的问题。

这是我的应用在部署时应该是什么样子:

http://i.stack.imgur.com/idmvC.jpg

以下是我在本地或网络上发布和查看时会发生什么:

http://i.stack.imgur.com/ikBrD.jpg

不仅仅是对象丢失了,但我可以看到应用程序的其余部分。整个应用程序都消失了。

这是我更新的代码:

library(shiny)

shinyServer(function(input, output) {

  Pitchers <- reactive({read.csv("data/Pitchers_Edge.csv", header=TRUE, check.names = FALSE)})
  Batters <- reactive({read.csv("data/Batters_Edge.csv", header=TRUE, check.names = FALSE)})

  output$table1 <- renderDataTable({
    Pitchers <- Pitchers()
    if (input$Year != "All"){
      Pitchers <- Pitchers[Pitchers$Year == input$Year,]
    } 
    subset(Pitchers, Pitchers$'# of total pitches'>= input$pitch_total) 
  })

  output$table2 <- renderDataTable({
    Batters <- Batters()
    if (input$Year != "All"){
      Batters <- Batters[Batters$Year == input$Year,]
    } 
    subset(Batters, Batters$'# of total pitches'>= input$pitch_total)
  })

})

shinyUI(fluidPage(
  titlePanel('Edge%: 2010-Present'),
  fluidRow(
    column(12,
           p("Provides current and historical data on the percentage of pitches thrown to different parts of the strike zone by pitchers and to batters"),
           p("Created and maintained by Bill Petti",
             a("(@BillPetti)", href = "https://twitter.com/billpetti")),
           p("Data last updated",Sys.time()))
  ), 
    sidebarLayout(
    sidebarPanel(selectInput("Year", 
                                    "Year:", 
                                    c("All", 
                                      unique(as.character(Pitchers$Year)))),
                 numericInput("pitch_total", 
                             label = "Minimum # of Pitches:",
                             value = 300)
  )
  ,
  mainPanel(
    tabsetPanel(
      tabPanel("Pitchers", dataTableOutput(outputId = 'table1')),
      tabPanel("Batters", dataTableOutput(outputId = 'table2')),
      tabPanel("About", 
               br(), h1("About Edge%"), 
               br(), p("A few years ago, Jeff Zimmerman and I created a metric to represent how often a pitcher threw to the edges of the strike zone compared to the heart of the strike zone. The result was Edge%. The metric has evolved to include separate metrics for different edges (upper, lower, etc.). In the image below, the brown shaded areas represent the horizontal edges of the strike zone, the blue the top, and the green the bottom edges. You will notice the horizontal edges differ by batter handedness, given how umpires actually call balls and strikes."), 
               br(), img( src = "edge_image.png", height = 350, width = 700), 
      br(), p("Edge% is useful in a number of contexts. For example, we know that as pitchers age they lose velocity and therefore need to avoid throwing to the heart of the plate to be successful. Edge% provides a quick look at who is adjusting to lower velocity and who isn't. It can also be used to see how pitchers are adjusting to hitters as they age (i.e. as hitters improve, pitchers may avoid the heart of the plate more, or as hitters decline they may begin challenge them more."), 
      br(), p("For more information on Edge%, check out these articles:"), 
              br(), a("Introduction to Edge%", href = "http://www.fangraphs.com/blogs/the-difference-pitching-on-the-edge-makes/"), 
              br(), br(), a("Collection of Articles Using and Expanding on Edge%", href = "http://www.fangraphs.com/blogs/category/edge/"),
              br(), br(), a("Most Recent Version", href = "http://www.hardballtimes.com/expanding-the-edges-of-the-strike-zone/") 
      )
    )))))

4 个答案:

答案 0 :(得分:6)

哦!我认为你没有加载投手文件作为ui.R脚本的一部分。因此,当您定义年份输入时,它无法找到Pitchers $ year。您可以尝试在shinyUI()电话上方的ui脚本中阅读投手文件吗?

关于数据injest闪亮的一些内容:如果您的数据不会定期更改,您不需要数据摄取被动反应。只需在shinyServer()调用之前将其放在服务器文件的最开头即可。当您部署应用程序时,该部分代码只运行一次,因此它是进行任何library()调用,数据注入或预处理不依赖于用户的任何方式的东西的好地方。您可以像使用常规R一样使用read.csv(),有时也可以保存数据帧的二进制文件,因为read.csv可能有点慢。

如果它会改变(就像定期将更新的csv放入文件中那么),那么将read.csv()调用放在shinyServer()调用之下但是不应该再次被动。

希望有所帮助!

答案 1 :(得分:1)

进入renderDataTable函数后,它无法识别Pitchers是什么,因为它是在函数外部定义的。但是,如果将Pitchers设置为反应元素,则可以在函数内调用它。因此,将设置变量,然后将其作为渲染函数内的函数调用。

Pitchers<- reactive({read.csv("Your Stuff")})

output$table1 <- renderDataTable(
Pitch<-Pitchers()

"Then call all your if statements and such on the function using Pitch"
)

希望有所帮助!

答案 2 :(得分:0)

以前的回答抄袭:

我同意问题是需要在shinyServer和shinyUI中定义对象“Pitchers”。创建名为“global.R”的第三个文件可能是解决此问题的最佳方法,因为这样您只需要一次读取数据库(这对于免费的shinyapps.io服务器来说更快更好)。有关创建具有全局范围的变量的详细信息,请参阅https://shiny.rstudio.com/articles/scoping.html

答案 3 :(得分:0)

我建议你必须在 UI 和服务器函数中包含库 DT,我是这样解决的:

library(DT)

shinyUI(....)

也在shinyServer(...)之前

我遇到了同样的问题。我希望这些信息对你有用。 :)