我有下面的shiny
个应用程序,其中的数据集是由最小和最大日期子集.在我的约会中,我有一些NAS值,我想把它们作为我的selectInput()
中的 Select .但是,我不能使子集工作.
基本上应该有4个 case .未 Select NA
的一个、同时为NA
的一个、仅最小为NA
的一个和仅最大为NA
的一个
library(shiny)
library(DT)
# Creating a sample dataframe
dates <- as.Date(c("2023-01-01", "2023-02-01", NA, "2023-04-01", "2023-05-01"))
names <- c("Alice", "Bob", "Charlie", "David", "Eve")
# Combine them into a dataframe
df <- data.frame(Date = dates, Name = names)
# Generate unique dates sorted in ascending order, including NA
unique_dates <- sort(unique(df$Date), na.last = TRUE)
# Creating a Shiny app
ui <- fluidPage(
titlePanel("Subset Data by Date Range"),
sidebarLayout(
sidebarPanel(
selectInput("start_date", "Start Date", choices = unique_dates, selected = unique_dates[1]),
selectInput("end_date", "End Date", choices = unique_dates, selected = unique_dates[length(unique_dates)])
),
mainPanel(
dataTableOutput("table")
)
)
)
server <- function(input, output) {
output$table <- renderDataTable({
# Subset the dataframe based on the selected date range
subset_df <- subset(df, Date >= input$start_date & Date <= input$end_date)
datatable(subset_df)
})
}
shinyApp(ui = ui, server = server)