启动Shiny应用程序时,我收到“ filter_impl中的错误:结果的长度必须为5,而不是0”。这发生在本地以及在托管时。该错误只是暂时的-半秒钟后消失。而且对于应用程序的功能来说似乎不是问题。但是看到错误消息很烦人!任何想法如何解决这个问题?
以下是可重现的示例(从较长的脚本中删除):
require(ggplot2)
require(dplyr)
require(shiny)
##Section 1 ____________________________________________________
#load your data or create a data table as follows:
schools <- structure(list(school_name = c("Berkeley Terrace", "Grove Street School",
"Madison At Chancellor South", "Mt. Vernon Avenue School", "Thurgood Marshall School"
), district_name = c("Irvington Township", "Irvington Township",
"Irvington Township", "Irvington Township", "Irvington Township"
), Percent_Black_Students = c(0.755364806866953, 0.903292181069959,
0.813953488372093, 0.857913669064748, 0.824644549763033), Percent_Black_Teachers = c(1,
1, 1, 1, 1), Percent_Latinx_Students = c(0.233905579399142, 0.088477366255144,
0.176079734219269, 0.12589928057554, 0.163507109004739), Percent_Latinx_Teachers = c(0,
0, 0, 0, 0), Percent_White_Students = c(0, 0, 0, 0.00539568345323741,
0.0023696682464455), Percent_White_Teachers = c(0, 0, 0, 0, 0
)), class = "data.frame", row.names = c(NA, -5L))
##Section 2 ____________________________________________________
#set up the user interface
ui = shinyUI(
fluidPage( #allows layout to fill browser window
titlePanel("Why does the error flash and then go away?"),
sidebarPanel( #designates location of following items
htmlOutput("school_selector")
),
mainPanel(
plotOutput("plot1") #put plot item in main area
# Output: HTML table with requested number of observations ----
# tableOutput("view")
)
) )
##Section 3 ____________________________________________________
#server controls what is displayed by the user interface
server = shinyServer(function(input, output) {
output$school_selector = renderUI({#creates County select box object called in ui
data_available <- schools
#creates a reactive list of available x based on the y selection made
selectInput(inputId = "school", #name of input
label = "School:", #label displayed in ui
choices = unique(data_available), #calls list of available counties
selected = "Madison At Chancellor South")
})
output$plot1 = renderPlot({ #creates a the plot to go in the mainPanel
focal.school <- schools %>% filter(school_name == input$school)
ggplot(focal.school, aes(x=input$school, y=Percent_Black_Students)) +
geom_point()
})
})#close the shinyServer
shinyApp(ui = ui, server = server)
另外:我已经将输入选择设置为级联到主应用程序中的其他输入选择器。也就是说,首先选择地区,然后将在下拉列表中填充学校...然后用户将选择一所学校,等等。
答案 0 :(得分:1)
发生这种情况是因为启动应用程序时,需要花费一些时间来处理您的renderUI()
函数。在这段时间内,input$school
为空,因此错误。您可以使用validate()
和need()
进行更正。阅读here,了解有关Shiny中验证和常规错误处理的更多信息。更正的代码:
require(ggplot2)
require(dplyr)
require(shiny)
##Section 1 ____________________________________________________
#load your data or create a data table as follows:
schools <- structure(list(school_name = c("Berkeley Terrace", "Grove Street School",
"Madison At Chancellor South", "Mt. Vernon Avenue School", "Thurgood Marshall School"
), district_name = c("Irvington Township", "Irvington Township",
"Irvington Township", "Irvington Township", "Irvington Township"
), Percent_Black_Students = c(0.755364806866953, 0.903292181069959,
0.813953488372093, 0.857913669064748, 0.824644549763033), Percent_Black_Teachers = c(1,
1, 1, 1, 1), Percent_Latinx_Students = c(0.233905579399142, 0.088477366255144,
0.176079734219269, 0.12589928057554, 0.163507109004739), Percent_Latinx_Teachers = c(0,
0, 0, 0, 0), Percent_White_Students = c(0, 0, 0, 0.00539568345323741,
0.0023696682464455), Percent_White_Teachers = c(0, 0, 0, 0, 0
)), class = "data.frame", row.names = c(NA, -5L))
##Section 2 ____________________________________________________
#set up the user interface
ui = shinyUI(
fluidPage( #allows layout to fill browser window
titlePanel("Why does the error flash and then go away?"),
sidebarPanel( #designates location of following items
htmlOutput("school_selector")
),
mainPanel(
plotOutput("plot1") #put plot item in main area
# Output: HTML table with requested number of observations ----
# tableOutput("view")
)
) )
##Section 3 ____________________________________________________
#server controls what is displayed by the user interface
server = shinyServer(function(input, output) {
output$school_selector = renderUI({#creates County select box object called in ui
data_available <- schools
#creates a reactive list of available x based on the y selection made
selectInput(inputId = "school", #name of input
label = "School:", #label displayed in ui
choices = unique(data_available), #calls list of available counties
selected = "Madison At Chancellor South")
})
output$plot1 = renderPlot({ #creates a the plot to go in the mainPanel
# add validate here
validate(
need(input$school != "", "No school selected") # display custom message in need
)
focal.school <- schools %>% filter(school_name == input$school)
ggplot(focal.school, aes(x=input$school, y=Percent_Black_Students)) +
geom_point()
})
})#close the shinyServer
shinyApp(ui = ui, server = server)
优良作法是始终在闪亮的代码中包含必要的validate()
语句。