R Shiny - 在没有图表时在 render Highchart() 中渲染文本字符串

RShiny - Render text string in renderHighchart() when there is no chart

我们的 RShiny 服务器中有如下内容:

output$our_graph <- renderHighchart({
  our_data <- get_our_data() # this is a reactive
  if(nrow(our_data) == 0) {
    return('Sorry no data')
  }

  our_return_graph <- highchart(...stuff)
  return(our_return_graph)
})

目前这会引发错误,因为似乎我们不允许从 renderHighchart 中 return 字符串 'Sorry no data'(这是有道理的)。有更好的方法吗?

编辑: 我认为我们不能有条件地从 UI 渲染整个 our_graph,因为图形是否渲染取决于什么get_our_data()returns,在UI中是没有的。如果可能,我们希望在服务器端处理。

我将这个 if 语句移到渲染函数上,恰好我添加了一个 validate(need( 组合。


data <- reactive({
  our_data <- get_our_data() # this is a reactive
  validate(need(nrow(our_data) == 0, "Please select a data set"))
  our_data
})


output$our_graph <- renderHighchart({
  data() ...
  our_return_graph <- highchart(...stuff)
  return(our_return_graph)
})

来源:https://shiny.rstudio.com/reference/shiny/0.14/validate.html

您可以查看 shinyalert 以了解与用户的清晰描述性交流。