 # SIR into shiny app

Hello everyone,
Can you help me with code below, its SIR model calculated by euler method and I would need to write it into shiny app, so user can input the required data, which will then show the updated plot on the website:

``````
# SIR Euler method

SIR_Euler <- function(S0, I0, R0, beta, gamma, days) {
# definition of beta, gamma and delta T
b <- beta
g <- gamma
delta_t <- 0.5
D <- days

# Definition of N_time for times of aproximation (here every 30 minutes, based on the days from input function)
nt <- (D*24)/delta_t

#Numeric object + size
I <- numeric(nt)
S <- numeric(nt)
R <- numeric(nt)

mistake <- numeric(nt)

# Setting index 1 and starting value of y0, which then helps to calculate y1 ...
I <- I0
S <- S0
R <- R0
N <- I0 + S0 + R0
mistake <- N - (I0 + S0 + R0)

# loop function for calculating aproximation based on days
n=1
for (n in 1:(nt)) {
S[n+1] = S[n] + (-b*S[n]*I[n]*delta_t)
I[n+1] = I[n] + (b*S[n]*I[n]*delta_t - g*I[n]*delta_t)
R[n+1] = R[n] + g*I[n]*delta_t

# mistake when making aproximation ... mistake is from equal of N = S(t) + I(t) + R(t)
mistake[n+1] <- N - (S[n+1]+I[n+1]+R[n+1])

}

# table of values of aproximation calculation for every x in interval each step h (30 minutes <--> 0.5h)
SIR_table <- data.frame(timet = 1:(nt+1), S=S, I=I, R=R, Mistake=mistake, Population=N, Time=D)

return(SIR_table)

}

SIR_plot <- function(data_table) {
plot(data_table\$timet/48, data_table\$S, main="SIR Euler Method", ylab = "Population", xlab = "time (Days)", type="l", col="blue", xlim=c(0, data_table\$Time), ylim=c(0, data_table\$Population))

lines(data_table\$timet/48, data_table\$I, col="red")

lines(data_table\$timet/48, data_table\$R, col="green")

legend("top", c("Susceptible","Infectious","Recovered"), fill=c("blue","red","green"))
}

SIR_plot(SIR_Euler(1000, 1, 0, 0.00002, 0.002, 100))

``````

You can start at https://mastering-shiny.org/basic-app.html to see how to create a basic shiny app.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.