我想做一個錯誤處理,當有資料時顯示繪圖圖表,當資料為空時,只顯示一個白色圖表,而不在 UI 上顯示錯誤。這是我從原始代碼中提取的代碼。運行app.R后,資料為空時UI會出現錯誤。
我也使用過 try catch 函式嘗試洗掉錯誤但失敗了。任何建議將不勝感激。
# Define UI for app that draws a histogram ----
library(ggplot2)
library(stringr)
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotlyOutput("toolbar")
)
)
server <- function(input, output) {
data == data.frame()
if (!is.na(data)){
print("no tool_install info!!")
proc_bars <- reactiveValues(plot = NULL)
print(proc_bars)
output$toolbar <- renderPlotly({
tryCatch(
expr = {print(plotly::ggplotly(proc_bars$plot))},
error = function(e){""}
)
})
}}
shinyApp(ui, server)
uj5u.com熱心網友回復:
也許您可以根據您的用例進行調整。
library(ggplot2)
library(stringr)
library(plotly)
NoDataPlotly <- function(){
df <- data.frame()
p <- ggplot(df) geom_point() xlim(0, 10) ylim(0, 10)
annotate("text", x=3.9, y=5.0, size=40, col="red", label="(" )
annotate("text", x=5, y=5.6, size=12, col="red", label="o o" )
annotate("text", x=6.1, y=5.0, size=40, col="red", label=")" )
annotate("text", x=5, y=5.1, size=12, col="red", label="|" )
geom_segment(aes(x = 4.7, xend = 5.3, y = 4.4, yend = 4.4), size=2, color="red")
annotate("text", x=5, y=3, size=6, col="red", label="No Data")
bp <- ggplotly(p)
bp
}
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Main panel for displaying outputs ----
mainPanel(
actionButton('dfrdf','Alternate'),
# Output: Histogram ----
plotlyOutput("toolbar")
)
)
server <- function(input, output) {
rv <- reactiveValues(plot=NULL)
df1 <- data.frame(x=c(3,4,5),y=c(15,12,5))
p <- ggplot(df1, aes(x=x,y=y)) geom_col()
### This is just an actionbutton to imitate your data being NULL in some instances
observeEvent(input$dfrdf,{
k <- as.numeric(input$dfrdf) %% 2
if (k==0){
rv$plot <- ggplotly(p)
}else rv$plot <- NoDataPlotly() ### assign a dummy plot when data is NULL
},ignoreNULL = FALSE)
output$toolbar <- renderPlotly({
dev.off()
rv$plot
})
}
shinyApp(ui, server)

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312480.html
