首页 > 解决方案 > 条件面板,其中条件是输入的长度

问题描述

我无法理解为什么我的病情input.wave1.length > 1不起作用。

我想要发生的是复选框“整体曲线”不会出现,除非input$loess并且如果在 Wave 1 或 Wave 2 手风琴中检查了超过 1 个项目。

我看不出我做错了什么。javascript中是否有条件可以完成这项工作,或者这可以用R脚本完成吗?

我的应用程序:

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)

标签: rshinyshinydashboardshinyapps

解决方案


要使用检查了超过 1 个项目的 Wave 1 或 Wave 2 手风琴,您可以使用以下内容:

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)
    )

另外,你还需要input.loess == 1在服务器端制作


推荐阅读