R中updateDateInput中没有“datesdisabled”吗?

R中updateDateInput中没有“datesdisabled”吗?,r,shiny,R,Shiny,我在R Shining中构建了一个应用程序,它使用排除许多日期的时间序列数据。在应用程序中,用户可以选择一个新的数据集,因此可用日期将发生变化。我正在使用updateDateInput更新日期输入选择器。但是,updateDateInput似乎不允许datesdisabled函数 这是一个reprex: library(shiny) # Sample 3 dates and disable the rest my_dates <- sample(seq(as.Date('2021-0

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

这是一个reprex:

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中出错:未使用的参数日期禁用= 禁用日期

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

没错,datesdisabled参数在update函数中不可用。您可以通过将UI声明移动到服务器并使用renderUI将其提供给客户端来更改禁用的日期

该示例不声明UI中输入的日期,而是声明uiOutputdate。服务器可以使用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个原始日期。加载应用程序时,我需要禁用日期才能运行。请注意,新应用程序中的按钮单击实际上会加载一个新的数据帧,因此我不想在服务器中模拟按钮单击。我想我要问的是:有没有一种方法可以在应用程序运行时调用renderUI一次,而不必单击按钮或模拟按钮单击?如果数据来自文件,则reactiveFileReader可能会很有趣。但仅当文件不只是初始化日期时才禁用。在我的示例中,我使用了一种使用onFlush回调初始化它的方法。使用once=TRUE时,将在第一次运行时启用1、15。当然,你可以通过改变它使用的任何反应式来强制渲染。在我的示例中,禁用日期的任何更改都可以确保再次呈现UI。我进行了更改:1输入$date显示在输出框中,否则用户会感到困惑,2更改onFlush,因此初始日期选择与应用程序运行时文件中的日期相同。我考虑过reactiveFileReader,但1我仍然会遇到使用datesdisabled更新日历日期的问题,2该应用程序计算量大,所以运行速度有点慢,我不想添加任何额外的延迟时间。用户可能只会在每个会话中单击按钮更新一到两次数据。