Shiny : showing one message for all errors

2020-07-11 08:52发布

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?

标签: r shiny
2条回答
The star\"
2楼-- · 2020-07-11 09:22

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")
    })
  }
))
查看更多
淡お忘
3楼-- · 2020-07-11 09:28

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

查看更多
登录 后发表回答