conditional panel where condition is length of input

Viewed 399

I'm having trouble understanding why my condition input.wave1.length > 1 does not work.

What I would like happen is for the checkbox "Overall Curve" to not appear unless input$loess AND if there are more than 1 items checked in the either Wave 1 or Wave 2 accordions.

I don't see what I'm doing wrong. Is there a condition in javascript that will make this work or can this be done with R script?

my app:

library(shiny)
library(shinydashboard)
library(bsplus) #accordion


#########define waves##########
wave1 <- c(
  "Cayuga", "Columbia", "Erie", "Greene", 
  "Lewis", "Putnam", "Suffolk", "Ulster" 
)

wave2 <- c(
  "Broome", "Chautauqua", "Cortland", "Genesee",
  "Monroe", "Orange", "Sullivan", "Yates" 
)

ui <- dashboardPage(
  dashboardHeader(title = "Example"),
  dashboardSidebar(
    tags$h4("waves:", style = "margin: 5px;"),
    bs_accordion(id = "waves") %>% 
      #use the entire heading panel as a link instead of just title
      bs_set_opts(use_heading_link = TRUE) %>% 
      bs_append(
        title = "Wave 1",
        content = checkboxGroupInput(inputId = "wave1", label = NULL,
                                     choices = c(wave1, "All Wave 1"),
                                     selected = "Cayuga")
      ) %>% 
      bs_append(
        title = "Wave 2",
        content = checkboxGroupInput(inputId = "wave2", label = NULL,
                                     choices = c(wave2, "All Wave 2"))
      ),
    br(),
    #LOESS CURVE ####
    checkboxInput(inputId = "loess", label = "Display Loess Curve",
                  value = FALSE),
    uiOutput("loess_a"),
    # uiOutput("loess_overall"),
    conditionalPanel(condition = "input.loess == TRUE & input.wave1.length > 1",  # should include selected > 1
                     checkboxInput(inputId = "loessGrouped", label = "Overall Curve",
                                   value = TRUE)
    )
  ),
  dashboardBody(
    tags$style(HTML('.checkbox label{color: red;}'))
  )
)

server <- function(input, output, session) {
  
  # conditional loess smoother #######
  output$loess_a <- renderUI({
    req(input$loess)
    conditionalPanel(condition = "input.loess == TRUE",
                     sliderInput(inputId = "smoothing", label = NULL,
                                 min = 0, max = 1, value = 1, step = 0.1))
  })
}

shinyApp(ui = ui, server = server)
1 Answers

To work with either Wave 1 or Wave 2 accordions having more than 1 item checked, you can use the following:

conditionalPanel(condition = "input.loess == 1 & (input.wave1.length > 1 || input.wave2.length > 1)",  # should include selected > 1
                     checkboxInput(inputId = "loessGrouped", label = "Overall Curve",
                                   value = TRUE)
    )

In addition, you still need to make input.loess == 1 on the server side

Related