-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dashboard1.R
137 lines (111 loc) · 3.76 KB
/
test_dashboard1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
# Anonimyzed/randomized data with the structure
# of a mobility data frame with information by lines
# and routes.
load(file="test_dashboard_data1.Rda")
df<- aggregate(list("CASH_TICKETS" = tdf$BOLETO,
"PREPAID" = tdf$PREPAGO,
"TRANSFERS" = tdf$TRANSFER,
"KMS" = as.numeric(tdf$KMS)),
by = list("DATE" = as.Date(tdf$HORA_SALIDA),
"ROUTE" = tdf$RUTA,
"LINE" = tdf$LINEA),
sum, na.rm = TRUE)
library(shiny)
library(plotly)
library(ggplot2)
ui <- fluidPage(
titlePanel("Mobility dashboards"),
sidebarLayout(
sidebarPanel(
radioButtons("shown_data", "Shown data:",
c("Total" = "total",
"per KM" = "km")),
br(),
checkboxGroupInput("type_mobility",
h3("Mobility type"),
choices = list("Cash tickets" = "CASH_TICKETS",
"Prepaid" = "PREPAID",
"Transfers" = "TRANSFERS"),
inline = TRUE),
br(),
dateRangeInput("dates", h3("Date range")),
br(),
checkboxGroupInput("lines",
h3("Lines"),
choices = list("GEN" = "GEN",
"NORTE" = "NORTE",
"STAR" = "STAR",
"TORO" = "TORO",
"VX" = "VX")),
br(),
downloadButton('down_csv','Download table')
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Daily mobility",plotlyOutput("plot_mob"))
,tabPanel("Table",tableOutput("table"))
)
)
)
)
server <- function(input, output) {
# Reactive expression to generate the requested distribution ----
# This is called whenever the inputs change. The output functions
# defined below then use the value computed from this expression
db <- reactive({
db<-df[,c("DATE","ROUTE","LINE","KMS",input$type_mobility)]
if(length(input$type_mobility)==1){
db$MOBILITY <- db[,input$type_mobility]
}else{
db$MOBILITY <- rowSums(db[,input$type_mobility])
}
db<-db[db$DATE >= input$dates[1] & db$DATE <= input$dates[2],
c("DATE","ROUTE","LINE","MOBILITY","KMS")]
db$DATE <- as.character(as.Date(db$DATE,format = "%Y%m%d"))
db<-db[db$LINE %in% input$lines,]
if(input$shown_data == "km"){
db$MOBILITY <- db$MOBILITY / db$KMS
}
db$KMS<-NULL
db
})
plot_mob<-reactive({
db() %>%
plot_ly(x=~DATE, y=~MOBILITY, group=~ROUTE,
type="scatter",color=~ROUTE, mode="lines+markers") %>%
config(
modeBarButtonsToRemove = list(
"zoom2d",
"pan2d",
"zoomIn2d",
"zoomOut2d",
"autoScale2d",
"resetScale2d",
"hoverClosestCartesian",
"hoverCompareCartesian",
"sendDataToCloud",
"toggleHover",
"resetViews",
"toggleSpikelines",
"resetViewMapbox"
),
displaylogo = FALSE)
})
# Generate an HTML table view of the data ----
output$table <- renderTable({
xtable::xtable(db())
})
output$plot_mob <-renderPlotly({
plot_mob()
})
output$down_csv <- downloadHandler(
filename = "data.csv"
,
content = function(file) {
write.csv(db(), file)
}
)
}
# Create Shiny app ----
shinyApp(ui, server)