0

I have this piece of code where error happens:

server <- function(input, output, session) {
  rvalues <- reactiveValues()

  observeEvent(input$run, {
    alldata <- data_request(as.numeric(input$text1))
    fields <- alldata$fields
    fields <- as.data.table(do.call(rbind, fields))

    setnames(fields,
             c("V1", "V2", "V3", "V4", "V5"),
             c('mod_id', 'fieldas', 'neturima', 'alternatyva', 'score'))
    rvalues$fields <- fields
    scores <- alldata$scores
    scores <- as.data.table(do.call(rbind, scores))
    setnames(scores,
             c("V1", "V2"),
             c("mod_id", "score"))
    rvalues$scores <- scores
    info <- alldata$info
    info <- as.data.table(do.call(rbind, info))
    setnames(info,
             c('V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7'),
             c('prod_id', 'mod_id', 'title', 'sell_price', 'cost_price', 'delivery_hours', 'score'))
    rvalues$info <- info
    rvalues$choices <- paste0('[',info$score, ']', '[', info$mod_id, ']', substr(info$title,
                                                          start = 1,
                                                          stop = 100),
                              '...')
    #print(class(rvalues$choices))
    updateSelectInput(session, 'selectinput', choices = rvalues$choices)
    })
}

My program asks for an item id and then downloads the data from mysql server. It works fine if the provided id exists, but if you type in an unexisting id, then the whole thing crashes near the first do.call statement:

Warning: Error in do.call: second argument must be a list

What I want to achieve is to check if this error comes up and if id does, I want my custom error message to come up and skip the rest of the code in current observeEvent.

I tried to put:

a <- tryCatch(warning("Wrong product id!"), warning=function(w) { w })
    mess <- a$message
    showNotification(mess)

after the first do.call statement, but that just shows the warning even if the id is correct and the app still crashes if the id is wrong...

milka1117
  • 521
  • 4
  • 8
  • 17

1 Answers1

0

Assuming alldata$fields is meant to be a list, here' a way that may help -

observeEvent(input$run, {
    alldata <- data_request(as.numeric(input$text1))
    fields <- alldata$fields
    # change below if condition as appropriate
    if(!is.list(fields)) showNotification("Wrong product id!")
    req(is.list(fields))
    fields <- as.data.table(do.call(rbind, fields))
    # rest of the code
}

Here's an example -

shinyApp(
  ui = fluidPage(
    numericInput("test", "test", value = 1),
    actionButton("act", "press"),
    verbatimTextOutput("op")
  ),
  server = shinyServer(function(input, output, session) {
    v <- reactiveValues(v = NULL)

    observeEvent(input$act, {
      if(input$test <= 5) showNotification("number must be > 5", type = "error")
      req(input$test > 5)
      v$v <- input$test
    })

    output$op <- renderPrint({
      v$v
    })
  })
)
Shree
  • 10,835
  • 1
  • 14
  • 36