-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
198 lines (165 loc) · 6.36 KB
/
server.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#Load packages
source("load_packages.R")
## Source functions
source("pebble_game.R")
## Stop spurious warnings
options(warn = - 1)
shinyServer(function(input, output) {
## Simulation the game
## Play the game simulation
sim_sum <- reactive({
## Depend on play button
input$play_button
## Sim
sim <- multi_sim_pebble_game(r0 = input$r0,
no_in_first_gen = input$no_in_first_gen,
prop_vac = input$prop_vac,
population = input$population,
simulations = input$simulations) %>%
summarise_pebble_game_sim(simulations = input$simulations,
population = input$population,
prop_vac = input$prop_vac)
return(sim)
})
## Primary disease simulation
prim_sim <- reactive({
## depends on comparision button press
input$compare_button
prim_sim <- multi_sim_pebble_game(r0 = input$r0_prim,
no_in_first_gen = input$no_in_first_gen_com,
prop_vac = input$prop_vac_com,
population = input$population_com,
simulations = input$simulations_com) %>%
summarise_pebble_game_sim(simulations = input$simulations_com,
population = input$population_com,
prop_vac = input$prop_vac_com)
return(prim_sim)
})
## Secondary disease simulation
sec_sim <- reactive({
## depends on comparision button press
input$compare_button
sec_sim <- multi_sim_pebble_game(r0 = input$r0_sec,
no_in_first_gen = input$no_in_first_gen_com,
prop_vac = input$prop_vac_com,
population = input$population_com,
simulations = input$simulations_com) %>%
summarise_pebble_game_sim(simulations = input$simulations_com,
population = input$population_com,
prop_vac = input$prop_vac_com)
return(sec_sim)
})
## serve results table
## Play the game simulation
output$results_table <- renderDataTable({
sim_sum()
},
options = list(
pageLength = 10)
)
##Primary
output$prim_results_table <- renderDataTable({
prim_sim()
},
options = list(
pageLength = 10)
)
##Secondary
output$sec_results_table <- renderDataTable({
sec_sim()
},
options = list(
pageLength = 10)
)
## Serve results plot - playing the game
output$pebble_plot <- renderPlot({
## Run simulations, summarise and plot see pebble_game.R
plot <- sim_sum() %>%
plot_pebbles(y = input$sumstat)
plot
})
## Serve primary disease plot
output$prim_plot <- renderPlot({
## Run simulations, summarise and plot see pebble_game.R
plot <- prim_sim() %>%
plot_pebbles(y = input$sumstat_com)
plot
})
## Serve secondary disease plot
output$sec_plot <- renderPlot({
## Run simulations, summarise and plot see pebble_game.R
plot <- sec_sim() %>%
plot_pebbles(y = input$sumstat_com,
colour = "firebrick2")
plot
})
## Serve results plot for comparing disease
output$com_plot <- renderPlot({
## Bind data
com_sim <- prim_sim() %>%
mutate(Disease = "Primary") %>%
bind_rows(sec_sim() %>%
mutate(Disease = "Secondary"))
## Run simulations, summarise and plot see pebble_game.R
plot <- com_sim %>%
plot_pebbles_compare(y = input$sumstat_com)
plot
})
## Summary data tables
## Play the game
output$pebble_table <- renderTable({
## generate summary df
sim_sum() %>% summary_table(population = input$population,
prop_vac = input$prop_vac
)
})
## Primary disease
output$prim_sum_tab <- renderTable({
## generate summary df
prim_sim() %>% summary_table(population = input$population_com,
prop_vac = input$prop_vac_com
)
})
## Secondary disease
output$sec_sum_tab <- renderTable({
## generate summary df
sec_sim() %>% summary_table(population = input$population_com,
prop_vac = input$prop_vac_com
)
})
## Set up downloadbale data
## Play the game
output$downloadDatatable <- downloadHandler(filename = "pebble_game_sim_results.csv",
content = function(file) {
write.csv(sim_sum(), file)
}
)
## Primary disease
output$downloadPrimDatatable <- downloadHandler(filename = "pebble_game_prim_disease_sim_results.csv",
content = function(file) {
write.csv(prim_sim(), file)
}
)
## Secondary disease
output$downloadSecDatatable <- downloadHandler(filename = "pebble_game_sec_disease_sim_results.csv",
content = function(file) {
write.csv(sec_sim(), file)
}
)
## Set up downloadable scripts
output$downloadData1 <- downloadHandler(filename = "pebble_game.R",
content = function(file) {
file.copy("pebble_game.R", file, overwrite = TRUE)
}
)
output$downloadData2 <- downloadHandler(filename = "ui.R",
content = function(file) {
file.copy("ui.R", file, overwrite = TRUE)
}
)
output$downloadData3 <- downloadHandler(filename = "server.R",
content = function(file) {
file.copy("server.R", file, overwrite = TRUE)
}
)
})