![](/img/trans.png)
[英]Show a message instead of/within plot while data is loading in R Shiny
[英]Shiny R - ggplotly - Show custom message instead of empty plot when the dataset does not return any information
我正在使用的閃亮應用程序正在使用ggplotly顯示圖形。 在結果數據集為空的情況下,將顯示空白圖,如下所示。
是否可以顯示自定義消息,例如“所選輸入不存在數據”而不是空白圖
借助validate的幫助,當用戶未在前端選擇輸入時,我將能夠顯示錯誤消息-
validate(
need(input$category, 'No data exists, please select a Category')
)
當最終數據集為空時,我想在服務器端類似地顯示自定義消息,根據google的幫助,我嘗試了以下代碼。 這些代碼沒有給出任何錯誤,但是默認情況下會顯示錯誤消息。
validate(
need(nrow(dataset() > 0), 'Message here')
)
要么
validate(
need(is.null(dataset), 'Message here')
)
我在以下代碼的幫助下進行繪圖,其中g()是應用了過濾器的用戶輸入后的最終數據集-
output$plot1 <- renderPlotly({
p <- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_point(alpha=0.4)
ggplotly(p)
})
我是Shiny和R的新手,感謝您的幫助。
謝謝。
像這樣嗎
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
g <- reactive({NULL})
output$plot <- renderPlotly({
validate(
# Old Code
need(nrow(g() > 0), 'No data exists, please select a Category')
# With parentheses fix
need(nrow(g()) > 0, 'No data exists, please select a Category')
)
plot_ly(g(), x = ~mpg, y = ~wt)
})
}
shinyApp(ui, server)
我推薦shinycssloaders
軟件包及其withSpinner()
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.