【问题标题】:No "datesdisabled" in updateDateInput in R Shiny?R Shiny的updateDateInput中没有“datesdisabled”?
【发布时间】:2021-01-28 19:13:04
【问题描述】:

我在 R Shiny 中构建了一个应用程序,它使用排除许多日期的时间序列数据。在应用程序中,用户可以选择一个新数据集,因此可用日期会发生变化。我正在使用updateDateInput 来更新dateInput 选择器。但是,updateDateInput 似乎不允许datesdisabled 功能?

这是一个代表:

library(shiny)

# Sample 3 dates and disable the rest
 
my_dates <- sample(seq(as.Date('2021-01-01'), as.Date('2021-01-31'), by = "day"), 3)    
date_choices <- seq.Date(from = min(my_dates), to = max(my_dates), by = 1)
dates_disabled <- date_choices[!(date_choices %in% my_dates)]

ui <- fluidPage(
    dateInput("date", "Select Date",
              min = min(date_choices),
              max = max(date_choices),
              value = max(date_choices),
              datesdisabled = dates_disabled),
    actionButton("click", "Click Me")
)

server <- function(input, output, session) {
    observeEvent(input$click, {
        my_dates <- sample(seq(as.Date('2021-01-01'), as.Date('2021-01-31'), by = "day"), 3)
        date_choices <- seq.Date(from = min(my_dates), to = max(my_dates), by = 1)
        dates_disabled <- date_choices[!(date_choices %in% my_dates)]
        updateDateInput(
            session, 
            "date",
            min = min(date_choices),
            max = max(date_choices),
            value = max(date_choices),
            datesdisabled = dates_disabled)
    })
}

shinyApp(ui, server)

单击按钮并运行updateDateInput 时,我收到此错误:

警告:updateDateInput 中的错误:未使用的参数(datesdisabled = 日期禁用)

我猜可以选择将日期更改为字符并使用selectInput?但是我没有得到漂亮的日历!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你是对的,datesdisabled 参数在更新函数中不可用。您可以通过将 UI 声明移至服务器并使用 renderUI() 将其提供给客户端来更改禁用日期。

    该示例未在 UI 中声明日期输入,而是声明了 uiOutput("date")。服务器可以使用datesdisabled 参数动态创建dateInput。这样您就可以更改禁用日期。

    该示例将在每次单击按钮后仅选择 3 个启用日期。

    
    # Reprex: The actual implementation of this uses data from a file:
    #    1. Reads data file before ui and server are established
    #    2. Does a bunch of calculations
    #    3. Identifies dates that exist in data file
    #    4. The data file is getting updated in the background from another application.
    #    5. Allows user to click the button to update the data file. Reprex shows code
    #       that is used to update the date selector based on new data read. Dates are 
    #       random in reprex, but would come from data file in actual code.
    
    # Sample 3 dates and disable the rest - actual code reads data file here
    #   and parses out dates that exist in records
    
    my_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-31'), by = "day")
    date_choices <- sample(my_dates, 31-3)
    
    ui <- fluidPage(
        uiOutput("date"), textOutput("disabled"),
        actionButton("click", "Click Me")
    )
    
    server <- function(input, output, session) {
        dates_disabled <- reactiveVal(NULL)
        
        # Init 'dates_disabled()' once before Shiny flushes the reactive system with callback,
        #   using date_choices that exist in original data set
    
        onFlush(fun = function () {dates_disabled(date_choices)}, once = TRUE)
        
        # dateInput widget
        output$date <- renderUI({
            maxDate <- as.Date(max(setdiff(my_dates, dates_disabled())),
                               origin = "1970-01-01")
            dateInput(input = "date", 
                      label = "Select Date",
                      min = min(my_dates),
                      max = max(my_dates),
                      value = maxDate,
                      datesdisabled = dates_disabled())
        })
        
        # This output makes it easier to test if it works by showing the enabled dates
        output$disabled <- renderPrint({
            req(dates_disabled()) # only run this when 'dates_disabled' is initialized properly
            Enabled <- as.Date(setdiff(seq(as.Date('2021-01-01'), as.Date('2021-01-31'), by = "day"), 
                                       dates_disabled()), 
                               origin = '1970-01-01')
            paste("Enabled:", paste(Enabled[order(Enabled)], collapse = ", "))
        })
        
        # Set new datesdisabled on button click
        #    Actual code would read updated data file and parse new dates
        observeEvent(input$click, {
            SelectedDates <- sample(my_dates, 31-3)
            dates_disabled( SelectedDates )
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这非常接近。谢谢!但是,当应用程序初始化时,所有日期都可用,而不仅仅是 3 个“原始”日期。我需要 datesdisabled 在加载应用程序时运行。请注意,新应用程序中的按钮单击实际上会加载一个新的数据框,因此我不想模拟服务器中的按钮单击。
    • 我想我要问的是:有没有办法在应用程序运行时调用renderUI,而无需单击按钮或模拟按钮单击?
    • 如果数据来自文件,reactiveFileReader 可能会很有趣。但只有当文件没有初始化时才dates_disabled()。在我的示例中,我使用了一种方法来使用onFlush 回调对其进行初始化。使用once = TRUE,它将在第一次启用第 1 次、第 15 次时运行。和 1 月 31 日。
    • 当然,您可以通过更改它使用的任何反应表达式来强制使用renderUI。在我的示例中,dates_disabled 中的任何更改都会确保再次呈现 UI。
    • 我进行了更改:(1) input$date 显示在输出框中(否则用户会感到困惑),并且 (2) 更改了onFlush,因此初始日期选择来自应用程序运行时的文件。我考虑过reactiveFileReader 但(1)我仍然会遇到用datesdisabled 更新日历日期的问题,并且(2)该应用程序的计算量很大,所以运行速度有点慢,我不想添加任何额外的延迟时间(用户可能只会在每个会话中单击按钮更新数据一次或两次)。
    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2020-08-15
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    相关资源
    最近更新 更多