我一直在尝试创建一个简单闪亮的应用程序。我使用的数据是一个数据框架,每天的航班和乘客数量的机场。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
输入(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"))
这就是我想到的,但不幸的是我没有错误消息要报告。应用程序运行,数据范围选择器在那里并工作,但没有输出情节。"center"的页面保持空白
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的任何答案!
以下是更正后的代码
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)