Skip to content

Commit 5aaecb2

Browse files
committed
Adding full reproductive plotly support
1 parent 8a849c6 commit 5aaecb2

12 files changed

+38
-71
lines changed

R/server-functions.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,29 @@ plot_epi <- function(model, mark_max) {
210210
)
211211
return(plot)
212212
}
213+
214+
#' plot_reproductive_epi Function
215+
#'
216+
#' This function generates a plot of the reproductive number over time
217+
#' @param model The model object
218+
#'
219+
#' @return A plot displaying the reproductive number for the model over the
220+
#' course of the simulation
221+
#'
222+
#' @export
223+
plot_reproductive_epi <- function (model) {
224+
# Calculating average rep. number for each unique source_exposure_date
225+
rep_num <- get_reproductive_number(model)
226+
average_rt <- aggregate(rt ~ source_exposure_date, data = rep_num,
227+
FUN = mean)
228+
# Plotting
229+
reproductive_plot <- plot_ly(data = average_rt, x = ~source_exposure_date,
230+
y = ~rt, type = 'scatter',
231+
mode = 'lines+markers')
232+
reproductive_plot <-
233+
reproductive_plot |>
234+
plotly::layout(title = "Reproductive Number",
235+
xaxis = list(title = 'Day (step)'),
236+
yaxis = list(title = 'Average Rep. Number'))
237+
return(reproductive_plot)
238+
}

