首页 > 解决方案 > 创建一个带有标题的空 Plotly 图在 R 闪亮的应用程序中不显示标题但完全空白

问题描述

我试图在数据可用时显示一个图表,并且标题为“图表不可用”的临时图表,但无法正常工作。我在这里阅读了一个线程,其中包含有关标题居中的空图的有用信息,但无法解决闪亮应用程序中的问题。有什么想法吗?提前致谢!

这是我试图在闪亮服务器中使用的代码。

  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)
          })
  }
 

标签: rshinyplotly

解决方案


plotly您可以使用在绘图不可用时validate输出警告消息,而不是在内部处理警告消息:UI

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
    })
})

在此处输入图像描述


推荐阅读