mardi 23 mai 2017

Shiny: validate for a group of checkboxGroupinput

I have a small shiny application that will take a user's input and create a plot based on it later on. So far my sidebar looks like this: like this

I now would like to use "validate" to check if one of the 6 options of colors is clicked and if not, ask the user to to so.

So far my code checks only for the color red.

I am also wondering if there is way to have one box (for fruit for example) automatically checked the whole time. For example: starting the app Apple is preselected, but if you unclick it, Banana (or Citrus) automatically get choosen. Is there a solution using this approach instead of validate?

Here is my code: library(shiny) library(ggplot2) library(plotly) library(data.table) library(DT) library(shinyjs)

ui <- shinyUI(fluidPage(
  tabsetPanel(
  tabPanel("Upload",
           sidebarLayout(
           sidebarPanel(width = 4,
                        fileInput('file1', 'Choose CSV File',
                                  accept=c('text/csv', 
                                           'text/comma-separated-values,text/plain', 
                                           '.csv')),
                        #tags$hr(),
                        checkboxInput('header', 'Header', TRUE),
                        radioButtons('sep', 'Separator',
                                     c(Comma=',',
                                       Semicolon=';',
                                       Tab='\t'),
                                     ',')
           ),
           mainPanel(dataTableOutput('table1')
           ))
  ),
  tabPanel("fruits",
           sidebarLayout(
             sidebarPanel(width = 4,
                          uiOutput("fruitTable")
                          ),
           mainPanel(dataTableOutput('table2')
    )))
)))


server <- shinyServer(function(input, output) {

  file_data <- reactive({
    fruit_file1 <- input$file1
    if(is.null(fruit_file1)){
      return(NULL)
    }else{
      fruits <- fread(fruit_file1$datapath, header=input$header, sep=input$sep, check.names = FALSE)
      return(fruits)
    }
  })


  output$table1 <- renderDataTable({
    if(is.null(file_data())){
      return(NULL)
    }else{
      datatable( file_data(),options = list(pageLength = 25))
    }
  })

  output$table2 <- renderDataTable({
    if(is.null(file_data())){
      return(NULL)
    }else{

      validate(
        need(input$fruit, 'Check at least one fruit!'),
        need(input$month, 'Check at least one fertilizer!'),
        need(input$marker1, 'Check at least one color!')
      )

      datatable( file_data(),options = list(pageLength = 25))
    }
  })

  output$fruitTable <- renderUI({
    if(is.null(file_data())){
      return()
    }else{
      tagList(
        checkboxGroupInput(inputId = "fruit",
                           label = "Fruit",
                           choices = c("Apple", "Banana", "Citrus")),
        radioButtons(inputId = "month",
                     label = "Month",
                     choices = c("1"= 1, "9" = 2, "12" = 3 ),
                     selected = 1,
                     inline = TRUE),
        checkboxGroupInput(inputId = "stim",
                           label = "Stimulation",
                           choices = list("A","B", "C")),
                           #choices = c(unique(as.character(file_data()[,3])))),
        checkboxGroupInput(inputId = "marker1",
                           label = "red",
                           choices= list("+", "-"),
                           #choices = c(unique(as.character(file_data()[,4]))),
                           inline = TRUE),
        checkboxGroupInput(inputId = "marker2",
                           label = "green",
                           choices= list("+", "-"),
                           #choices = c(unique(as.character(file_data()[,5]))),
                           inline = TRUE),
        checkboxGroupInput(inputId = "marker3",
                           label = "yellow",
                           choices= list("+", "-"),
                           #choices = c(unique(as.character(file_data()[,6]))),
                           inline = TRUE)
      )
    }
  })
})

shinyApp(ui = ui, server = server)

and a link to the file I used.

After uploading the file you need to switch tabs to fruit.(I have not figured out yet, how to automatically have the tab switched after uploading)

Thanks for helping!




Aucun commentaire:

Enregistrer un commentaire