我想在這種情況下集成 dateRangeInput 嗎?
目標是選擇一個日期范圍,以便 geom_bar 都能對我的輸入做出反應。
這是我處理的資料:(https://impfdashboard.de/static/data/germany_deliveries_timeseries_v2.tsv)
library(readr)
library(shiny)
library(dplyr)
library(ggplot2)
library(magrittr)
ui <- basicPage(
# first-input
selectInput(
inputId = "sel22", label = "M?glichkeitfür Histogramm (2) ausw?hlen",
list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
),
# second-input
selectInput(inputId = "sel2", label = "M?glichkeit für Histogramm (1) ausw?hlen",
list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
),
# outpot
plotOutput("plot"),
dateRangeInput("daterange", "Date range:",
start = "2020-12-29",
end = "2021-12-09",
min = "2020-12-28",
max = "2021-12-08",
format = "mm/dd/yy",
separator = " - "),
)
server <- function(input, output, session) {
# Summarize Data and then Plot
data2 <- reactive({
req(input$sel2)
lf <- germany_vaccinations_timeseries_v2 %>% group_by(date ) %>% summarise( output = get(input$sel2))
})
datao2 <- reactive({
req(input$sel22)
lf <- germany_vaccinations_timeseries_v2 %>% group_by(date ) %>% summarise( output = get(input$sel22))
})
output$plot2 <- renderPlot({
ggplot()
geom_bar(data = data2(), aes(y = output, x = date), stat = "sum")
geom_bar(data = datao2(), aes( y = output,x = date), stat = "sum")
theme(legend.position = "none" ) labs(y= "Anzahl der Dosen", x = "Datum")
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
這是你如何做到的。
您需要看到的第一件事是您的date列dfis of classdate而不是character。
germany_vaccinations_timeseries_v2$date <- as.Date(germany_vaccinations_timeseries_v2$date,
format = "%d/%m/%Y")
我在代碼中發現的下一個錯誤是您使用的標簽selectInput。您需要指定它們以匹配df.
它應該是這樣的:
selectInput(
inputId = "sel22", label = "M?glichkeitfür Histogramm (2) ausw?hlen",
list("impfstoff", "region", "dosen", "einrichtung")
#list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
),
# second-input
selectInput(inputId = "sel2", label = "M?glichkeit für Histogramm (1) ausw?hlen",
list("impfstoff", "region", "dosen", "einrichtung")
#list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
)
整個代碼ui如下:
ui <- basicPage(
# first-input
selectInput(
inputId = "sel22", label = "M?glichkeitfür Histogramm (2) ausw?hlen",
list("impfstoff", "region", "dosen", "einrichtung")
#list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
),
# second-input
selectInput(inputId = "sel2", label = "M?glichkeit für Histogramm (1) ausw?hlen",
list("impfstoff", "region", "dosen", "einrichtung")
#list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
),
dateRangeInput(inputId = "date", "Date range:",
start = "2020-12-29",
end = "2021-02-20",
min = "2020-12-28",
max = "2021-12-08",
separator = " - "),
# outpot
plotOutput("plot")
)
為了dateRangeInput從用戶那里獲取,您可以filter這樣使用:
df <- reactive({
filter(germany_vaccinations_timeseries_v2, between(date, input$date[1], input$date[2]))
})
服務器的整個代碼:
server <- function(input, output, session) {
# Summarize Data and then Plot
df <- reactive({
filter(germany_vaccinations_timeseries_v2, between(date, input$date[1], input$date[2]))
})
data2 <- reactive({
req(input$sel2)
lf <- df() %>% group_by(date) %>% summarise(output = get(input$sel2))
})
datao2 <- reactive({
req(input$sel22)
lf <- df() %>% group_by(date) %>% summarise(output = get(input$sel22))
})
output$plot <- renderPlot({
ggplot()
geom_bar(data = data2(), aes(y = output, x = date), stat = "sum")
#geom_bar(data = datao2(), aes( y = output,x = date), stat = "sum")
theme(legend.position = "none" ) labs(y= "Anzahl der Dosen", x = "Datum")
})
}
我不明白為什么你geom_bar的代碼中有兩個。您可以將它與另一個分開繪制,plotOutput并在那里使用相同的日期范圍。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416801.html
標籤:
