-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
122 lines (90 loc) · 3.13 KB
/
app.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
#Dependencies
library(shiny)
library(COVID19)
library(incidence)
library(ggplot2)
library(dplyr)
#First, define some functions to be called in the app
#Function to convert the cumulative incidence data from the COVID19 package
#to and incidence object
#iso is a country's iso code, so DE for Germany, FR for France, etc.
incidenceData <- function(iso){
#COVID19 data for a given country
RAW.DATA <- covid19(iso)
#Convert from cumulative cases to daily cases
RAW.CASES <- diff(RAW.DATA$confirmed)
CASES <- c(RAW.DATA$confirmed[1], RAW.CASES)
#Extract the dates
DATES <- RAW.DATA$date
nDATES <- length(DATES)
#Create an empty vector of class "Date"
DATA <- integer(0)
class(DATA) <- "Date"
#Convert from daily cases to "list line" format, which contains a date
#for each case. This is the required format for the incidence package
#Clean this up later to eliminate for loop
for(i in 1:nDATES){
if(CASES[i]>0) {
DAY <- rep(DATES[i], CASES[i])
DATA <- c(DATA, DAY)
}
}
#Create the incidence object and return it
return(incidence(DATA))
}
#A function that returns different plot types based on user input
#anl is a user chosen analysis type (currently only 4 options)
#dat is a user chosen data source (currently only 2 options)
plotSwitch <- function(AN, DATASET){
ANL <- switch(AN,
"Incidence curve" = 1,
"Incidence curve + model fit" = 2
)
if(ANL == 1){
#Plot an incidence curve
plot(DATASET, border = "white")
} else if(ANL == 2) {
#Fit Log-linear models and estimate an optimal split point between
#growth and decline phases of the epidemic
FOS <- fit_optim_split(DATASET)
#Plot the incidence curve together with the fitted growth and decay curves
plot(DATASET, border = "white") %>%
add_incidence_fit(FOS$fit)
}
}
#User interface element of app
ui <- fluidPage(
pageWithSidebar(
headerPanel('Incidence-based COVID-19 Analyses'),
#Create two drop down boxes to chose (currently) country and analysis type
sidebarPanel(
selectInput('CTY', 'Country', c("France", "Germany", "Italy", "Spain"), selected="Germany"),
selectInput('ANL', 'Analysis', c("Incidence curve", "Incidence curve + model fit"), selected="Incidence curve + model fit")
),
#Add a plot based on the data and analysis type chosen
mainPanel(
plotOutput('plot1')
)
)
)
#Server component of app
server <- function(input, output, session) {
#Create a new data frame with the user-selected data source
selectedData <- reactive({
#Get the iso code based on the user's selected country
CC <- switch(input$CTY,
"France" = "FR",
"Germany" = "DE",
"Italy" = "IT",
"Spain" = "ES"
)
#Pass the iso code to incidenceData to return an incidence object
incidenceData(CC)
})
output$plot1 <- renderPlot({
#Based on the data and analysis type, return the appropriate plot
plotSwitch(input$ANL, selectedData())
})
}
#Make it a shiny app
shinyApp(ui = ui, server = server)