Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 部署应用程序时未找到数据对象_R_Shiny - Fatal编程技术网

R 部署应用程序时未找到数据对象

R 部署应用程序时未找到数据对象,r,shiny,R,Shiny,我正在开发我的第一个闪亮应用程序,遇到了一个问题,shinyapps.io没有获取用于呈现我的数据表的数据 该应用程序在我的控制台中运行良好,但当我部署它时,该应用程序会在浏览器中打开,并出现错误:找不到对象“Pitchers”,其中“Pitchers”是我的数据对象之一 我发现它说把你的数据放在应用程序文件夹中的一个文件夹中,但这仍然不起作用 这是我当前的服务器。R代码: shinyServer(function(input, output) { Pitchers <- read.

我正在开发我的第一个闪亮应用程序,遇到了一个问题,shinyapps.io没有获取用于呈现我的数据表的数据

该应用程序在我的控制台中运行良好,但当我部署它时,该应用程序会在浏览器中打开,并出现错误:找不到对象“Pitchers”,其中“Pitchers”是我的数据对象之一

我发现它说把你的数据放在应用程序文件夹中的一个文件夹中,但这仍然不起作用

这是我当前的服务器。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
  })

})
我正在从Windows PC部署

如果您有任何建议,我们将不胜感激

更新

我采纳了科林的建议并更新了代码,但我仍然遇到同样的问题

以下是我的应用在部署时的外观:

以下是我在本地或网络上发布和查看时发生的情况:

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

这是我的更新代码:

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/") 
      )
    )))))
库(闪亮)
shinyServer(功能(输入、输出){

Pitchers一旦进入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"
)

Pitchers噢!我想这是因为您没有将Pitchers文件作为ui.R脚本的一部分加载。因此,当您定义年份输入时,它无法找到Pitchers$year。您可以尝试在
shinyUI()调用上方的ui脚本中读取Pitchers文件吗

关于Shinny中data injest的一些内容:如果您的数据不会定期更改,那么您不需要对数据进行被动接收。只需在调用
shinyServer()
之前将其放在服务器文件的最开头。当您部署应用程序时,这部分代码只运行一次,因此这是执行
库()的好地方
调用、数据输入或预处理的内容在任何方面都不依赖于用户。您可以使用
read.csv()
就像在常规R中一样,有时保存数据帧的二进制文件也很好,因为read.csv可能有点慢

如果要更改(比如定期将更新的csv放入文件中),则将
read.csv()
调用放在
shinyServer()调用下方,但不需要再次进行响应


希望这能有所帮助!

借鉴前面的答案:


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

谢谢,科林!我仍然存在问题--我更新了代码并提供了指向正在发生的事情的图像的链接。似乎这可能是一个更大的问题,因为当我部署到web上时,应用程序似乎真的消失了。确实如此!谢谢,这似乎解决了问题。M如果有人在乎的话,最后的代码是。
Pitchers<- reactive({read.csv("Your Stuff")})

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

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