我们的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
中返回字符串"对不起没有数据"(这是有道理的)。有没有更好的方法可以做到这一点?
编辑:我认为我们不能有条件地从UI渲染整个our_graph
,因为图形是否呈现取决于get_our_data()
返回的内容,这在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
,以便与用户进行清晰和描述性的通信。