Input/Output Validation in R Shiny

It is good practice to validate the data before you visualize it in any form such as a table or plot. Shiny offers a simple and intuitive way to do that. As a result, the user does not see a confusing error in red, but an informative message that tells him the reason for the missing content.

In the following somewhat artificial example, we have a dropdown menu with the column names of a dataset. We use the standard data set

mtcars
mtcars here. By choosing an option, we plot the distribution of values in the corresponding column as a histogram. The default selection is an ’empty’ character. As there is no column with that name, this results in an error that is shown to the user.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
library(shiny)
ui <- fluidPage(
titlePanel("Validate"),
sidebarLayout(
sidebarPanel(
# Dropdown menu with additional empty choice
selectInput("col", label = "Column",
choices = c("",colnames(mtcars)))
),
mainPanel(
plotOutput("hist")
)
)
)
server <- function(input, output) {
output$hist <- renderPlot({
colname <- input$col
# Plot a histogram of the values in the chosen column
hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname)
})
}
shinyApp(ui, server)
library(shiny) ui <- fluidPage( titlePanel("Validate"), sidebarLayout( sidebarPanel( # Dropdown menu with additional empty choice selectInput("col", label = "Column", choices = c("",colnames(mtcars))) ), mainPanel( plotOutput("hist") ) ) ) server <- function(input, output) { output$hist <- renderPlot({ colname <- input$col # Plot a histogram of the values in the chosen column hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname) }) } shinyApp(ui, server)
library(shiny)

ui <- fluidPage(
    titlePanel("Validate"),
    sidebarLayout(
        sidebarPanel(
           # Dropdown menu with additional empty choice
            selectInput("col", label = "Column",
                        choices = c("",colnames(mtcars)))
        ),
        mainPanel(
            plotOutput("hist")
        )
    )
)

server <- function(input, output) {
    output$hist <- renderPlot({
        colname <- input$col
        # Plot a histogram of the values in the chosen column
        hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname)
    })
}

shinyApp(ui, server)

Validation

To validate variables, Shiny offers the

validate
validate function which wraps one or several conditions (
need
need
). In our case, we want to make sure the selected element is not empty. If any of the conditions is not satisfied, Shiny will display the provided message instead of the output. In case of multiple conditions, we separate them by a comma.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
validate(
need(colname != "", "Choose a column on the left.")
)
validate( need(colname != "", "Choose a column on the left.") )
validate(
    need(colname != "", "Choose a column on the left.")
)

The new server function with the input validation now looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server <- function(input, output) {
output$hist <- renderPlot({
colname <- input$col
validate(
need(colname != "", "Choose a column on the left.")
)
hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname)
})
}
server <- function(input, output) { output$hist <- renderPlot({ colname <- input$col validate( need(colname != "", "Choose a column on the left.") ) hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname) }) }
server <- function(input, output) {
    output$hist <- renderPlot({
        colname <- input$col
        validate(
            need(colname != "", "Choose a column on the left.")
        )
        hist(mtcars[[colname]], main = paste('Histogram of', colname), xlab = colname)
    })
}

The new message is much more informative and therefore makes the user interaction more intuitive. For more details see the documentation and/or this article on the topic.

This post first appeared on rshiny.blog.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.