Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Error Messages For Shiny UI With validate()

Tags:

r

shiny

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:

  • When nothing is selected, the message displayed in "NULL NULL" instead of "Please select an option"

enter image description here

  • When a box is selected, this error message is displayed: "Error:is.character(txt) is not TRUE" instead of the a print out of the input selected.

enter image description here

like image 886
dts86 Avatar asked Oct 31 '15 15:10

dts86


1 Answers

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.

like image 168
C W Avatar answered Nov 26 '22 15:11

C W