-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
166 lines (146 loc) · 6.11 KB
/
server.R
File metadata and controls
166 lines (146 loc) · 6.11 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
library(shiny)
library(plotly)
library(tidyverse)
library(magrittr)
library(lubridate)
result <- readRDS("data.rds")
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
colours_util <- gg_color_hue(4)
count_state_changes <- function(nhrs, day, data) {
begin <- ymd_hms(paste(day, "00:00:00"),
tz = "America/Regina") + dhours(nhrs)
end <- begin + dhours(1)
output <- data %>%
transmute(registered = regtime < end & regtime >= begin,
assessed = physiciantime < end & physiciantime >= begin,
admitted = bedtime < end & bedtime >= begin,
discharged = dischargetime < end & dischargetime >= begin) %>%
summarise(nReg = sum(registered, na.rm = TRUE),
nAssessed = sum(assessed, na.rm = TRUE),
nAdmit = sum(admitted, na.rm = TRUE),
nDisch = sum(discharged, na.rm = TRUE)) %>%
mutate(Time = begin)
}
hour_data <- data.frame(TimeCensus = paste(0:23, ":00:00", sep=""))
shinyServer(function(input, output) {
hour_data_for_specific_day <- reactive({hour_data %>%
mutate(TimeCensus = ymd_hms(paste(input$date,
TimeCensus),
tz = "America/Regina")) %>%
mutate(foo = 1)})
result_per_day <- reactive({
begin <- ymd_hms(paste(input$date + ddays(0), "00:00:00"),
tz = "America/Regina")
end <- ymd_hms(paste(input$date + ddays(1), "00:00:00"),
tz = "America/Regina")
result %>%
filter(regtime < end, dischargetime >= begin)
})
subresult <- reactive({
result_per_day() %>%
filter(Site == input$hospital)
})
hourly_change_states <- reactive({
map_df(0:23, count_state_changes,
data = subresult(), day = input$date)
})
# output$censusPlot <- renderPlot({
#
# hourly_change_states() %>%
# mutate(Status = factor(Status, levels = c("nReg", "nAssessed",
# "nAdmit", "nDisch"))) %>%
# arrange(Status) %>%
# ggplot(aes(x = Time, y = Count, fill = Status)) +
# geom_area() +
# scale_fill_discrete(labels = c("Registered",
# "Assessed",
# "Admitted",
# "Discharged"),
# breaks = c("nReg", "nAssessed",
# "nAdmit", "nDisch")) +
# ggtitle(paste("Change in status for every hour on", input$date)) +
# theme(axis.text.x = element_text(angle = 45, hjust = 1),
# legend.position = "bottom") + xlab("")
#
# })
#
# output$utilPlot <- renderPlot({
#
# results_for_specific_day <- subresult() %>%
# mutate(foo = 1) %>%
# full_join(hour_data_for_specific_day(), by = 'foo') %>%
# select(-foo) %>%
# mutate(Present = as.numeric(regtime < TimeCensus &
# dischargetime >= TimeCensus),
# WaitingForBed = as.numeric(bedtime < TimeCensus &
# dischargetime >= TimeCensus)) %>%
# group_by(TimeCensus) %>%
# summarise(Census = sum(Present),
# BC4 = sum(WaitingForBed, na.rm = TRUE)) %>%
# gather("Type", "Census", Census, BC4)
#
# if (input$hospital == "SCH") {
# results_for_specific_day %<>%
# filter(Type == "Census")
# }
#
# results_for_specific_day %>%
# mutate(Type = factor(Type, levels = c("Census", "BC4"))) %>%
# arrange(Type) %>%
# ggplot(aes(x = TimeCensus, y = Census, colour = Type)) +
# geom_line() + geom_point()+
# geom_smooth(se = FALSE, method = 'loess') +
# ggtitle(paste("Hourly census on", input$date)) +
# xlab("") + theme(axis.text.x = element_text(angle = 45, hjust = 1),
# legend.position = "bottom") +
# ylim(c(0, max(results_for_specific_day$Census)))
#
# })
output$utilPlot <- renderPlotly({
p <- hourly_change_states() %>%
plot_ly(x = ~Time, y = ~nReg+nAssessed+nAdmit+nDisch,
name = 'Discharged', text = ~nDisch,
type = 'scatter', mode = 'none', hoverinfo = "text+name",
fill = 'tozeroy', fillcolor = colours_util[4]) %>%
add_trace(y = ~nReg+nAssessed+nAdmit, fillcolor = colours_util[3],
name = 'Admitted', text = ~nAdmit) %>%
add_trace(y = ~nReg+nAssessed, fillcolor = colours_util[2],
name = 'Assessed', text = ~nAssessed) %>%
add_trace(y = ~nReg, fillcolor = colours_util[1],
name = 'Registered', text = ~nReg) %>%
layout(title = paste("Change in status for every hour on", input$date),
xaxis = list(title = ""),
yaxis = list(title = "Count"),
hovermode = "x+y") %>%
config(displayModeBar = FALSE)
p
})
output$censusPlot <- renderPlotly({
results_for_specific_day <- subresult() %>%
mutate(foo = 1) %>%
full_join(hour_data_for_specific_day(), by = 'foo') %>%
select(-foo) %>%
mutate(Present = as.integer(regtime < TimeCensus &
dischargetime >= TimeCensus),
WaitingForBed = as.integer(bedtime < TimeCensus &
dischargetime >= TimeCensus)) %>%
group_by(TimeCensus) %>%
summarise(Census = sum(Present),
BC4 = sum(WaitingForBed, na.rm = TRUE)) %>%
gather("Type", "Census", Census, BC4)
results_for_specific_day %>%
mutate(Type = factor(Type, levels = c("Census", "BC4"))) %>%
arrange(Type) %>%
plot_ly(x = ~TimeCensus, y = ~Census, color = ~Type, colors = gg_color_hue(2),
type = 'scatter', mode = 'lines+markers') %>%
layout(yaxis = list(rangemode = "tozero"),
title = paste("Hourly census on", input$date),
xaxis = list(title = ""),
yaxis = list(title = "Census"),
hovermode = "x+y") %>%
config(displayModeBar = FALSE)
})
})