7

The shiny application I'm working on is displaying graphs using ggplotly. In an instance when the resulting dataset is empty, a blank plot is being displayed, as below. enter image description here

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(
      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. 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 -

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.

Thanks.

Prashanth kumar
  • 949
  • 3
  • 10
  • 32

2 Answers2

8

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)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Hi @Pork Chop, your suggestion isn't giving any error in the beginning, but when there is no data it is showing the error as - "Error: data is too long", any suggestions on that? – Prashanth kumar Oct 03 '17 at 18:43
  • please print exact error, that error has nothing to do with my example – Pork Chop Oct 03 '17 at 19:34
0

I recommend shinycssloaders package and its withSpinner() function.