Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a empty Plotly graph with title is NOT showing title but totatly blank in R shiny app

Tags:

r

shiny

plotly

I'm trying to show one graph when the data is available and temp graph with title "Graph Not available", but couldn't get this works. I read one thread here with helpful information on empty graph with centered title but couldn't solve the issue in shiny app. Any thoughts? Thanks in advance!

Here is the code I was trying to use in the shiny server.

  observeEvent(input$selected_city, {
    if(input$selected_city %in% scenario$place)
    {
  output$Scenario <-
    renderPlotly({
      legendtitle <- list(text=paste0("<b>City: ",test_sc()$place),font=list(size=11, 
      test_sc <- plot_ly(test_sc(), width=700,height=550) 
      test_sc <- add_lines(test_sc,x=~date,y=~incc,color=I('blue'),name='actual',showlegend=F)
    })
  }
  else{
    output$Scenario <-
      renderPlotly({
          p <- plotly_empty(test_sc(),type = "scatter", mode = "markers") %>%
          layout(title = list(
              text = 'Sorry! The graph is not available for this city!',
              yref = "paper",
              xref= "paper",
              x = 0.5,
              y = 0.5
            )
          )
          return(p)
          })
  }
 
like image 981
ponyhd Avatar asked Oct 16 '25 05:10

ponyhd


1 Answers

Instead of handling the warning message inside plotly, you could use validate to output a warning message in the UI when the plot isn't available :

observeEvent(input$selected_city, {
    output$Scenario <-
    renderPlotly({
      validate(need(input$selected_city %in% scenario$place, "Sorry! The graph is not available for this city!"))
      ... # the plotly code
    })
})

enter image description here

like image 72
Waldi Avatar answered Oct 17 '25 19:10

Waldi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!