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)
})
}
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
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With