Files
shiny/man/validateInput.Rd
2014-05-02 17:56:20 -05:00

66 lines
2.4 KiB
R

% Generated by roxygen2 (4.0.0): do not edit by hand
\name{validateInput}
\alias{validateCondition}
\alias{validateInput}
\title{Check if input values satisfy the output rendering function}
\usage{
validateInput(...)
validateCondition(condition, message)
}
\arguments{
\item{...}{A list of arguments, and each argument takes either a logical
value or an object returned by \code{validateCondition()}. When an argument
takes a logical value, the value is the condition on which the rendering
function should stop (the condition normally returns \code{TRUE} or
\code{FALSE}, and this function stops when the condition is \code{FALSE};
see Details).}
\item{condition}{A condition to be validated.}
\item{message}{A character string as the error message if the condition is
not satisfied.}
}
\description{
For an output rendering function (e.g. \code{\link{renderPlot}()}), you may
need to check certain input values before you can render the output. If the
input values do not satisfy the rendering function, a special type of error
can be emitted to indicate this special situation. If you need to show the
error message(s), you can use \code{validateCondition()} as the input to
\code{validateInput()}, otherwise \pkg{shiny} will silently stop processing
the input.
}
\details{
For the sake of convenience, it is not strictly required that the condition
is a logical value, and you can use input values themselves as the testing
conditions, since there are a few common cases in which the input values are
often considered invalid, including \code{NULL}, \code{NA}, values of length
zero, and a special case for action buttons when they take values of 0 (i.e.
not clicked). If any of these values happen to be valid, you can explicitly
turn them to logical values. For example, if you allow \code{NA} but not
\code{NULL}, you can use the condition \code{!is.null(input$foo)}, because
\code{!is.null(NA) == TRUE}.
}
\examples{
# in ui.R
fluidPage(
actionButton('in1', 'Go!'),
checkboxGroupInput('in2', 'Check some letters', choices = head(LETTERS)),
selectizeInput('in3', 'Select a state', choices = state.name),
plotOutput('plot')
)
# in server.R
function(input, output) {
output$plot <- renderPlot({
validateInput(
input$in1, # ensure the button has been clicked
validateCondition(input$in2, 'Check at least one letter!'),
validateCondition(input$in3 == '', 'Please choose a state.')
)
plot(1:10, main = paste(c(input$bar, input$foo), collapse = ', '))
})
}
}