models/shiny_seir.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ shiny_seir <- function(input) {
3131
# Summary
3232
summary_seir <- function() summary(model_seir)
3333
# Reproductive Number
34-
reproductive_seir <- function()
35-
plot_reproductive_number(
36-
model_seir,
37-
main = "SEIR Model Reproductive Number"
38-
)
39-
34+
reproductive_seir <- function() plot_reproductive_epi(model_seir)
4035
# Table
4136
table_seir <- function() {
4237

models/shiny_seirconn.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,8 @@ shiny_seirconn <- function(input) {
2121

2222
# Plot, summary, and reproductive number
2323
plot_seirconn <- function() plot_epi(model_seirconn)
24-
2524
summary_seirconn <- function() summary(model_seirconn)
26-
27-
reproductive_seirconn <- function()
28-
plot_reproductive_number(
29-
model_seirconn,
30-
main = "SEIRCONNECTED Model Reproductive Number"
31-
)
32-
25+
reproductive_seirconn <- function() plot_reproductive_epi(model_seirconn)
3326
# Table
3427
table_seirconn <- function() {
3528
df <- as.data.frame(get_hist_total(model_seirconn))

models/shiny_seirconnequity.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,9 @@ shiny_seirconnequity <- function(input) {
139139
}
140140

141141
summary_seirconnequity <- function() summary(model_seirconnequity)
142+
reproductive_seirconnequity <-
143+
function() plot_reproductive_epi(model_seirconnequity)
142144

143-
reproductive_seirconnequity <- function()
144-
plot_reproductive_number(
145-
model_seirconnequity,
146-
main = "SEIRCONNECTED Model Reproductive Number"
147-
)
148-
149145
# Table
150146
table_seirconnequity <- function() {
151147
df <- as.data.frame(get_hist_total(model_seirconnequity))

models/shiny_seird.R

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,31 @@ shiny_seird <- function(input) {
3232
# Summary
3333
summary_seird <- function() summary(model_seird)
3434
# Reproductive Number
35-
reproductive_seird <- function()
36-
plot_reproductive_number(
37-
model_seird,
38-
main = "SEIRD Model Reproductive Number"
39-
)
40-
35+
reproductive_seird <- function() plot_reproductive_epi(model_seird)
4136
# Table
4237
table_seird <- function() {
43-
4438
df <- as.data.frame(get_hist_total(model_seird))
45-
4639
# Subset to only include "infection" state
4740
infection_data <- df[df$state == "Infected", ]
48-
4941
# Row with the maximum count
5042
max_infection_row <- infection_data[which.max(infection_data$counts), ]
51-
5243
# Row number of the maximum count in the original data frame
5344
max_row_number <- which(
5445
df$date == max_infection_row$date & df$state == "Infected"
5546
)
56-
5747
df[max_row_number,"counts"] <- sprintf(
5848
"<strong>%s</strong>",
5949
df[max_row_number, "counts"]
6050
)
61-
6251
# Making sure factor variables are ordered
6352
df$state <- factor(
6453
x = df$state,
6554
levels = c("Susceptible", "Exposed", "Infected", "Removed")
6655
)
67-
6856
# Reshaping the data to wide format
6957
df <- reshape(df, idvar = "date", timevar = "state", direction = "wide")
70-
7158
colnames(df) <- gsub(colnames(df), pattern = "counts.", replacement = "")
7259
df
73-
7460
}
7561

7662
# Output list

models/shiny_sir.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ shiny_sir <- function(input) {
2929
# Plot, summary and repnum
3030
plot_sir <- function() plot_epi(model_sir)
3131
summary_sir <- function() summary(model_sir)
32-
reproductive_sir <- function()
33-
plot_reproductive_number(
34-
model_sir,
35-
main = "SIR Model Reproductive Number"
36-
)
37-
32+
reproductive_sir <- function() plot_reproductive_epi(model_sir)
3833
# Table
3934
table_sir <- function() {
4035
df <- as.data.frame(get_hist_total(model_sir))

models/shiny_sirconn.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ shiny_sirconn <- function(input) {
1919

2020
# Plot, summary, and reproductive number
2121
plot_sirconn <- function() plot_epi(model_sirconn)
22-
2322
summary_sirconn <- function() summary(model_sirconn)
24-
25-
reproductive_sirconn <- function()
26-
plot_reproductive_number(
27-
model_sirconn,
28-
main = "SIRCONNECTED Model Reproductive Number"
29-
)
30-
23+
reproductive_sirconn <- function() plot_reproductive_epi(model_sirconn)
3124
# Table
3225
table_sirconn <- function() {
3326
df <- as.data.frame(get_hist_total(model_sirconn))

models/shiny_sird.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ shiny_sird <- function(input) {
3030
# Plot, summary, and repnum
3131
plot_sird <- function() plot_epi(model_sird)
3232
summary_sird <- function() summary(model_sird)
33-
reproductive_sird <- function()
34-
plot_reproductive_number(
35-
model_sird,
36-
main = "SIRD Model Reproductive Number"
37-
)
38-
33+
reproductive_sird <- function() plot_reproductive_epi(model_sird)
3934
# Table
4035
table_sird <- function() {
4136
df <- as.data.frame(get_hist_total(model_sird))

models/shiny_sis.R

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@ shiny_sis <- function(input) {
2828

2929
# Plot, summary, and reproductive number
3030
plot_sis <- function() plot_epi(model_sis)
31-
3231
summary_sis <- function() summary(model_sis)
33-
34-
reproductive_sis <- function()
35-
plot_reproductive_number(
36-
model_sis,
37-
main = "SIS Model Reproductive Number"
38-
)
39-
32+
reproductive_sis <- function() plot_reproductive_epi(model_sis)
4033
# Table
4134
table_sis <- function() {
4235
df <- as.data.frame(get_hist_total(model_sis))

models/shiny_sisd.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ shiny_sisd <- function(input) {
3030
# Plot, summary, and repnum
3131
plot_sisd <- function() plot_epi(model_sisd)
3232
summary_sisd <- function() summary(model_sisd)
33-
reproductive_sisd <- function()
34-
plot_reproductive_number(
35-
model_sisd,
36-
main = "SISD Model Reproductive Number"
37-
)
38-
33+
reproductive_sisd <- function() plot_reproductive_epi(model_sisd)
3934
# Table
4035
table_sisd <- function() {
4136
df <- as.data.frame(get_hist_total(model_sisd))

server.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function(input, output) {
4545
output$model_plot <- renderPlotly({
4646
model_output()$epicurves_plot()
4747
})
48-
output$model_reproductive_plot <- renderPlot({
48+
output$model_reproductive_plot <- renderPlotly({
4949
model_output()$reproductive_plot()
5050
})
5151
output$model_summary <- renderPrint({

ui.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ body <- dashboardBody(
9999
),
100100
fluidRow(
101101
column(6, plotlyOutput("model_plot") %>% withSpinner(color="#009bff")),
102-
column(6, plotOutput("model_reproductive_plot") %>% withSpinner(color="#009bff"))
102+
column(6, plotlyOutput("model_reproductive_plot") %>% withSpinner(color="#009bff"))
103103
),
104104
HTML("<br>"),
105105
fluidRow(

0 commit comments

Comments
 (0)