Apricot
Apricot

Reputation: 3011

updateCheckBoxGroupInput in shiny based on selection of other checkboxes

My shiny application has multiple tabs. In one of the tabs I have plot output which I want to use to create reports in another tab. I have included a checkbox in the first tab for the user to select the output for reporting. In the second tab I am trying to update a check box group input based on the selection of the first tab. However I am getting only the first option selected.

The reproducible code is as follows: This is based on ifelse condition:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
                   dashboardHeader(
                       title = "MODULE",titleWidth = 225
                   ),
                   dashboardSidebar(
                       width = 225,
                       sidebarMenu(id = "tabs",
                                   menuItem("TOPLINES", tabName = "tplines", icon = shiny::icon("dashboard")),
                                   menuItem("MY MONTHLY REPORTS", tabName = "myweeklyrep", icon = shiny::icon("compass"))
                       )),
                   dashboardBody(
                       tabItems(
                           tabItem(
                               tabName = "tplines",
                               fluidRow(
                                   box(
                                       checkboxInput(inputId = "inventorytop8metrocheck", "Add to reports", value = FALSE),
                                       width = 6, status = "info", title = "Inventory information",
                                       div(plotlyOutput("inventorytop8metro"), width = "100%", height = "400px", style = "font-size:80%;")
                                   ),
                                   box(
                                       checkboxInput(inputId = "top15categoriestplinescheck", "Add to reports", value = FALSE),
                                       width = 6, status = "info", title = "Top 15 categories",
                                       div(plotlyOutput("top15categoriestplines"), style = "font-size:90%")
                                   ))),
                           tabItem(
                               tabName = "myweeklyrep",
                               fluidRow(
                                   h4("AVAILABLE ANALYSIS", align = 'center'),br(),
                                   column(width = 12, 
                                          list(tags$div(align = 'left', 
                                                        class = 'multicol', 
                                                        checkboxGroupInput(inputId  = 'analysisSelector', 
                                                                           label = "Select the analysis:",
                                                                           choices  = "",
                                                                           selected = "",
                                                                           inline   = FALSE)))
                                   ))))))

server <- function(session,input,output){
    observe({
            updateCheckboxGroupInput(session, inputId = "analysisSelector", label = "", choices = 
                                         ifelse(!is.null(input$top15categoriestplinescheck) || length(input$top15categoriestplinescheck) != 0, "Inventory top 8 metros",
                                            ifelse(!is.null(input$inventorytop8metrocheck) || length(input$inventorytop8metrocheck) != 0, "Top 15 categories - Topline", "No selection")),
                                     selected = "",inline = FALSE)

    })
}

shinyApp(ui,server)

I tried with if, else if also but they aren't working. Any thoughts?

The if, else if conditions:

    updateCheckboxGroupInput(session, inputId = "analysisSelector", label = "", choices = 
                                 if(!is.null(input$top15categoriestplinescheck) || length(input$top15categoriestplinescheck) != 0){
                                     "Inventory top 8 metros"
                                 } else if (!is.null(input$inventorytop8metrocheck) || length(input$inventorytop8metrocheck) != 0){
                                     "Top 15 categories - Topline"
                                 } else {
                                     return()
                                 },
                             selected = "",inline = FALSE)

EDIT:

I tried the following option: which renders the checkboxes irrespective of whether they are selected or not.

getlist <- reactive({
        if(!is.null(input$top15categoriestplinescheck) & !is.null(input$inventorytop8metrocheck)){
            c("Top 15 categories - Topline","Inventory of top 8 metros - Topline")
        } else if (!is.null(input$top15categoriestplinescheck)){
            "ABC"
        } else if (!is.null(input$inventorytop8metrocheck)){
            "DEF"
        } else {
            return()
        }
    })

    observe({
            updateCheckboxGroupInput(session, inputId = "analysisSelector", label = "Select the analysis:", choices = 
                                         as.list(getlist()),
                                     selected = "",inline = FALSE)            
    })

Upvotes: 0

Views: 2311

Answers (1)

Tutuchan
Tutuchan

Reputation: 1567

This is actually easier to handle with observeEvent as explained in the documentation of this function (see ?observeEvent). From what I understand, it actually wraps observe but in a more intuitive way.

You have to pass it two arguments: an event (in this case, the click on one of your checkboxGroupInputs) and the action to perform when this event occurs.

The server function thus becomes:

server <- function(session,input,output){
    updateAnalysisSelector <- function(session) {
        choices <- ifelse(input$top15categoriestplinescheck, "Inventory top 8 metros",
                          ifelse(input$inventorytop8metrocheck, "Top 15 categories - Topline", "No selection"))
        updateCheckboxGroupInput(session, 
                                 inputId = "analysisSelector", 
                                 label = "Select the analysis:", 
                                 choices = choices,
                                 selected = "",
                                 inline = FALSE)
    }

  observeEvent(input$top15categoriestplinescheck, updateAnalysisSelector(session))
  observeEvent(input$inventorytop8metrocheck, updateAnalysisSelector(session))
}

I'm sure this could be simplified if your UI did not have two separate checkbox groups but this works for your current implementation.

Upvotes: 1

Related Questions