在Shiny Web应用程序中显示错误而不是绘图

ark*_*hon 3 r shiny

我有一个包含许多情节的Shiny Web应用程序。每个图都有自己的SQL查询来获取数据。查询之一可能返回一个数据不足的表。如果发生这种情况,那么我想在绘图所在的选项卡中显示一条文本消息。

server.R:

library(shiny)
library(RMySQL)

shinyServer(function(input, output, session) {

    output$lineGraphOne <- renderPlot({

        table <- getDataSomeHowOne()

        if(dim(table[1]) < 3) {            
            error <- paste("Some error message")            
        } else {            
            plot(x = as.Date(table$date), y = table$count)            
        }
    })

    output$lineGraphTwo <- renderPlot({

        table <- getDataSomeHowTwo()

        if(dim(table[1]) < 3) {            
            error <- paste("Some error message")            
        } else {            
            plot(x = as.Date(table$date), y = table$count)            
        }

    })    
})
Run Code Online (Sandbox Code Playgroud)

用户界面

library(shiny)

shinyUI(navbarPage("Title",                   
    tabPanel("Name",                            
        sidebarLayout(            
            mainPanel(
                tabsetPanel(id = "tabs",
                    tabPanel("One", plotOutput("lineGraphOne")),
                    tabPanel("Two", plotOutput("lineGraphTwo"))
                )
            ),            
            sidebarPanel(
                dateInput('queryDate', 'Datum:', value = as.Date("2010-04-09"))
            )
        )            
    )                   
))
Run Code Online (Sandbox Code Playgroud)

如何实现显示错误字符串而不是在相应选项卡中显示图?

Por*_*hop 5

看一下validate,请注意,该示例取自带有validate的UI的Write错误消息。

rm(list = ls())
library(shiny)
runApp(list(
  ui = (fluidPage(

    titlePanel("Validation App"),

    sidebarLayout(
      sidebarPanel(
        selectInput("data", label = "Data set",
                    choices = c("", "mtcars", "faithful", "iris"))
      ),

      # Show a plot of the generated distribution
      mainPanel(
        tableOutput("table"),
        plotOutput("plot")
      )
    )
  )),
  server = function(input, output) {

    data <- reactive({ 
      validate(
        need(input$data != "", "Please select a data set")
      )
      get(input$data, 'package:datasets') 
    })

    output$plot <- renderPlot({
      hist(data()[, 1], col = 'forestgreen', border = 'white')
    })

    output$table <- renderTable({
      head(data())
    })

  }
))
Run Code Online (Sandbox Code Playgroud)