mhannani
mhannani

Reputation: 502

tryCatch error handling doesn't work in shiny app?

I'm doing some touch to my shiny app , the problem i'm encountering is that i can't handle the errors using tryCatch like :

tryCatch({
    # expr
},
error = function(e) {
    # handle the error
}

I'm using the Apriori algorithm within my application ,when the user choose a dataset ,he can also adjust the values of min-support and min-confidence, but sometimes with some values of these, apriori algorithm returns 0 rules , and the error occurs when trying to plot the graph of the association rules .

Here's a small spinet of my code so far :

Getting the file

...

...

...

Find the association rules :

  rules <- reactive({
    validate(
      need(input$file, "Please choose a data set")
    )
    transactions = read.transactions(
      file = file(input$file$datapath),
      format = "basket",
      sep = ","
    )
    minValue <- min(length(transactions),input$visualization)
    
    
rules <-
  apriori(transactions[0:minValue],
          parameter = list(
            support = input$min_supp,
            confidence = input$min_conf
          ))
    print(length(transactions[0:minValue]))
    return(rules)
  })

Plot the obtained association rules :

  output$graphChart <- renderPlot({
    Sys.sleep(1) 
    validate(
      need(input$file, "Please choose a data set")
    )
    set.seed(42)
    # validate(
    #   need(length(rules()) == 0, "zero rules")
    # )
    
    tryCatch({
      plot(rules(), method = "graph")
    })
    error = function(condition){
      print('there was an error')
    }
    
  })

But nothing changed i still get the error and no message printed in the R studio's console

enter image description here

I tried this but it doesn't help me get rid of the error, By the way i also get errors on other tabs when no rules found .

EDITED

As Pork mentioned in his comment ,i tried :

  output$graphChart <- renderPlot({
    Sys.sleep(1) 
    validate(
      need(input$file, "Please choose a data set")
    )
    set.seed(42)
    # validate(
    #   need(length(rules()) == 0, "zero rules")
    # )
    
    tryCatch({
      plot(rules(), method = "graph",)
    })
    error=function(cond) {
      message(cond)
      
      return(NA)
    }
    warning=function(cond) {

      message(cond)
      # Choose a return value in case of warning
      return(NULL)
    }
    
  })

And the error persist again,

Can someone help me please ?

Any suggestions or advice would be appreciated!

Thanks.

Upvotes: 0

Views: 1925

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Here is a small example of how you may use tryCatch block. We shall use showNotification to notify the user of the error

library(shiny)

ui <- fluidPage(
    sidebarPanel(width = 2,
                 selectInput("data","data",choices = c(1,2),selected = 1)
                 
    ),
    mainPanel(
        plotOutput("graphChart")
    )
)

server <- function(input, output, session) {
    
    rules <- reactive({
        if(input$data == 1){
            return(mtcars$mpg)
        }else{
            "some error"
        }
    })
    
    output$graphChart <- renderPlot({
        
        tryCatch({
            plot(rules())
        }, warning = function(w) {
            showNotification('there was a warning','',type = "error")
            return()
        }, error = function(e) {
            showNotification('there was an error','',type = "error")
            return()
        }, silent=TRUE)
        
    })
    
}

shinyApp(ui, server)

enter image description here

Upvotes: 5

Related Questions