简体   繁体   English

闪亮的R-ggplotly-当数据集不返回任何信息时,显示自定义消息而不是空图

[英]Shiny R - ggplotly - Show custom message instead of empty plot when the dataset does not return any information

The shiny application I'm working on is displaying graphs using ggplotly. 我正在使用的闪亮应用程序正在使用ggplotly显示图形。 In an instance when the resulting dataset is empty, a blank plot is being displayed, as below. 在结果数据集为空的情况下,将显示空白图,如下所示。 在此处输入图片说明

Is it possible to show a custom message such as "No data exists with the selected inputs" instead of an empty plot 是否可以显示自定义消息,例如“所选输入不存在数据”而不是空白图

With the help of validate, need I am able to display the error message when the user does not select input in the front-end - 借助validate的帮助,当用户未在前端选择输入时,我将能够显示错误消息-

validate(
      need(input$category, 'No data exists, please select a Category')
      )



I would like to display a custom message similarly in the server side when the final dataset is empty, I've tried below codes so far as per the help from google. 当最终数据集为空时,我想在服务器端类似地显示自定义消息,根据google的帮助,我尝试了以下代码。 These codes aren't giving any errors, but the error message is being print by default. 这些代码没有给出任何错误,但是默认情况下会显示错误消息。

validate(
    need(nrow(dataset() > 0), 'Message here')
    )

or 要么

validate(
    need(is.null(dataset), 'Message here')
    )



I am plotting with the help of below code, where g() is my final dataset after filter applied basis user input - 我在以下代码的帮助下进行绘图,其中g()是应用了过滤器的用户输入后的最终数据集-

output$plot1 <- renderPlotly({
    p <- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_point(alpha=0.4)
    ggplotly(p)

  })

I am new to Shiny and R, any help is appreciated. 我是Shiny和R的新手,感谢您的帮助。

Thanks. 谢谢。

Something like this? 像这样吗

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("plot")
)

server <- function(input, output) {

  g <- reactive({NULL})

  output$plot <- renderPlotly({
    validate(
      # Old Code
      need(nrow(g() > 0), 'No data exists, please select a Category')
      # With parentheses fix
      need(nrow(g()) > 0, 'No data exists, please select a Category')
    )
    plot_ly(g(), x = ~mpg, y = ~wt)
  })
}

shinyApp(ui, server)

我推荐shinycssloaders软件包及其withSpinner()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM