I would like to create a Shiny UI with 3 checkboxes for the user to choose from. However, I want to prompt the user with an error message if none of the boxes are selected.
I have tried using the validate function to solve this (as shown below) but it's currently not working. Can anyone point out what's wrong with my code and show me how to resolve it?
shinyUI(fluidPage(sidebarLayout(
sidebarPanel(checkboxGroupInput(
"variable", "Select An Option:",
c(
"One",
"Two",
"Three"
)
)),
mainPanel(textOutput("text"))
)))
shinyServer(function(input, output) {
text.data <- reactive({
validate(need(!is.null(input$variable),
"Please select an option"))
print(input$variable)
})
output$text <- renderPrint({
print(text.data())
})
})
EDIT
I have included images of the results I'm getting below:
For others benefit, package problems are a common error with the validate()
function due to the presence of naming collisions or masking (because it's such a common function name). For example, if you're using the qdap or jsonlite packages then calling validate()
might not be calling the shiny version of the function.
To test if this is your issue, try replacing validate()
with shiny::validate()
or changing the order you load your libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With