Details
If the number of levels of x
is less than min_levels
or greater than max_levels
the validation will fail.
This function is a wrapper for shiny::validate
.
Examples
data <- data.frame(
one = rep("a", length.out = 20),
two = rep(c("a", "b"), length.out = 20),
three = rep(c("a", "b", "c"), length.out = 20),
four = rep(c("a", "b", "c", "d"), length.out = 20),
stringsAsFactors = TRUE
)
ui <- fluidPage(
selectInput(
"var",
"Select variable",
choices = c("one", "two", "three", "four"),
selected = "one"
),
verbatimTextOutput("summary")
)
server <- function(input, output) {
output$summary <- renderText({
validate_n_levels(data[[input$var]], min_levels = 2, max_levels = 15, var_name = input$var)
paste0(
"Levels of selected treatment variable: ",
paste(levels(data[[input$var]]),
collapse = ", "
)
)
})
}
if (interactive()) {
shinyApp(ui, server)
}