我一直在嘗試創建一個簡單的閃亮應用程式。我使用的資料是 .xlsx 格式的機場每日航班和乘客數量的資料框,日期列采用 yyyy-mm-dd 格式,每個列對應天數、月份和年份。我希望能夠選擇一個日期范圍并接收相應的繪圖,其中 x=date(輸入日期范圍)和 y=flights。
這是頭():
# A tibble: 6 × 7
date passengers flights pas_per_flight month day year
<dttm> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 2019-01-01 00:00:00 75505 563 134. 1 1 2019
2 2019-01-02 00:00:00 92393 659 140. 1 2 2019
3 2019-01-03 00:00:00 81189 627 129. 1 3 2019
4 2019-01-04 00:00:00 83156 658 126. 1 4 2019
5 2019-01-05 00:00:00 78043 560 139. 1 5 2019
6 2019-01-06 00:00:00 87890 655 134. 1 6 2019
這是 dput(head())
structure(list(date = structure(c(1546300800, 1546387200, 1546473600,
1546560000, 1546646400, 1546732800), tzone = "UTC", class = c("POSIXct",
"POSIXt")), passengers = c(75505, 92393, 81189, 83156, 78043,
87890), flights = c(563, 659, 627, 658, 560, 655), pas_per_flight = c(134.11190053286,
140.201820940819, 129.488038277512, 126.376899696049, 139.3625,
134.18320610687), month = c("1", "1", "1", "1", "1", "1"), day = c("1",
"2", "3", "4", "5", "6"), year = c("2019", "2019", "2019", "2019",
"2019", "2019")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
這是我想出的,但不幸的是我沒有要報告的錯誤訊息。應用程式運行,日期范圍選擇器在那里并且正在作業,但沒有顯示任何情節。應用程式的“中心”只是保持空白。
library(shiny)
library(dplyr)
library(ggplot2)
library(readxl)
zhairport<-read_xlsx("airport.xlsx")
ui <- fluidPage(
sidebarLayout(
mainPanel(plotOutput("date")),
sidebarPanel(
dateRangeInput("date", "Choose timeframe", start="2019-01-01", end="2020-12-31",
min="2019-01-01",max="2020-12-31"),
)),
)
server<-function(input, output, session){
subdata<-reactive({
zhairport %>%
filter(
as.Date(date) >= as.Date(input$date[1]),
as.Date(date) <= as.Date(input$date[2]),
)
})
output$plot<-renderPlot({
ggplot(subdata(),
aes(x=date, y=flights)
geom_line(aes(x=date,y=flights)))
})
}
shinyApp(ui = ui, server = server)
為可怕的格式和可能非常糟糕的代碼道歉,這是我的第一個應用程式。
TIA 的任何答案!
uj5u.com熱心網友回復:
這是更正后的代碼
zhairport <-structure(list(date = structure(c(1546300800, 1546387200, 1546473600,
1546560000, 1546646400, 1546732800), tzone = "UTC", class = c("POSIXct", "POSIXt")), passengers = c(75505, 92393, 81189, 83156, 78043,87890), flights = c(563, 659, 627, 658, 560, 655), pas_per_flight = c(134.11190053286,140.201820940819, 129.488038277512, 126.376899696049, 139.3625,134.18320610687), month = c("1", "1", "1", "1", "1", "1"), day = c("1","2", "3", "4", "5", "6"), year = c("2019", "2019", "2019", "2019","2019", "2019")), row.names = c(NA, -6L), class = c("tbl_df","tbl", "data.frame"))
library(shiny)
library(dplyr)
library(ggplot2)
library(readxl)
ui <- fluidPage(
sidebarLayout(
mainPanel(plotOutput("plot")),
sidebarPanel(
dateRangeInput("date", "Choose timeframe", start="2019-01-01", end="2020-12-31",
min="2019-01-01",max="2020-12-31"),
)),
)
server<-function(input, output, session){
subdata<-reactive({
zhairport %>%
filter(
as.Date(date) >= as.Date(input$date[1]),
as.Date(date) <= as.Date(input$date[2]),
)
})
output$plot<-renderPlot({
data = subdata()
ggplot(data,
aes(x=date, y=flights)) geom_line(aes(x=date,y=flights))
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485236.html
上一篇:尋找有序二進制資料的華夫餅型圖表