rachelong
rachelong

Reputation: 11

How to display a text when dataset is empty in ploty

does anyone know if it is possible to display a text in renderPlotly instead error message when the dataset is empty? I tried print for the time being, but it didnt work.

if (nrow(result) == 0){
      print("Sorry, ther is no such flat type in the select town")
    }
    else{
      p<-ggplot(result, (aes(year,total_price,colour = town)))


      p <- p + geom_line() +
        ggtitle("Total Resale Price By Town") +
        labs(x = "Year", y = "Total Resale Price ($)")+
        scale_y_continuous(labels = dollar) +
        theme(plot.title = element_text(size = 12,hjust = 0.5))

      # without tooltip settings, "town" appears twice...
      p <- ggplotly(p, tooltip = c("x","y","colour"))
      p
    }

Here is the error : no applicable method for 'plotly_build' applied to an object of class "character"

Upvotes: 0

Views: 149

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

You can use validate:

  output$plot <- renderPlotly({
    validate(
      need(nrow(result) != 0, 'Sorry, the dataset is empty')
    )
    p <- ggplot(......) + ......
    ggplotly(p)
  })

Upvotes: 2

Related Questions