mercredi 25 mai 2016

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)




Aucun commentaire:

Enregistrer un commentaire