Shiny : showing one message for all errors

无人久伴 提交于 2020-01-22 20:02:12

问题


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?


回答1:


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




回答2:


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")
    })
  }
))


来源:https://stackoverflow.com/questions/30887205/shiny-showing-one-message-for-all-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!