在闪亮的Web应用程序中显示错误而不是绘图 [英] Display error instead of plot in Shiny web app

查看:11
本文介绍了在闪亮的Web应用程序中显示错误而不是绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的Web应用程序,上面有很多情节。每个绘图都有自己的SQL查询来获取数据。其中一个查询可能返回数据不足的表。如果发生这种情况,则我希望在绘图所在的选项卡中显示一条文本消息。

服务器.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)            
        }

    })    
})

ui.R

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"))
            )
        )            
    )                   
))

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

推荐答案

查看validate,注意示例取自Write error messages for your UI with validate

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())
    })

  }
))

这篇关于在闪亮的Web应用程序中显示错误而不是绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