我已经创建了一个小的闪亮的应用程序,在这个应用程序中,用户被要求输入多少个周期/他想要削减给定的日期矢量(在2到4之间)。然后,对于用户想要的每个时间段(除了最后一个时间段),要求他/她选择该时间段的最后日期。
该应用程序正在工作,但是,我担心一些愚蠢的用户可能会选择不是递增的结束日期,例如,为时间段1选择的结束日期可能晚于为时间段2选择的结束日期,等等。
换句话说,我希望在定义cutpoint2时让用户可以选择(日期),只包含cutpoint1日期之后的日期,等等。所以,如果用户选择'2006-12-31‘作为时段1的结束日期,我希望时段2的可供用户使用的日期输入框在该日期之后开始。
然而,在这种超级动态的情况下,我甚至不确定这是否可能,因为首先,我第一次创建这些切入点输入-而用户甚至根本没有被询问日期,所以我不能让它们真正相互依赖。然后我要求用户定义切入点-然后我希望动态发挥作用。
感谢您的建议!
library(shiny)
ui = shinyUI(fluidPage(
titlePanel("Defining time periods"),
sidebarLayout(
sidebarPanel(
numericInput("num_periodsnr", label = "Desired number of time periods?",
min = 2, max = 4, value = 2),
uiOutput("period_cutpoints"),
actionButton("submit", "Update time periods")
),
mainPanel( # Just shows what was selected
textOutput("nr_of_periods"),
textOutput("end_dates")
)
)
))
server = shinyServer(function(input, output, session) {
library(lubridate)
output$nr_of_periods <- renderPrint(input$num_periodsnr)
# Dates string to select dates from:
dates <- seq(ymd('2016-01-02'), ymd('2017-12-31'), by = '1 week')
output$period_cutpoints <- renderUI({
req(input$num_periodsnr)
lapply(1:(input$num_periodsnr - 1), function(i) {
selectInput(inputId = paste0("cutpoint", i),
label = paste0("Select the last date of Time Period ", i, ":"),
choices = dates)
})
})
dates_chosen <- reactiveValues(x = NULL)
observeEvent(input$submit, {
dates_chosen$x <- list()
lapply(1:(input$num_periodsnr - 1), function(i) {
dates_chosen$x[[i]] <- input[[paste0("cutpoint", i)]]
})
})
output$end_dates <- renderText({paste(as.character(dates_chosen$x), collapse = ", ")})
})
shinyApp(ui = ui, server = server)发布于 2019-04-02 01:34:08
将以下代码插入到服务器函数中:
observe({
if(input$num_periodsnr > 2){
for(i in 2:(input$num_periodsnr - 1)) {
updateSelectInput(session, paste0("cutpoint", i), choices = dates[dates > input[[paste0("cutpoint", i-1)]]])
}
}
})由于您的lapply在每次增加周期数时创建新的selectInput,因此每当用户从3到4个切点周期时,您(无意中)会覆盖先前的结果并重置开始周期。
https://stackoverflow.com/questions/55458295
复制相似问题