【问题标题】:Can't see conditional choices in Shiny mainPanel?在 Shiny mainPanel 中看不到条件选择?
【发布时间】:2020-07-09 20:03:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    这是由于 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
            )
          )
    

    这是输出:

    请注意,如果您还想选择时间,请在airDatepickerInput() 中插入timepicker = TRUE

    【讨论】:

    • 非常感谢,绝对是错字!你知道如何添加日期和时间(有多个选项可以选择多个时间和日期)小部件吗? @米哈
    • 不,@miha。我的意思是我需要像 airDatepickerInput 之类的东西
    • 如果方向正确,请查看我的更新答案。
    • 我怎样才能联系到您以通过缩放向您展示情况?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2016-07-21
    • 2017-06-03
    • 2014-10-12
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多