部署闪亮的应用程序时找不到数据对象
Data object not found when deploying shiny app
我正在开发我的第一个闪亮的应用程序,并且 运行 遇到了一个问题,即用于呈现我的数据 table 的数据没有被 shinyapps.io 获取。
该应用程序在我的控制台中运行良好,但当我部署它时,该应用程序在浏览器中打开并出现错误:未找到对象 'Pitchers',其中 'Pitchers' 是我的数据对象之一。
我发现 说要将您的数据放在应用程序文件夹中的一个文件夹中,但仍然不起作用。
这是我当前的 server.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 进行部署。
如有任何建议,我们将不胜感激!
更新
我采纳了 Colin 的建议并更新了我的代码,但我仍然 运行 遇到同样的问题。
这是我的应用在部署后的样子:
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/")
)
)))))
进入 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"
)
希望对您有所帮助!
哦!我认为这是因为您没有将投手文件加载为 ui.R 脚本的一部分。因此,当您定义年份输入时,它无法找到 Pitchers$year。您可以尝试阅读 shinyUI()
调用上方 ui 脚本中的投手文件吗?
一些关于闪亮数据注入的东西:如果你的数据不会定期改变,你不需要你的数据摄取是反应性的。只需将它放在 shinyServer()
调用之前服务器文件的最开头即可。当您部署应用程序时,这部分代码只会获得 运行 一次,因此它是执行任何 library()
调用、数据注入或不依赖于用户的预处理内容的好地方以任何方式。您可以像在常规 R 中一样使用 read.csv()
,有时保存数据帧的二进制也很好,因为 read.csv 可能有点慢。
如果要更改(例如更新的 csv 将定期放入文件中),则将 read.csv()
调用放在 shinyServer()
调用下方,但同样不需要有反应。
希望对您有所帮助!
借鉴之前的回答:
我同意问题是对象 "Pitchers" 需要在 shinyServer 和 shinyUI 中定义。创建名为 "global.R" 的第三个文件可能是解决此问题的最佳方法,因为这样您只需要读取数据库一次(这比免费的 shinyapps.io 服务器更快更好)。有关创建具有全局范围的变量的详细信息,请参阅 https://shiny.rstudio.com/articles/scoping.html。
我建议你必须在 UI 和服务器函数中包含库 DT,我是这样解决的:
library(DT)
shinyUI(....)
也在shinyServer(...)
之前
我遇到了同样的问题。我希望这些信息对你有用。 :)
我正在开发我的第一个闪亮的应用程序,并且 运行 遇到了一个问题,即用于呈现我的数据 table 的数据没有被 shinyapps.io 获取。
该应用程序在我的控制台中运行良好,但当我部署它时,该应用程序在浏览器中打开并出现错误:未找到对象 'Pitchers',其中 'Pitchers' 是我的数据对象之一。
我发现
这是我当前的 server.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 进行部署。
如有任何建议,我们将不胜感激!
更新
我采纳了 Colin 的建议并更新了我的代码,但我仍然 运行 遇到同样的问题。
这是我的应用在部署后的样子:
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/")
)
)))))
进入 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"
)
希望对您有所帮助!
哦!我认为这是因为您没有将投手文件加载为 ui.R 脚本的一部分。因此,当您定义年份输入时,它无法找到 Pitchers$year。您可以尝试阅读 shinyUI()
调用上方 ui 脚本中的投手文件吗?
一些关于闪亮数据注入的东西:如果你的数据不会定期改变,你不需要你的数据摄取是反应性的。只需将它放在 shinyServer()
调用之前服务器文件的最开头即可。当您部署应用程序时,这部分代码只会获得 运行 一次,因此它是执行任何 library()
调用、数据注入或不依赖于用户的预处理内容的好地方以任何方式。您可以像在常规 R 中一样使用 read.csv()
,有时保存数据帧的二进制也很好,因为 read.csv 可能有点慢。
如果要更改(例如更新的 csv 将定期放入文件中),则将 read.csv()
调用放在 shinyServer()
调用下方,但同样不需要有反应。
希望对您有所帮助!
借鉴之前的回答:
我同意问题是对象 "Pitchers" 需要在 shinyServer 和 shinyUI 中定义。创建名为 "global.R" 的第三个文件可能是解决此问题的最佳方法,因为这样您只需要读取数据库一次(这比免费的 shinyapps.io 服务器更快更好)。有关创建具有全局范围的变量的详细信息,请参阅 https://shiny.rstudio.com/articles/scoping.html。
我建议你必须在 UI 和服务器函数中包含库 DT,我是这样解决的:
library(DT)
shinyUI(....)
也在shinyServer(...)
我遇到了同样的问题。我希望这些信息对你有用。 :)