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

执政官

我有一个包含许多情节的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)            
        }

    })    
})

用户界面

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,请注意该示例取自带有validate的UI的写入错误消息。

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

  }
))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Shiny Web 应用程序中的传单不显示地图

R Shiny Dashboard 中的 Word Cloud 显示在查看窗格而不是应用程序窗口中

无法在R Shiny应用程序中显示模式警报

Shiny 应用程序中未显示 RGL 小部件

在 R Shiny 应用程序中渲染 ggplot2 和绘图对象

我可以通过编程方式控制绘图缩放功能而不必在R Shiny应用程序中重新绘图吗?

如何在Shiny应用程序中将数据框显示为网格而不是表格?

神秘的JavaScript错误,具体取决于R Shiny应用程序中使用的绘图对象名称

通过单击“ Shiny”应用程序中的链接来打开其他“ Shiny”应用程序

为什么Boxplot出现在RStudio中而不是我的Shiny应用程序中?

尝试生成 Shiny Web 应用程序,但遇到 arg 错误问题

运行应用程序时,Shiny的用户界面未显示

错误:R的Shiny应用程序中的“数学函数中的非数值参数”

如何在Shiny应用程序中显示“请在输出中等待提示”

R Shiny应用程序中的传单地图图例不显示颜色

为什么图标没有显示在Shiny应用程序的DT :: datatable中?

如何避免在R Shiny应用程序中显示为HTML标题的代码?

如何使用htmlOutput在R Shiny应用程序中启用语法突出显示

如何在我的R Shiny应用程序中显示使用rcharts创建的图表?

显示未存储在R Shiny应用程序内www文件夹中的PDF文件

如何在R Shiny应用程序中显示嵌入式推文?

plot_ly 在 Shiny 应用程序中不显示图形

在 Shiny 应用程序中创建动态标签集会在 ggploty 上出现错误

Shiny应用程序中的动态方差分析,我的输入错误吗?

r Shiny.io错误中的小叶闪亮应用程序

在 Shiny Web 应用程序输出中获取“范围 0 表”

在R Shiny应用程序中下载ggplot2和绘图对象

如何在Shiny应用程序中将表格和绘图作为参数传递给R Markdown?

缺少通过Plotly在Shiny应用程序中绘制的数据