问题
I have the shiny app delow in which the user uploads a file. Then there is a column named EventDate which contains Dates. Those dates are passed to a date range Input. There is also a second widget which takes as inputs the unique values of the 1st column of the uploaded file. When the actionbutton is pressed the data frame is subseted based on the picker input.
The issue is that Im trying to subset the picker input values based on the date range so I use:
#dat<-subset(dat, EventDate>=input$dateRange[1]&EventDate<=input$dateRange[2])
but then everything is empty. Its like 'nothing selected'. How can I subset the picker Input values based on the date range input?
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)
来源:https://stackoverflow.com/questions/61501058/how-to-subset-the-values-of-a-shiny-widget-based-on-the-values-of-another-shiny