summaryrefslogtreecommitdiff
path: root/R/server.R
blob: a2ba674c0827f45203257976cb1e09555172f43e (plain) (blame)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#' @importFrom methods is
#' @importFrom utils read.csv write.csv
server <- function(input, output) {
  reactive <- shiny::reactiveValues(
    data_table = data.frame(Name = character(0),
                            `Reporting Frequency` = character(0),
                            `Case Counts` = numeric(0), check.names = FALSE),
    est_table = data.frame(Dataset = character(0)),
    estimators = list()
  )

  # Validate and add datasets when button is clicked.
  # Also evaluate the new datasets on existing estimators.
  shiny::observeEvent(input$addData, {
    # Option 1: Manual entry.
    if (input$dataInputMethod == 1) {
      checks_passed <- TRUE

      # Ensure the dataset name is not blank.
      if (grepl("^\\s*$", input$dataName)) {
        output$dataNameWarn <- shiny::renderText(
          "Error: The dataset name cannot be blank.")
        checks_passed <- FALSE
      }
      # Ensure the dataset name is not a duplicate.
      else if (input$dataName %in% reactive$data_table[,1]) {
        output$dataNameWarn <- shiny::renderText(
          "Error: There is already a dataset with the specified name.")
        checks_passed <- FALSE
      }
      else
        output$dataNameWarn <- shiny::renderText("")

      # Ensure the case counts consist only of positive integers, separated by
      # commas.
      counts <- as.numeric(unlist(strsplit(input$dataCounts, split = ",")))
      if (any(is.na(counts)) || any(counts <= 0) || any(counts %% 1 != 0)) {
        output$dataCountsWarn <- shiny::renderText("Error: The list of case
          counts should only contain positive integers, separated by commas.")
        checks_passed <- FALSE
      }
      # Ensure the case counts contain at least two entries.
      else if (length(counts) < 2) {
        output$dataCountsWarn <- shiny::renderText(
          "Error: The list of case counts should contain at least two entries.")
        checks_passed <- FALSE
      }
      else
        output$dataCountsWarn <- shiny::renderText("")
      
      if (checks_passed)
        d <- data.frame(input$dataName, input$dataUnits, t(counts))
    }

    else {
      checks_passed <- FALSE

      # Option 2: Upload .csv
      if (input$dataInputMethod == 2)
        d <- try(read.csv(input$dataUpload$datapath, header = FALSE))
      # Option 3: Paste .csv
      else
        d <- try(read.csv(text = input$dataPaste, header = FALSE))

      if (is(d, "try-error"))
        output$dataCSVWarn <- shiny::renderText("Error reading file.")
      else if (ncol(d) < 4 || anyNA(d[,1]) || anyNA(sapply(d[,3:4], as.numeric))
               || !all(trimws(d[,2]) %in% c("Daily", "Weekly")))
        output$dataCSVWarn <- shiny::renderText(
          "Error: The provided .csv file does not match the required format.")
      else if (length(intersect(reactive$data_table[,1], d[,1])) > 0)
        output$dataCSVWarn <- shiny::renderText("Error: The provided .csv file
          contains dataset names which already exist.")
      else if (length(unique(d[,1])) != length(d[,1]))
        output$dataCSVWarn <- shiny::renderText(
          "Error: The provided .csv file contains duplicate dataset names.")
      else {
        output$dataCSVWarn <- shiny::renderText("")
        checks_passed <- TRUE
      }
    }

    if (checks_passed) {
      d[,3:ncol(d)] <- apply(d[,3:ncol(d)], 2, as.numeric)
      d[,3] <- data.frame(I(lapply(split(d[,3:ncol(d)], 1:nrow(d)),
                                   function(x) x[!is.na(x)])))
      d <- d[,1:3]
      d[,2] <- trimws(d[,2])
      colnames(d) <- c("Name", "Reporting Frequency", "Case Counts")
      reactive$data_table <- rbind(reactive$data_table, d)
      reactive$est_table <- update_est_row(input, output, d,
        reactive$estimators, reactive$est_table)
    }
  })
  
  output$dataTable <- shiny::renderDataTable(reactive$data_table,
                                             escape = FALSE)
  output$estTable <- shiny::renderDataTable(reactive$est_table,
                                            escape = FALSE)

  # Download table of estimates as a .csv file.
  output$downloadEst <- shiny::downloadHandler(
    filename = function() { paste0("Rnaught-", Sys.Date(), ".csv") },
    content = function(file) { write.csv(reactive$est_table, file) }
  )

  shiny::observeEvent(input$addWP, {
    if (input$serialWPKnown == 1) {
      serial <- validate_serial(input, output, "serialWPInput", "serialWPWarn")
      if (!is.na(serial)) {
        reactive$estimators[[length(reactive$estimators) + 1]] <- list(
          method = "WP", mu = serial, mu_units = input$serialWPUnits,
          search = list(B = 100, shape.max = 10, scale.max = 10))
        reactive$est_table <- update_est_col(input, output, reactive$data_table,
          reactive$estimators[[length(reactive$estimators)]],
          reactive$est_table)
      }
    }
    else {
      checks_passed <- TRUE

      grid_length <- as.numeric(input$gridLengthInput)
      max_shape <- as.numeric(input$gridShapeInput)
      max_scale <- as.numeric(input$gridScaleInput)

      if (is.na(grid_length) || grid_length <= 0 || grid_length %% 1 != 0) {
        output$gridLengthWarn <- shiny::renderText(
          "Error: The grid size must be a positive integer.")
        output$gridShapeWarn <- shiny::renderText("")
        output$gridScaleWarn <- shiny::renderText("")
        checks_passed <- FALSE
      }
      else {
        output$gridLengthWarn <- shiny::renderText("")

        if (is.na(max_shape) || max_shape < 1 / grid_length) {
          output$gridShapeWarn <- shiny::renderText("Error: The maximum shape
            must be at least the reciprocal of the grid length.")
          checks_passed <- FALSE
        }
        else
          output$gridShapeWarn <- shiny::renderText("")

        if (is.na(max_scale) || max_scale < 1 / grid_length) {
          output$gridScaleWarn <- shiny::renderText("Error: The maximum scale
            must be at least the reciprocal of the grid length.")
          checks_passed <- FALSE
        }
        else
          output$gridScaleWarn <- shiny::renderText("")
      }

      if (checks_passed) {
        reactive$estimators[[length(reactive$estimators) + 1]] <- list(
          method = "WP", mu = NA, mu_units = input$serialWPUnits,
          search = list(B = grid_length, shape.max = max_shape,
                        scale.max = max_scale))
        reactive$est_table <- update_est_col(input, output, reactive$data_table,
          reactive$estimators[[length(reactive$estimators)]],
          reactive$est_table)
      }  
    }
  })

  shiny::observeEvent(input$addseqB, {
    serial <- validate_serial(input, output, "serialseqBInput",
                              "serialseqBWarn")
    checks_passed <- !is.na(serial)

    kappa <- as.numeric(input$kappaInput)
    if (is.na(kappa) || kappa <= 0) {
      output$kappaWarn <- shiny::renderText(
        "Error: The maximum value must be a positive number.")
      checks_passed <- FALSE
    }
    else
      output$kappaWarn <- shiny::renderText("")

    if (checks_passed) {
      reactive$estimators[[length(reactive$estimators) + 1]] <- list(
        method="seqB", mu = serial, kappa = kappa,
        mu_units = input$serialseqBUnits)
      reactive$est_table <- update_est_col(input, output, reactive$data_table,
        reactive$estimators[[length(reactive$estimators)]], reactive$est_table)
    }
  })

  shiny::observeEvent(input$addID, {
    serial <- validate_serial(input, output, "serialIDInput", "serialIDWarn")
    if (!is.na(serial)) {
      reactive$estimators[[length(reactive$estimators) + 1]] <- list(
        method = "ID", mu = serial, mu_units = input$serialIDUnits)
      reactive$est_table <- update_est_col(input, output, reactive$data_table,
        reactive$estimators[[length(reactive$estimators)]], reactive$est_table)
    }
  })

  shiny::observeEvent(input$addIDEA, {
    serial <- validate_serial(input, output, "serialIDEAInput",
                              "serialIDEAWarn")
    if (!is.na(serial)) {
      reactive$estimators[[length(reactive$estimators) + 1]] <- list(
        method = "IDEA", mu = serial, mu_units = input$serialIDEAUnits)
      reactive$est_table <- update_est_col(input, output, reactive$data_table,
        reactive$estimators[[length(reactive$estimators)]], reactive$est_table)
    }
  })
}

validate_serial <- function(input, output, serialInputId, serialWarnId) {
  serial <- as.numeric(input[[serialInputId]])
  if (is.na(serial) || serial <= 0) {
    output[[serialWarnId]] <- shiny::renderText(
      "Error: The mean serial interval should be a positive number.")
    serial <- NA
  }
  else
    output[[serialWarnId]] <- shiny::renderText("") # Clear warning text.

  return(serial)
}

# Create a new column in the estimator table when a new estimator is added.
update_est_col <- function(input, output, datasets, estimator, est_table) {
  if (nrow(datasets) == 0)
    new_est_table <- data.frame(matrix(nrow = 0, ncol = ncol(est_table) + 1))
  else {
    estimates <- rep(NA, nrow(datasets))

    for (row in 1:nrow(datasets))
      estimates[row] <- eval_estimator(input, output, estimator, datasets[row,])

      if (nrow(est_table) == 0)
        new_est_table <- cbind(datasets[,1], estimates)
      else
        new_est_table <- cbind(est_table, estimates)
  }

  colnames(new_est_table) <- c(colnames(est_table), shiny::HTML(
    paste0(estimator$method, "<br/>(&mu; = ", estimator$mu, " ",
           tolower(estimator$mu_units), ")")))

  return(new_est_table)
}

# Create a new row in the estimator table when new datasets are added.
update_est_row <- function(input, output, datasets, estimators, est_table) {
  if (length(estimators) == 0) {
    if (nrow(est_table) == 0)
      new_est_table <- data.frame(datasets[,1])
    else
      new_est_table <- data.frame(c(est_table[,1], datasets[,1]))

    colnames(new_est_table) <- colnames(est_table)
  }
  else {
    new_est_table <- data.frame(matrix(nrow = nrow(datasets),
                                       ncol = length(estimators)))

    for (row in 1:nrow(datasets))
      for (col in 1:length(estimators))
        new_est_table[row, col] <- eval_estimator(input, output,
          estimators[[col]], datasets[row,])

    new_est_table <- cbind(datasets[,1], new_est_table)
    colnames(new_est_table) <- colnames(est_table)
    new_est_table <- rbind(est_table, new_est_table)
  }

  return(new_est_table)
}

# Evaluate an estimator on a given dataset.
eval_estimator <- function(input, output, estimator, dataset) {
  # Adjust serial interval to match time unit of case counts.
  serial <- estimator$mu
  if (estimator$mu_units == "Days" && dataset[2] == "Weekly")
    serial <- serial / 7
  else if (estimator$mu_units == "Weeks" && dataset[2] == "Daily")
    serial <- serial * 7

  # White and Panago
  if (estimator$method == "WP") {
    estimate <- WP(unlist(dataset[3]), mu = serial, search = estimator$search)

    if (!is.na(estimator$mu))
      estimate <- round(estimate$Rhat, 2)
    # Display the estimated mean of the serial distribution if mu was not
    # specified.
    else {
      if (dataset[2] == "Daily")
        mu_units <- "days"
      else
        mu_units <- "weeks"
      MSI <- sum(estimate$SD$supp * estimate$SD$pmf)
      estimate <- shiny::HTML(paste0(round(estimate$Rhat, 2), "<br/>(&mu; = ",
                                     round(MSI, 2), " ", mu_units, ")"))
    }
  }
  # Sequential Bayes
  else if (estimator$method == "seqB")
    estimate <- round(seqB(unlist(dataset[3]), mu = serial,
                           kappa = estimator$kappa)$Rhat, 2)
  # Incidence Decay
  else if (estimator$method == "ID")
    estimate <- round(ID(unlist(dataset[3]), mu = serial), 2)
  # Incidence Decay with Exponential Adjustement
  else if (estimator$method == "IDEA")
    estimate <- round(IDEA(unlist(dataset[3]), mu = serial), 2)

  return(estimate)
}