R选中的每个checkboxGroupInput的闪亮有条件的dateInput

时间:2019-02-05 21:15:22

标签: r shiny conditional

我正在创建一个应用程序,允许用户多选项目列表。对于每个选定的项目,我想在项目名称(即a / b / c)旁边显示一个dateInput字段。

当前,我可以为任何一个项目显示dateInput字段,但不能显示多个(如下所示)

One dateInput field for one selected project

一旦在项目列表旁边显示多个dateInputs,如下所示:我想知道dateInput字段如何很好地对齐,因为它们似乎比复选框列表占用更多的空间,如下所示:

enter image description here

ui <-  dashboardPage(
  dashboardHeader(
    title = ""
  ),

  dashboardSidebar(
    sidebarMenu(
      menuItem("Projects", tabName = "tab1")
    ) 
  ),

  dashboardBody(
    tabItems(
      tabItem(tabName = "tab1",
              fluidRow(h2("Projects"), 
                column(6,
                       checkboxGroupInput("Projects", "",
                                          c("a", "b", "c")
                       )
                ),
                column(4,
                       conditionalPanel(condition = "input.Projects == 'a' || 
                                        input.Projects == 'b' || input.Projects 
                                        == 'c'", dateInput("proj_date", ""))
                       )
                )
              )
      )
    )
  )

server <- function(input, output) {
}


shinyApp(ui, server)

我了解我需要在conditionalPanel的condition参数中放入javascript。请告知我如何更正代码,并为所有选定的项目显示一个或多个dateInput字段。

1 个答案:

答案 0 :(得分:1)

尝试uiOutput

library(shiny)
library(shinydashboard)

ui <-  dashboardPage(
    dashboardHeader(),
    dashboardSidebar(
        sidebarMenu(
            menuItem("Projects", tabName = "tab1")
        ) 
    ),
    dashboardBody(
        tabItems(
            tabItem(tabName = "tab1",
                    fluidRow(h2("Projects"), 
                             column(6, checkboxGroupInput("Projects", "", c("a", "b", "c"))),
                             column(4, uiOutput('dates'))))
        )
    )
)

server <- function(input, output) {
    output$dates <- renderUI({
        lapply(input$Projects, function(project) {
            div(dateInput(paste0("proj_date_",project), paste("Project", project, "date")))
        })
    })
}

shinyApp(ui, server)