当 api 调用在 Shiny 中返回为空时显示错误消息?

Display error message when api call comes back empty in Shiny?

我有一个连接到市政府警察数据的交互式可视化 API。

选择输入的某些组合时,我的API呼叫回到空,我收到一个讨厌的红色错误消息(因为我的绘图输入不可用)。

谁能告诉我如何根据 "there are no incidents matching your selection, please try again" 显示信息更丰富的错误消息?我希望此错误消息显示为 showNotification 并且我的 ggplot 不呈现。

下面是我正在做的一个非常精简的版本。 Note how when a combination like "AVONDALE" and "CHEMICAL IRRITANT" is selected, the chart renders, whereas when a combination like "ENGLISH WOODS" and "TASER-BEANBAG-PEPPERBALL-40MM FOAM" is selected, an error message is returned.我想通过 showNotification 警报解决此错误消息。

请注意,这使用了 Socrata API,因此必须安装并加载包 RSocrata。

install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)

# Define UI for application that draws a histogram
ui <- fluidPage(
  navbarPage("Example", 
             theme = shinytheme("united"),
             tabPanel("Plot",
                      sidebarLayout(
                        sidebarPanel(

                          # neighborhood selector
                          selectizeInput("neighbSelect", 
                                         "Neighborhoods:", 
                                         choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS"), 
                                         multiple = FALSE)),

                          # incident description selector
                          selectizeInput("incSelect", 
                                         "Incident Type:", 
                                         choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"), 
                                         multiple = FALSE))
                        ),

                        # Output plot
                        mainPanel(
                          plotlyOutput("plot")
                        )
                      )
             )

# Define server logic
server <- function(input, output) {
  forceInput <- reactive({
    forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))
  })

# Render plot
  output$plot <- renderPlotly({
    ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
      geom_histogram(stat = "count")
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

非常感谢您提供的任何帮助!

我将举一个使用 shinyalert 库的示例来显示弹出窗口。这里我添加了样本选择TEST表示没有数据:

#install.packages("RSocrata")
library(shiny)
library(reshape2)
library(dplyr)
library(plotly)
library(shinythemes)
library(tibble)
library(RSocrata)
library(shinyalert)

# Define UI for application that draws a histogram
ui <- fluidPage(
  useShinyalert(),
  navbarPage("Example", 
             theme = shinytheme("united"),
             tabPanel("Plot",
                      sidebarLayout(
                        sidebarPanel(

                          # neighborhood selector
                          selectizeInput("neighbSelect", 
                                         "Neighborhoods:", 
                                         choices = c("AVONDALE", "CLIFTON", "ENGLISH WOODS","TEST"), 
                                         multiple = FALSE)),

                        # incident description selector
                        selectizeInput("incSelect", 
                                       "Incident Type:", 
                                       choices = c("CHEMICAL IRRITANT", "TASER-BEANBAG-PEPPERBALL-40MM FOAM"), 
                                       multiple = FALSE))
             ),

             # Output plot
             mainPanel(
               plotlyOutput("plot")
             )
  )
)

# Define server logic
server <- function(input, output,session) {

  forceInput <- reactive({
    forceInput <- read.socrata(paste0("https://data.cincinnati-oh.gov/resource/e2va-wsic.json?$where=sna_neighborhood= '", input$neighbSelect, "' AND incident_description= '", input$incSelect, "'"))

    if(nrow(forceInput)==0){
      shinyalert("Oops!", "No data returned", type = "error")
      forceInput <- NULL
    }
    forceInput
  })

  # Render plot
  output$plot <- renderPlotly({
    req(forceInput())
    ggplot(data = forceInput(), aes(x = sna_neighborhood)) +
      geom_histogram(stat = "count")
  })
}

# Run the application 
shinyApp(ui = ui, server = server)