我有一个闪亮的应用程序,用户可以在其中上传文件。然后是一个名为EventDate
的列,其中包含日期。这些日期将传递到日期范围输入。还有第二个小部件,其将上载文件的第一列的唯一值作为输入。按下动作按钮后,数据框将根据选择器的输入进行分组。
问题是我试图根据日期范围对选择器输入值进行子集化,所以我使用:
#dat<-subset(dat, EventDate>=input$dateRange[1]&EventDate<=input$dateRange[2])
,但随后一切都空了。就像“什么都没有选择”。如何根据日期范围输入来对选择器输入值进行子集设置?
library(shiny)
library(DT)
library(shinyWidgets)
ui <- pageWithSidebar(
headerPanel('Iris k-means clustering'),
sidebarPanel(
fileInput("file1", "Choose CSV File",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv")
),
uiOutput("dr"),
uiOutput("picker"),
actionButton("go","Go")
),
mainPanel(
DTOutput("dtable")
)
)
server <- function(input, output, session) {
output$dr<-renderUI({
inFile <- input$file1
df<-data.frame(read.csv(inFile$datapath, header = TRUE))
df$EventDate <-as.Date(df$EventDate, "%Y-%m-%d")
dateRangeInput('dateRange',
label = 'Date range Input',
start = min(df$EventDate) ,end= max(df$EventDate)
)
})
filteredCSV <- reactiveVal(NULL)
CSV <- eventReactive(input[["file1"]], {
dat <- read.csv(input[["file1"]]$datapath, header = TRUE)
dat<-data.frame(subset(dat, as.Date(EventDate)>=as.Date(input$dateRange[1], "%Y-%m-%d") &
as.Date(EventDate)<=as.Date(input$dateRange[2], "%Y-%m-%d")))
filteredCSV(dat)
dat
})
output[["picker"]] <- renderUI({
req(CSV())
choices <- unique(as.character(CSV()[,1]))
pickerInput("select", "Select ID",
choices = choices,
multiple = TRUE, options = list(`actions-box` = TRUE),
selected = choices)
})
observeEvent(input[["go"]], {
req(CSV())
filteredCSV(CSV()[CSV()[,1] %in% input[["select"]],])
})
output[["dtable"]] <- renderDT({
req(filteredCSV())
datatable(
filteredCSV(),
options = list(scrollX = TRUE, pageLength = 5)
)
})
}
shinyApp(ui = ui, server = server)