How can I validate select input in RShiny app?

Viewed 37

I want to ask, can somebody help me how to validate select input like this ? We assume that there is no factor in iris. Is there some way how to validate it ?

Server:

output$choose_y = renderUI({
    is_factor <- sapply(iris, FUN = is.factor)
    y_choices <- names(iris) [is_factor]
    selectInput('choose_y', label = 'Choose Target Variable', choices = y_choices)
}) 

Ui:

uiOutput('choose_y')
1 Answers

To illustrate our discussion in comments, you could check if any factor is present in dataset:

# With iris
is_factor <- sapply(iris, FUN = is.factor)
y_choices <- names(iris) [is_factor]
validate(need(any(is_factor),"No factor in dataset"))
print(paste('OK to use iris'))

#[1] "OK to use iris"

# With mtcars
is_factor <- sapply(mtcars, FUN = is.factor)
y_choices <- names(mtcars) [is_factor]
validate(need(any(is_factor),"No factor in dataset"))

Error: No factor in dataset
Related