[英]R Shiny - Dynamically filter ggplot2 chart using DateSlider
有很多這樣的相關問題,我試過他們沒有鍛煉,所以我發布了一個新問題。
我的樣本數據
Week_End Product nts
2021-10-22 A 17
2021-10-15 B 12
2021-10-08 C 18
2021-10-01 A 37
2021-09-24 B 46
2021-09-17 C 27
2021-09-10 A 31
2021-09-03 A 45
2021-08-27 B 23
2021-08-20 B 12
我使用代碼繪制了一個條形圖
server <- function(input, output,session) {
nt_data <- reactive({
chart_nts <- perf_ind %>%
filter(product %in% input$productid & (week_end >= input$start_dt & week_end <= input$end_dt)) %>%
group_by(week_end,product) %>%
summarise(c_nts = sum(nts))
})
observe({
updateSelectizeInput(session,"productid",choices = prod_dim$prod_nm)
})
output$ntsplot <- renderPlot({
dateid<-input$dateid
g <- ggplot(nt_data(),aes(y= c_nts, x = week_end))
g + geom_bar(stat = "sum")
})
}
我的 UI 代碼看起來像
sidebarLayout(position = "left",
sidebarPanel(
selectizeInput("productid", "Select product","Names"),
sliderInput("dateid",
"Slide your Date:",
min = as.Date(date_range$start_dt,"%Y-%m-%d"),
max = as.Date(date_range$end_dt,"%Y-%m-%d"),
value=as.Date(date_range$asofdate,"%Y-%m-%d"),
timeFormat="%Y-%m-%d")
),
mainPanel(
fluidRow(
splitLayout(cellWidths = c("50%", "50%"), plotOutput("ntsplot"), plotOutput(""))
)
)
)
我所需要的只是當我使用 Date Slider 時,我的圖表應該相應地改變,為此我已經做到了
output$ntplot <- renderPlot({
dateid<-input$dateid
data <- nt_data %>%
filter (week_end >= input$start_dt & week_end <= input$end_dt) %>%
g <- ggplot(data(),aes(y= c_nts, x = week_end))
g + geom_bar(stat = "sum")
})
和
nt_data <- reactive({
chart_nts <- perf_ind %>%
filter(product %in% input$productid & (week_end >= input$start_dt & week_end <= input$end_dt)) %>%
group_by(week_end,product) %>%
summarise(c_nts = sum(nts))
})
DateRange 值我從數據庫中獲取它。
當我執行我收到以下錯誤
Warning: Error in : Problem with `filter()` input `..1`.
x Input `..1` must be of size 4842 or 1, not size 0.
我在這里缺少的東西可以幫助我理解!! 謝謝你的幫助!!
不確定您想如何使用sliderInput
。 我用dateRangeInput()
替換了它。 嘗試這個
prod <- read.table(text=
"week_end product nts
2021-10-22 A 17
2021-10-15 B 12
2021-10-08 C 18
2021-10-01 A 37
2021-09-24 B 46
2021-09-17 C 27
2021-09-10 A 31
2021-09-03 A 45
2021-08-27 B 23
2021-08-20 B 12", header=T)
ui <- fluidPage(
sidebarLayout(position = "left",
sidebarPanel(
selectizeInput("productid", "Select product","Names"),
dateRangeInput("date_range", "Period you want to see:",
start = min(prod$week_end),
end = max(prod$week_end),
min = min(prod$week_end),
max = max(prod$week_end)
)#,
# sliderInput("dateid",
# "Slide your Date:",
# min = as.Date(date_range$start_dt,"%Y-%m-%d"),
# max = as.Date(date_range$end_dt,"%Y-%m-%d"),
# value=as.Date(date_range$asofdate,"%Y-%m-%d"),
# timeFormat="%Y-%m-%d")
),
mainPanel(
fluidRow(
splitLayout(cellWidths = c("50%", "50%"), plotOutput("ntplot"), DTOutput("t1"))
)
)
)
)
server <- function(input, output,session) {
nt_data <- reactive({
chart_nts <- prod %>%
dplyr::filter(product %in% input$productid & (week_end >= input$date_range[1] & week_end <= input$date_range[2])) %>%
group_by(week_end,product) %>%
dplyr::summarise(c_nts = sum(nts))
data.frame(chart_nts)
})
output$t1 <- renderDT({nt_data()})
observe({
updateSelectizeInput(session,"productid",choices = prod$product)
})
output$ntplot <- renderPlot({
#dateid<-input$dateid
data <- nt_data() # %>% dplyr::filter(week_end >= input$date_range[1] & week_end <= input$date_range[2])
g <- ggplot(data,aes(y= c_nts, x = week_end)) +
geom_bar(stat = "identity")
g
})
}
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.