mirror of
https://github.com/rstudio/shiny.git
synced 2026-04-07 03:00:20 -04:00
a few tweaks to customStop()
This commit is contained in:
@@ -101,7 +101,7 @@ NULL
|
||||
#' simple generic error message is printed instead (the error and strack trace
|
||||
#' printed to the console remain unchanged). If you want this behavior in
|
||||
#' general, but you DO want a particular error message to get displayed to the
|
||||
#' user, please use \code{stopWithCondition} instead.}
|
||||
#' user, please use \code{customStop} instead.}
|
||||
#' }
|
||||
#' @name shiny-options
|
||||
NULL
|
||||
|
||||
51
R/utils.R
51
R/utils.R
@@ -882,7 +882,9 @@ columnToRowData <- function(data) {
|
||||
#' Create a custom error
|
||||
#'
|
||||
#' This should be used when you want to create a custom error to which error
|
||||
#' default settings do not apply.
|
||||
#' default options do not apply. In particular, \code{customStop} will
|
||||
#' ignore the value of \code{getOption("shiny.sanitize.errors")} and always
|
||||
#' display the error in the app itself.
|
||||
#'
|
||||
#' @param error Either an "error" object or a "character" object (string).
|
||||
#' The former will just be rethrown, while the latter will be used as the
|
||||
@@ -891,7 +893,7 @@ columnToRowData <- function(data) {
|
||||
#'
|
||||
#' @details An error generated by \code{customStop} has priority over all
|
||||
#' other Shiny errors. This can be dangerous. For example, if you have set
|
||||
#' \code{options((shiny.sanitize.errors = TRUE)}, then by default all error
|
||||
#' \code{options(shiny.sanitize.errors = TRUE)}, then by default all error
|
||||
#' messages are omitted in the app, and replaced by a generic error message.
|
||||
#' However, this does not apply to \code{customStop}: whatever you pass
|
||||
#' through \code{error} will be displayed to the user. So, this should only
|
||||
@@ -900,17 +902,34 @@ columnToRowData <- function(data) {
|
||||
#' your users' lives much easier by giving them a hint as to where the
|
||||
#' error occurred.
|
||||
#'
|
||||
#' @seealso \code{\link{shiny-options}}
|
||||
#'
|
||||
#' @export
|
||||
#' @examples
|
||||
#' # in ui.R
|
||||
#' fluidPage(
|
||||
#' textInput('number', 'Enter your favorite number from 1 to 10', '5'),
|
||||
#' textOutput('result')
|
||||
#' )
|
||||
#' # uncomment the desired line to experiment with shiny.sanitize.errors
|
||||
#' # options(shiny.sanitize.errors = TRUE)
|
||||
#' # options(shiny.sanitize.errors = FALSE)
|
||||
#'
|
||||
#' # in server.R
|
||||
#' function(input, output) {
|
||||
#' output$result <- renderText({
|
||||
#' # ui
|
||||
#' ui <- shinyUI(fluidPage(
|
||||
#' textInput('number', 'Enter your favorite number from 1 to 10', '5'),
|
||||
#' textOutput('errorStop'),
|
||||
#' textOutput('errorCustomStop')
|
||||
#' ))
|
||||
#'
|
||||
#' # server
|
||||
#' server <- shinyServer(function(input, output) {
|
||||
#' output$errorStop <- renderText({
|
||||
#' number <- input$number
|
||||
#' if (number %in% 1:10) {
|
||||
#' return(paste('You chose', number, '!'))
|
||||
#' } else {
|
||||
#' return(stop(
|
||||
#' paste(number, 'is not a number between 1 and 10')
|
||||
#' ))
|
||||
#' }
|
||||
#' })
|
||||
#' output$errorCustomStop <- renderText({
|
||||
#' number <- input$number
|
||||
#' if (number %in% 1:10) {
|
||||
#' return(paste('You chose', number, '!'))
|
||||
@@ -920,21 +939,21 @@ columnToRowData <- function(data) {
|
||||
#' ))
|
||||
#' }
|
||||
#' })
|
||||
#' }
|
||||
#' })
|
||||
#'
|
||||
#' # run
|
||||
#' if (interactive()) shinyApp(ui = ui, server = server)
|
||||
customStop <- function(error, errorClass = character(0)) {
|
||||
if (inherits(error, "error")) {
|
||||
attr(error, "class") <- c("shiny.custom.error", errorClass,
|
||||
attr(error, "class"))
|
||||
class(error) <- c("shiny.custom.error", errorClass, class(error))
|
||||
stop(error)
|
||||
} else if (inherits(error, "character")) {
|
||||
stopWithCondition(c("shiny.custom.error", errorClass), error)
|
||||
} else {
|
||||
stop("The class of the `error` paramater must be either 'error'
|
||||
or 'character'")
|
||||
stop("The class of the `error` parameter must be either 'error' or 'character'")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#' Validate input values and other conditions
|
||||
#'
|
||||
#' For an output rendering function (e.g. \code{\link{renderPlot}()}), you may
|
||||
|
||||
@@ -15,12 +15,14 @@ message of the error returned by \code{customStop}.}
|
||||
}
|
||||
\description{
|
||||
This should be used when you want to create a custom error to which error
|
||||
default settings do not apply.
|
||||
default options do not apply. In particular, \code{customStop} will
|
||||
ignore the value of \code{getOption("shiny.sanitize.errors")} and always
|
||||
display the error in the app itself.
|
||||
}
|
||||
\details{
|
||||
An error generated by \code{customStop} has priority over all
|
||||
other Shiny errors. This can be dangerous. For example, if you have set
|
||||
\code{options((shiny.sanitize.errors = TRUE)}, then by default all error
|
||||
\code{options(shiny.sanitize.errors = TRUE)}, then by default all error
|
||||
messages are omitted in the app, and replaced by a generic error message.
|
||||
However, this does not apply to \code{customStop}: whatever you pass
|
||||
through \code{error} will be displayed to the user. So, this should only
|
||||
@@ -32,14 +34,15 @@ error occurred.
|
||||
\examples{
|
||||
# in ui.R
|
||||
fluidPage(
|
||||
textInput('number', 'Enter your favorite number from 1 to 10'),
|
||||
textOutput('result')
|
||||
textInput('number', 'Enter your favorite number from 1 to 10', '5'),
|
||||
textOutput('resultStop')
|
||||
textOutput('resultCustomStop')
|
||||
)
|
||||
|
||||
# in server.R
|
||||
function(input, output) {
|
||||
output$result <- renderText({
|
||||
number <- input$number
|
||||
number <- input$number
|
||||
if (number \%in\% 1:10) {
|
||||
return(paste('You chose', number, '!'))
|
||||
} else {
|
||||
@@ -50,4 +53,7 @@ function(input, output) {
|
||||
})
|
||||
}
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{shiny-options}}
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ The default polling interval is 500 milliseconds. You can change this
|
||||
simple generic error message is printed instead (the error and strack trace
|
||||
printed to the console remain unchanged). If you want this behavior in
|
||||
general, but you DO want a particular error message to get displayed to the
|
||||
user, please use \code{stopWithCondition} instead.}
|
||||
user, please use \code{customStop} instead.}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user