1

I have a multiselect input (statsmanuf_selector) in my R Shiny applications with the following possible choice values:

  • "All Manufacturers"
  • "Aurobindo"
  • "Varichem"
  • "Caps"
  • "Cipla"
  • "Hetero"

I need to validate this selected input, if the selection does not include at least two values except for "All Manufacturers", it prints the error message "Please choose at least two manufacturers"

I have tried to use the following code in this validation:

  output$Stats = renderPrint({
    if (try(length(input$statsmanuf_selector) < 2 && input$statsmanuf_selector != "All Manufacturers")) {
      validate("Please choose at least two manufacturers")
    }
        req(input$Active)
        req(input$ManufacturerST)
        dataset <- dataset()
        attach(dataset)
        fligner.test(Assay ~ Manufacturer, data = dataset)

    })

Created on 2022-12-27 with reprex v2.0.2

However, this gives me the error message "missing value where TRUE/FALSE needed".

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 2
    You maximise your chance of getting a useful answer if you provide a minimal reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example) may help. – Limey Dec 27 '22 at 12:14

1 Answers1

0

validate is designed to look for a need statement. This is therefore more easy than using try . See this minimal example:

library(shiny)

ui <- fluidPage(
  selectInput("sel","Manufacturers",
              c("All Manufacturers" ,"Aurobindo" ,"Varichem" ,
                "Caps" ,"Cipla", "Hetero"),
              multiple=TRUE),
  textOutput("Stats")
)

server <- function(input, output, session) {
  output$Stats = renderPrint({
    validate(
      need(length(input$sel) >= 2 | input$sel == "All Manufacturers","Please choose at least two manufacturers")
    )
    print(input$sel) ## Just as an example ...Use Your code here
  })
}

shinyApp(ui, server)

I flipped the logic to 2 or more, or "all manufacturers" to avoid unnecessary negation. Also i just return a print statement if the condition is met, since i dont know your data, and stats .. .

user12256545
  • 2,755
  • 4
  • 14
  • 28