Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny : showing one message for all errors

Tags:

r

shiny

I have an application in R's Shiny. I would like to handle messages so that users do not see what error occurred. I know that via

 tags$style(type="text/css",
                        ".shiny-output-error { visibility: hidden; }",
                        ".shiny-output-error:before { visibility: hidden; }"
            ),

I can disable error messages entirely, but I would like to show users one message like

An error occurred. Please contact the admin.

whenever message of whatever type occurs (and still keep the original error message in the log). Any ideas?

like image 800
Love-R Avatar asked Jun 17 '15 09:06

Love-R


2 Answers

You can add options(shiny.sanitize.errors = TRUE) somewhere in your app. All error messages will then be replaced with the generic error message:

Error: An error has occurred. Check your logs or contact the app author for clarification.

If you do want a particular error to pass through unsanitised, you can use base::stop(shiny::safeError(e)) instead of just base::stop(e), where e is an error string or object with class 'error'.

Reference: https://shiny.rstudio.com/articles/sanitize-errors.html

like image 91
Peter Harrison Avatar answered Nov 16 '22 07:11

Peter Harrison


Interesting question. I only thought about it for one minute so I'm sure there are better, cleaner solutions that use R code, but here's a CSS solution since you used CSS in the question

Basically, since I saw that you used a :before, it made me realize that you can just change the text of that pseudoelement.

runApp(shinyApp(
  ui = fluidPage(
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: visible; content: 'An error occurred. Please contact the admin.'; }"
    ),
    textOutput("text")
  ),
  server = function(input, output, session) {
    output$text <- renderText({
      stop("lalala")
    })
  }
))
like image 21
DeanAttali Avatar answered Nov 16 '22 06:11

DeanAttali