在我的闪亮应用中,我为用户输入的日期范围每列选择创建了动态日期范围ui(input$daterange[i]
)。然后,我想将每个input$daterange[i]
值传递给变量,检查相应列中的哪些行在用户选择的日期范围内,我将检查结果存储在dt$range_checks[i]
中。然后在检查了所有input$daterange[1:i]
之后,我将检查结果dt$range_checks[1:i]
结合起来找出满足所有检查条件的行,此值将存储在dt$filtered_ind
中。因此,我将使用此索引来过滤满足所有日期范围要求的行的数据集。但是,我不知道我是如何做到最后一部分的,我如何结合动态检查来获得最终索引?请在下面找到我的代码:
第1部分 - 生成由用户选择的每个日期列的日期范围:
# generate daterange uis per selected input$datecols
observeEvent({
# when a new file uploaded
input$file
# when user change selection date column(s)
input$datecols}, {
dt$datecols = input$datecols
dt$datecols_len = length(dt$datecols)
# render daterange ui(s) per selected datecols
output$daterangescontrol <- renderUI({
# when input$datecols is NULL, no daterangecontrol ui
if ( is.null(dt$datecols) ) { return(NULL) }
# otherwise
else {
# whenever input$datecols change, start/end of daterange ui refresh to original state
D = dt$data
output = tagList()
req(dt$datecols_len > 0)
for (i in 1:dt$datecols_len) {
output[[i]]= tagList()
output[[i]][[1]] = tags$div(id = paste("dateranges_div", i, sep = "_"),
dateRangeInput(paste0("daterange", i),
paste("Date range of", dt$datecols[[i]]),
start = min(D[[dt$datecols[[i]]]]),
end = max(D[[dt$datecols[[i]]]])
))
}
# return output tagList() with ui elements
output
}
})
}, ignoreNULL = FALSE)
第2部分:这里是用于分配输入$ daterange数据以过滤相应列的代码:
observe({
# loop observeEvent on input$daterange1, input$daterange2...
lapply( 1:dt$datecols_len,
FUN = function(i) {
# don't call the following unless input$datecols is not of length 0
req(dt$datecols_len > 0)
# on change of input$daterange[i]
observeEvent(input[[paste0("daterange", i)]], {
# update reactive values to test whether this loop is working
dt$range[[i]] = input[[paste0("daterange", i)]]
req(dt$range[[i]])
# filter dataset with the corresonding date column:
D = dt$data[dt$cols]
col = dt$datecols[[i]]
dt$range_checks[i] = D[[col]] >= dt$range[[i]][[1]] & D[[col]] <= dt$range[[i]][[2]]
}) # end of observeEvent
}
) # end of lapply()
lapply( dt$range_checks,
FUN = function(x) {
# How to write the code to get the following desired result????
dt$filtered_ind = dt$range_checks[1] & dt$range_checks[2] ... dt$range_checks[i]
}
)
})