8.1 Validation
8.1.1 Validating input
- giving feedback with the
shinyFeedback
package
UI: add useShinyFeedback()
ui <- fluidPage(
shinyFeedback::useShinyFeedback(),
numericInput("n", "n", value = 10),
textOutput("half")
)
Server: use feedback()
, feedbackWarning()
, feedbackDanger()
, and feedbackSuccess()
–> example app
8.1.2 Cancelling execution with req()
req()
checks for required values before allowing a reactive producer to continue.
Without req()
the complete reactive graph is computed (even without user input):
ui <- fluidPage(
selectInput("language", "Language", choices = c("", "English", "Maori")),
textInput("name", "Name"),
textOutput("greeting")
)
server <- function(input, output, session) {
greetings <- c(
English = "Hello",
Maori = "Ki ora"
)
output$greeting <- renderText({
paste0(greetings[[input$language]], " ", input$name, "!")
})
}
Using req()
:
8.1.3 req()
and validation
ui <- fluidPage(
shinyFeedback::useShinyFeedback(),
textInput("dataset", "Dataset name"),
tableOutput("data")
)
server <- function(input, output, session) {
data <- reactive({
req(input$dataset)
exists <- exists(input$dataset, "package:datasets")
shinyFeedback::feedbackDanger("dataset", !exists, "Unknown dataset")
req(exists, cancelOutput = TRUE)
get(input$dataset, "package:datasets")
})
output$data <- renderTable({
head(data())
})
}
Check out this app: https://hadley.shinyapps.io/ms-require-cancel/
8.1.4 Validate output
validate(message)
stops execution of the rest of the code and instead displays message in any downstream outputs
ui <- fluidPage(
numericInput("x", "x", value = 0),
selectInput("trans", "transformation",
choices = c("square", "log", "square-root")
),
textOutput("out")
)
server <- function(input, output, session) {
output$out <- renderText({
if (input$x < 0 && input$trans %in% c("log", "square-root")) {
validate(message = "x can not be negative for this transformation")
}
switch(input$trans,
square = input$x ^ 2,
"square-root" = sqrt(input$x),
log = log(input$x)
)
})
}