0

Hello everyone I'm trying to write a function in Shiny R. I have checkboxgroupinput like this:

checkboxGroupInput("quality", "Columns in quality to show:", 
choices = numbers, selected = numbers, width = '50%' ), width =2)

I want a histogram to appear when at least one box is selected otherwise it shows helptex("please select at least one"). How can I do this?

C.Robin
  • 1,085
  • 1
  • 10
  • 23
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Some shiny-specific reproducible example advice here as well: https://github.com/rstudio/shiny/wiki/Creating-a-Reproducible-Example – MrFlick Aug 13 '21 at 18:55

1 Answers1

0
library(shiny)

ui <- fluidPage(
  br(),
  checkboxGroupInput(
    "quality", "Columns in quality to show:", 
    choices = c("A", "B", "C"), selected = c("A", "B", "C"), width = "50%"
  ),
  br(),
  conditionalPanel(
    condition = "input.quality.length === 0",
    helpText("Select at least one column")
  ),
  conditionalPanel(
    condition = "input.quality.length !== 0",
    plotOutput("histo")
  )
)

server <- function(input, output, session){
  
  output[["histo"]] <- renderPlot({
    
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = 11)
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

shinyApp(ui, server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225