在 Shiny mainPanel 中看不到条件选择?

Can't see conditional choices in Shiny mainPanel?

我试图让用户从各种选项中进行选择,但出于某种原因,我在主面板中看不到这些选项:

ui <- fluidPage(
  h1("Reminder to do X"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(
        inputId = "type",
        label = "Reminder Type",
        choices = c("Single Date Reminder" = "single",
                    "Multi Date Reminder" = "multi",
                    "From-To Reminder" = "from_to"),
        selected = "single", width = '100%'
      )
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.source == 'single'",
        dateInput("single_date", "Enter the date for your reminder", value = Sys.Date())
      ),
      conditionalPanel(
        condition = "input.source == 'multi'",
        dateRangeInput("multi_date_1", 
                       label = "Enter from to dates", 
                       start = Sys.Date(), 
                       end = Sys.Date() + 7,
                       autoclose = TRUE)
      ),
      conditionalPanel(
        condition = "input.source == 'from_to'",
        dateRangeInput("multi_date_2", 
                       label = "Enter from to dates", 
                       start = Sys.Date(), 
                       end = Sys.Date() + 7,
                       autoclose = TRUE)
      )
    )
  )
)

  shinyApp(ui, server = function(input, output) { })

请告知如何查看所选的日期选择器选项?我正在点击单选按钮,但看不到日期选择器。

这是由于引用了错误的 ID。您的 inputiD"type",而在您的 conditionalPanel() 中,您指的是“source”。因此,将所有 input.source 更改为 input.type 即可。

因此您的代码如下所示:

ui <- fluidPage(
  h1("Reminder to do X"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(
        inputId = "type",
        label = "Reminder Type",
        choices = c("Single Date Reminder" = "single",
                    "Multi Date Reminder" = "multi",
                    "From-To Reminder" = "from_to"),
        selected = "single", width = '100%'
      )
    ),
    mainPanel(
      conditionalPanel(
        condition = "input.type == 'single'",
        dateInput("single_date", "Enter the date for your reminder", value = Sys.Date())
      ),
      conditionalPanel(
        condition = "input.type == 'multi'",
        dateRangeInput("multi_date_1", 
                       label = "Enter from to dates", 
                       start = Sys.Date(), 
                       end = Sys.Date() + 7,
                       autoclose = TRUE)
      ),
      conditionalPanel(
        condition = "input.type == 'from_to'",
        dateRangeInput("multi_date_2", 
                       label = "Enter from to dates", 
                       start = Sys.Date(), 
                       end = Sys.Date() + 7,
                       autoclose = TRUE)
      )
    )
  )
)

shinyApp(ui, server = function(input, output) { })

并且输出:

更新

我稍微修改了您的代码并包含了一个 airDatepickerInput() 示例。因此我更改了这部分代码

     conditionalPanel(
        condition = "input.type == 'multi'",
        dateRangeInput("multi_date_1", 
                       label = "Enter from to dates", 
                       start = Sys.Date(), 
                       end = Sys.Date() + 7,
                       autoclose = TRUE)
)

对此

 conditionalPanel(
        condition = "input.type == 'multi'",
        airDatepickerInput(
          inputId = "multiple",
          label = "Select multiple dates:",
          placeholder = "You can pick 3 dates",
          multiple = 3, clearButton = TRUE
        )
      )

这是输出:

注意,如果你也想select时间,在airDatepickerInput()里面插入timepicker = TRUE