]> nmode's Git Repositories - Rnaught/commitdiff
Update Shiny app
authorNaeem Model <me@nmode.ca>
Sun, 26 May 2024 02:38:19 +0000 (02:38 +0000)
committerNaeem Model <me@nmode.ca>
Sun, 26 May 2024 02:38:19 +0000 (02:38 +0000)
- Refactor data upload and sample data
- Create custom data upload button
- Create script.js
- Change Shiny notification colour
- Bug fix: ensure the case counts in bulk data that have only one row are treated as a data frame, by wrapping them in 'data.frame' before passing to 'apply'

data.R [deleted file]
enter-data.html [deleted file]
inst/app/index.html
inst/app/scripts/data.R
inst/app/templates/content/data/enter-data.html
inst/app/templates/content/data/enter-data/bulk-entry.html
inst/app/templates/content/data/enter-data/load-samples.html [new file with mode: 0644]
inst/app/www/script.js [new file with mode: 0644]
inst/app/www/styles.css
sample-entry.html [deleted file]
uploadData-entry.html [deleted file]

diff --git a/data.R b/data.R
deleted file mode 100644 (file)
index a07372a..0000000
--- a/data.R
+++ /dev/null
@@ -1,363 +0,0 @@
-# Main logic block for data-related interactions.\r
-data_logic <- function(input, output, react_values) {\r
-  # Initialize a data frame to hold the datasets.\r
-  react_values$data_table <- data.frame(\r
-    Name = character(0),\r
-    `Time units` = character(0),\r
-    `Case counts` = character(0),\r
-    check.names = FALSE\r
-  )\r
-  \r
-  render_plot(input, output)\r
-  single_entry(input, output, react_values)\r
-  bulk_entry(input, output, react_values)\r
-  upload_entry(input, output, react_values)\r
-  sample_entry(input, output, react_values)\r
-  render_data(output, react_values)\r
-  delete_data(input, react_values)\r
-  export_data(output, react_values)\r
-}\r
-\r
-warnings <- function(df, df_elems) {\r
-  \r
-  warning_text <- ''\r
-  \r
-  # Ensure the dataset names are neither blank nor duplicates.\r
-  if (anyNA(df_elems[[1]]) || any(df_elems[[1]] == "")) {\r
-    warning_text <- paste0(warning_text, sep = "<br>",\r
-                           "Each row must begin with a non-blank dataset name."\r
-    )\r
-  } \r
-  \r
-  if (length(unique(df_elems[[1]])) != length(df_elems[[1]])) {\r
-    warning_text <- paste0(warning_text, sep = "<br>",\r
-                           "The rows contain duplicate dataset names."\r
-    )\r
-  }\r
-  \r
-  if (any(df_elems[[1]] %in% react_values$data_table[, 1])) {\r
-    warning_text <- paste0(warning_text, sep = "<br>",\r
-                           "The rows contain dataset names which already exist."\r
-    )\r
-  }\r
-  # Ensure the second entry in each row is a time unit equal to\r
-  # "Days" or "Weeks".\r
-  if (!all(df_elems[[2]] %in% c("Days", "Weeks"))) {\r
-    warning_text <- paste0(warning_text, sep = "<br>",\r
-                           "The second entry in each row must be either 'Days' or 'Weeks'."\r
-    )\r
-  }\r
-  # Ensure the counts in each row have at least one non-negative integer.\r
-  if (any(df_elems[[3]] == "")) {\r
-    warning_text <- paste0(warning_text, sep = "<br>",\r
-                           "Each row must contain at least one non-negative integer."\r
-    )\r
-  }\r
-  return(warning_text)\r
-}\r
-\r
-\r
-# Convert the input case counts string to an integer vector.\r
-tokenize_counts <- function(counts_str) {\r
-  suppressWarnings(as.integer(unlist(strsplit(trimws(counts_str), ","))))\r
-}\r
-\r
-# Render the preview plot for single entry data.\r
-render_plot <- function(input, output) {\r
-  observe({\r
-    counts <- tokenize_counts(input$data_counts)\r
-    if (length(counts) > 0 && !anyNA(counts) && all(counts >= 0)) {\r
-      output$data_plot <- renderPlot(\r
-        plot(seq_along(counts) - 1, counts, type = "o", pch = 16, col = "black",\r
-             xlab = input$data_units, ylab = "Cases", cex.lab = 1.5,\r
-             xlim = c(0, max(length(counts) - 1, 1)), ylim = c(0, max(counts, 1))\r
-        )\r
-      )\r
-    } else {\r
-      output$data_plot <- renderPlot(\r
-        plot(NULL, xlim = c(0, 10), ylim = c(0, 10),\r
-             xlab = input$data_units, ylab = "Cases", cex.lab = 1.5\r
-        )\r
-      )\r
-    }\r
-  })\r
-}\r
-\r
-# Add a single dataset to the existing table.\r
-single_entry <- function(input, output, react_values) {\r
-  observeEvent(input$data_single, {\r
-    valid <- TRUE\r
-    \r
-    # Ensure the dataset name is neither blank nor a duplicate.\r
-    name <- trimws(input$data_name)\r
-    if (name == "") {\r
-      output$data_name_warn <- renderText("The dataset name cannot be blank.")\r
-      valid <- FALSE\r
-    } else if (name %in% react_values$data_table[, 1]) {\r
-      output$data_name_warn <- renderText(\r
-        "There is already a dataset with the specified name."\r
-      )\r
-      valid <- FALSE\r
-    } else {\r
-      output$data_name_warn <- renderText("")\r
-    }\r
-    \r
-    # Ensure the case counts are specified as a comma-separated of one or more\r
-    # non-negative integers.\r
-    counts <- tokenize_counts(input$data_counts)\r
-    if (length(counts) == 0) {\r
-      output$data_counts_warn <- renderText("Case counts cannot be blank.")\r
-      valid <- FALSE\r
-    } else if (anyNA(counts) || any(counts < 0)) {\r
-      output$data_counts_warn <- renderText(\r
-        "Case counts can only contain non-negative integers."\r
-      )\r
-      valid <- FALSE\r
-    } else {\r
-      output$data_counts_warn <- renderText("")\r
-    }\r
-    \r
-    if (valid) {\r
-      # Add the new dataset to the data table.\r
-      new_row <- data.frame(name, input$data_units, toString(counts))\r
-      colnames(new_row) <- c("Name", "Time units", "Case counts")\r
-      react_values$data_table <- rbind(react_values$data_table, new_row)\r
-      \r
-      # Evaluate all existing estimators on the new dataset and update the\r
-      # corresponding row in the estimates table.\r
-      update_estimates_rows(new_row, react_values)\r
-      \r
-      showNotification("Dataset added successfully.",\r
-                       duration = 3, id = "notify-success"\r
-      )\r
-    }\r
-  })\r
-}\r
-\r
-# Add multiple datasets to the existing table.\r
-bulk_entry <- function(input, output, react_values) {\r
-  observeEvent(input$data_bulk, {\r
-    tryCatch(\r
-      {\r
-        datasets <- read.csv(text = input$data_area, header = FALSE, sep = ",")\r
-        \r
-        names <- trimws(datasets[, 1])\r
-        units <- trimws(datasets[, 2])\r
-        counts <- apply(datasets[, 3:ncol(datasets)], 1,\r
-                        function(row) {\r
-                          row <- suppressWarnings(as.integer(row))\r
-                          toString(row[!is.na(row) & row >= 0])\r
-                        }\r
-        )\r
-        output$data_area_warn <- renderText("")\r
-        warning_text <- warnings(datasets, list(names, units, counts))\r
-        \r
-        if (warning_text == "") {\r
-          # Add the new datasets to the data table.\r
-          new_rows <- data.frame(names, units, counts)\r
-          colnames(new_rows) <- c("Name", "Time units", "Case counts")\r
-          react_values$data_table <- rbind(react_values$data_table, new_rows)\r
-          \r
-          # Evaluate all existing estimators on the new dataset and update the\r
-          # corresponding row in the estimates table.\r
-          update_estimates_rows(new_rows, react_values)\r
-          \r
-          showNotification("Datasets added successfully.",\r
-                           duration = 3, id = "notify-success"\r
-          )\r
-        } else {\r
-          output$data_area_warn <- renderUI(HTML(warning_text))\r
-        }\r
-      },\r
-      error = function(e) {\r
-        output$data_area_warn <- renderText(\r
-          "The input does not match the required format."\r
-        )\r
-      }\r
-    )\r
-  })\r
-}\r
-\r
-# Upload datasets to the existing table.\r
-upload_entry <- function(input, output, react_values) {\r
-  observeEvent(input$data_load, {\r
-    tryCatch(\r
-      {\r
-        df <- read.csv(file = input$upload_csv$datapath)\r
-        names <- trimws(df[, 1])\r
-        units <- trimws(df[, 2])\r
-        counts <- sapply(tokenize_counts(df[, 3:ncol(df)]),\r
-                         function(row) {\r
-                           row <- suppressWarnings(as.integer(row))\r
-                           toString(row[!is.na(row) & row >= 0])\r
-                         }\r
-        )\r
-        output$data_load_warn <- renderText("")\r
-        warning_text <- ''\r
-        warning_text <- warnings(df, list(names, units, counts))\r
-        \r
-        if (warning_text == "") {\r
-          \r
-          # Add the new datasets to the data table.\r
-          new_rows <- read.csv(file = input$upload_csv$datapath)\r
-          colnames(new_rows) <- c("Name", "Time units", "Case counts")\r
-          react_values$data_table <- rbind(react_values$data_table, new_rows)\r
-          \r
-          # Evaluate all existing estimators on the new dataset and update the\r
-          # corresponding row in the estimates table.\r
-          update_estimates_rows(new_rows, react_values)\r
-          \r
-          showNotification("Datasets added successfully.",\r
-                           duration = 3, id = "notify-success")\r
-          \r
-          \r
-        } else {\r
-          output$data_load_warn <- renderUI(HTML(warning_text))        \r
-        }\r
-      },\r
-      error = function(e) {\r
-        output$data_load_warn <- renderText(\r
-          "The input does not match the required format."\r
-        )\r
-      }\r
-    )\r
-  })\r
-}\r
-\r
-# Add sample datasets to the existing table.\r
-sample_entry <- function(input, output, react_values) {\r
-  observeEvent(input$sample_entry, {\r
-    tryCatch(\r
-      {\r
-        # datasets <- read.csv(text = input$sample, header = FALSE, sep = ",")\r
-        \r
-        names <- c()\r
-        units <- c()\r
-        counts <-c()\r
-        \r
-        if (input$march){\r
-          names <- append(names, c("Covid-19 March 2020"))\r
-          units<-append(units, c("Daily"))\r
-          counts<-append(counts,c(covid_cases[1]))}\r
-        if (input$april){ names <- append(names, c("Covid-19 April 2020"))\r
-        units<-append(units, c("Daily"))\r
-        counts<-append(counts,c(covid_cases[2]))}\r
-        if (input$may){ names <- append(names, c("Covid-19 May 2020"))\r
-        units<-append(units, c("Daily"))\r
-        counts<-append(counts,c(covid_cases[3]))}\r
-        \r
-        \r
-        if (input$june){ names <- append(names, c("Covid-19 June 2020"))\r
-        units<-append(units, c("Daily"))\r
-        counts<-append(counts,c(covid_cases[4]))}\r
-        \r
-        if (input$july){ names <- append(names, c("Covid-19 July 2020"))\r
-        units<-append(units, c("Daily"))\r
-        counts<-append(counts,c(covid_cases[5]))}\r
-        \r
-        warning_text <- ""\r
-        \r
-        # Ensure the dataset names are not duplicates.\r
-        \r
-        \r
-        if (any(names %in% react_values$data_table[, 1])) {\r
-          warning_text <- paste0(warning_text, sep = "<br>",\r
-                                 "The rows contain dataset names which already exist."\r
-          )\r
-          \r
-        }\r
-        \r
-        \r
-        output$sample_area_warn <- renderUI(HTML(warning_text))\r
-        \r
-        if (warning_text == "") {\r
-          # Add the new datasets to the data table.\r
-          \r
-          new_rows <- data.frame(names, units, counts)\r
-          colnames(new_rows) <- c("Name", "Time units", "Case counts")\r
-          react_values$data_table <- rbind(react_values$data_table, new_rows)\r
-          \r
-          # Evaluate all existing estimators on the new dataset and update the\r
-          # corresponding row in the estimates table.\r
-          update_estimates_rows(new_rows, react_values)\r
-          \r
-          showNotification("Datasets added successfully.",\r
-                           duration = 3, id = "notify-success"\r
-          )\r
-        }\r
-      }\r
-    )\r
-  })\r
-}\r
-\r
-# Render the data table when new datasets are added.\r
-render_data <- function(output, react_values) {\r
-  observe({\r
-    output$data_table <- DT::renderDataTable(react_values$data_table)\r
-  })\r
-}\r
-\r
-# Delete rows in the data table,\r
-# and the corresponding rows in the estimates table.\r
-delete_data <- function(input, react_values) {\r
-  observeEvent(input$data_delete, {\r
-    new_table <- react_values$data_table[-input$data_table_rows_selected, ]\r
-    if (nrow(new_table) > 0) {\r
-      rownames(new_table) <- seq_len(nrow(new_table))\r
-    }\r
-    react_values$data_table <- new_table\r
-    \r
-    if (ncol(react_values$estimates_table) == 1) {\r
-      react_values$estimates_table <- data.frame(\r
-        Datasets = react_values$data_table[, 1]\r
-      )\r
-    } else {\r
-      react_values$estimates_table <-\r
-        react_values$estimates_table[-input$data_table_rows_selected, ]\r
-    }\r
-  })\r
-}\r
-\r
-# Export data table as a CSV file.\r
-export_data <- function(output, react_values) {\r
-  output$data_export <- downloadHandler(\r
-    filename = function() {\r
-      paste0("Rnaught_data_", format(Sys.time(), "%y-%m-%d_%H-%M-%S"), ".csv")\r
-    },\r
-    content = function(file) {\r
-      write.csv(react_values$data_table, file, row.names = FALSE)\r
-    }\r
-  )\r
-}\r
-\r
-# When new datasets are added, evaluate all existing estimators on them and\r
-# add new rows to the estimates table.\r
-update_estimates_rows <- function(datasets, react_values) {\r
-  new_rows <- data.frame(\r
-    matrix(nrow = nrow(datasets), ncol = ncol(react_values$estimates_table))\r
-  )\r
-  colnames(new_rows) <- colnames(react_values$estimates_table)\r
-  \r
-  for (row in seq_len(nrow(datasets))) {\r
-    new_rows[row, 1] <- datasets[row, 1]\r
-    \r
-    if (length(react_values$estimators) > 0) {\r
-      for (col in 2:ncol(react_values$estimates_table)) {\r
-        new_rows[row, col] <- eval_estimator(\r
-          react_values$estimators[[col - 1]], datasets[row, ]\r
-        )\r
-      }\r
-    }\r
-  }\r
-  \r
-  react_values$estimates_table <- rbind(\r
-    react_values$estimates_table, new_rows\r
-  )\r
-}\r
-#Sample datasets case counts\r
-covid_cases = c("7,1,13,10,6,10,13,28,47,53,62,90,88,130,143,150,186,276,279,350,458,604,570,667,878,883,785,1085,1252",\r
-                "1469,1278,1346,1119,1109,1120,1202,1429,1178,1337,1165,1312,1551,1633,1870,1688,1888,1702,1535,1549,1563,1583,1777,1511,1482,1298,1350,1422,1502,1546",\r
-                "1499,1330,1232,1205,1101,1306,1317,1187,1115,997,953,903,1086,1101,1198,1133,1219,1057,954,1061,1056,1094,922,884,963,660,762,781,1038,763,827",\r
-                "678,656,602,545,557,497,464,411,391,481,402,427,380,322,309,345,358,375,373,300,315,340,288,297,280,330,344,358,242,267",\r
-                "315,291,267,284,244,220,269,313,359,343,348,351,277,362,451,443,517,490,457,472,507,509,573,497,425,408,344,493,405,466,455")\r
-\r
diff --git a/enter-data.html b/enter-data.html
deleted file mode 100644 (file)
index 0ff82f2..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<div class="row mb-5">\r
-  <form class="col-md">\r
-    {{ htmlTemplate("templates/content/data/enter-data/single-entry.html") }}\r
-  </form>\r
-  <div class="col-md mt-5 mt-md-0 d-flex flex-column align-items-center">\r
-    <h5>Data plot</h5>\r
-    {{ plotOutput(outputId = "data_plot") }}\r
-  </div>\r
-</div>\r
-<hr>\r
-<form>\r
-  {{ htmlTemplate("templates/content/data/enter-data/bulk-entry.html") }}\r
-</form>\r
-<hr>\r
-<form>\r
-  {{ htmlTemplate("templates/content/data/enter-data/uploadData-entry.html") }}\r
-</form>\r
-<form>\r
-  {{ htmlTemplate("templates/content/data/enter-data/sample-entry.html") }}\r
-</form>
\ No newline at end of file
index b0f12455a8804f87e333f80372d1abedc5b484b3..504918d998141df1f54588c7a8f94888c159ae4d 100644 (file)
@@ -5,12 +5,7 @@
     {{ headContent() }}
     <title>Rnaught Web</title>
     <link rel="stylesheet" type="text/css" href="styles.css">
-    <!-- Enable tooltips. -->
-    <script>
-      $(document).ready(function(){
-        $('[data-bs-toggle="tooltip"]').tooltip();
-      });
-    </script>
+    <script src="script.js"></script>
   </head>
   <body class="d-flex flex-column h-100">
     <noscript>
index eebd14fd6ba4bd830c68915ffa5436ba85d5690d..02b57c569700b4f797a72f9c9151baccd65a38bb 100644 (file)
@@ -10,7 +10,9 @@ data_logic <- function(input, output, react_values) {
 
   render_plot(input, output)
   single_entry(input, output, react_values)
-  bulk_entry(input, output, react_values)
+  manual_bulk_entry(input, output, react_values)
+  upload_data(input, output, react_values)
+  load_samples(input, output, react_values)
   render_data(output, react_values)
   delete_data(input, react_values)
   export_data(output, react_values)
@@ -93,80 +95,143 @@ single_entry <- function(input, output, react_values) {
   })
 }
 
-# Add multiple datasets to the existing table.
-bulk_entry <- function(input, output, react_values) {
+manual_bulk_entry <- function(input, output, react_values) {
   observeEvent(input$data_bulk, {
-    tryCatch(
-      {
-        datasets <- read.csv(text = input$data_area, header = FALSE, sep = ",")
+    validate_bulk_data(input, output, react_values, "data_area")
+  })
+}
 
-        names <- trimws(datasets[, 1])
-        units <- trimws(datasets[, 2])
-        counts <- apply(datasets[, 3:ncol(datasets)], 1,
-          function(row) {
-            row <- suppressWarnings(as.integer(row))
-            toString(row[!is.na(row) & row >= 0])
-          }
-        )
+upload_data <- function(input, output, react_values) {
+  observeEvent(input$data_upload, {
+    validate_bulk_data(input, output, react_values, "data_upload")
+  })
+}
 
-        warning_text <- ""
+validate_bulk_data <- function(input, output, react_values, data_source) {
+  tryCatch(
+    {
+      if (data_source == "data_area") {
+        datasets <- read.csv(text = input$data_area, header = FALSE, sep = ",")
+      } else if (data_source == "data_upload") {
+        datasets <- read.csv(
+          file = input$data_upload$datapath, header = FALSE, sep = ","
+        )
+      }
 
-        # Ensure the dataset names are neither blank nor duplicates.
-        if (anyNA(names) || any(names == "")) {
-          warning_text <- paste0(warning_text, sep = "<br>",
-            "Each row must begin with a non-blank dataset name."
-          )
-        } else {
-          if (length(unique(names)) != length(names)) {
-            warning_text <- paste0(warning_text, sep = "<br>",
-              "The rows contain duplicate dataset names."
-            )
-          }
-          if (any(names %in% react_values$data_table[, 1])) {
-            warning_text <- paste0(warning_text, sep = "<br>",
-              "The rows contain dataset names which already exist."
-            )
-          }
+      names <- trimws(datasets[, 1])
+      units <- trimws(datasets[, 2])
+      counts <- apply(data.frame(datasets[, 3:ncol(datasets)]), 1,
+        function(row) {
+          row <- suppressWarnings(as.integer(row))
+          toString(row[!is.na(row) & row >= 0])
         }
+      )
 
-        # Ensure the second entry in each row is a time unit equal to
-        # "Days" or "Weeks".
-        if (!all(units %in% c("Days", "Weeks"))) {
-          warning_text <- paste0(warning_text, sep = "<br>",
-            "The second entry in each row must be either 'Days' or 'Weeks'."
+      warning_text <- ""
+
+      # Ensure the dataset names are neither blank nor duplicates.
+      if (anyNA(names) || any(names == "")) {
+        warning_text <- paste0(warning_text,
+          "Each row must begin with a non-blank dataset name.<br>"
+        )
+      } else {
+        if (length(unique(names)) != length(names)) {
+          warning_text <- paste0(warning_text,
+            "The rows contain duplicate dataset names.<br>"
           )
         }
-
-        # Ensure the counts in each row have at least one non-negative integer.
-        if (any(counts == "")) {
-          warning_text <- paste0(warning_text, sep = "<br>",
-            "Each row must contain at least one non-negative integer."
+        if (any(names %in% react_values$data_table[, 1])) {
+          warning_text <- paste0(warning_text,
+            "The rows contain dataset names which already exist.<br>"
           )
         }
+      }
 
-        output$data_area_warn <- renderUI(HTML(warning_text))
+      # Ensure the second entry in each row is a time unit equal to
+      # "Days" or "Weeks".
+      if (!all(units %in% c("Days", "Weeks"))) {
+        warning_text <- paste0(warning_text,
+          "The second entry in each row must be either 'Days' or 'Weeks'.<br>"
+        )
+      }
 
-        if (warning_text == "") {
-          # Add the new datasets to the data table.
-          new_rows <- data.frame(names, units, counts)
-          colnames(new_rows) <- c("Name", "Time units", "Case counts")
-          react_values$data_table <- rbind(react_values$data_table, new_rows)
+      # Ensure the counts in each row have at least one non-negative integer.
+      if (any(counts == "")) {
+        warning_text <- paste0(warning_text,
+          "Each row must contain at least one non-negative integer.<br>"
+        )
+      }
 
-          # Evaluate all existing estimators on the new dataset and update the
-          # corresponding row in the estimates table.
-          update_estimates_rows(new_rows, react_values)
+      output[[paste0(data_source, "_warn")]] <- renderUI(HTML(warning_text))
 
-          showNotification("Datasets added successfully.",
-            duration = 3, id = "notify-success"
-          )
-        }
-      },
-      error = function(e) {
-        output$data_area_warn <- renderText(
-          "The input does not match the required format."
+      if (warning_text == "") {
+        # Add the new datasets to the data table.
+        new_rows <- data.frame(names, units, counts)
+        colnames(new_rows) <- c("Name", "Time units", "Case counts")
+        react_values$data_table <- rbind(react_values$data_table, new_rows)
+
+        # Evaluate all existing estimators on the new datasets and update the
+        # corresponding rows in the estimates table.
+        update_estimates_rows(new_rows, react_values)
+
+        showNotification("Datasets added successfully.",
+          duration = 3, id = "notify-success"
         )
       }
-    )
+    },
+    error = function(e) {
+      output[[paste0(data_source, "_warn")]] <- renderText(
+        "The input does not match the required format."
+      )
+    }
+  )
+}
+
+# Load sample datasets.
+load_samples <- function(input, output, react_values) {
+  observeEvent(input$data_samples, {
+    names <- c()
+    units <- c()
+    counts <- c()
+
+    # COVID-19 Canada, March 2020 (weekly).
+    if (input$covid_canada) {
+      names <- c(names, "COVID-19 Canada 2020/03/03 - 2020/03/31")
+      units <- c(units, "Weeks")
+      counts <- c(counts, toString(Rnaught::COVIDCanada[seq(41, 69, 7), 2]))
+    }
+    # COVID-19 Ontario, March 2020 (weekly).
+    if (input$covid_ontario) {
+      names <- c(names, "COVID-19 Ontario 2020/03/03 - 2020/03/31")
+      units <- c(units, "Weeks")
+      counts <- c(counts,
+        toString(Rnaught::COVIDCanadaPT[seq(10176, 10204, 7), 3])
+      )
+    }
+
+    if (length(names) == 0) {
+      output$data_samples_warn <- renderText(
+        "At least one sample dataset must be selected."
+      )
+    } else if (any(names %in% react_values$data_table[, 1])) {
+      output$data_samples_warn <- renderText(
+        "At least one of the selected dataset names already exist."
+      )
+    } else {
+      output$data_samples_warn <- renderText("")
+
+      new_rows <- data.frame(names, units, counts)
+      colnames(new_rows) <- c("Name", "Time units", "Case counts")
+      react_values$data_table <- rbind(react_values$data_table, new_rows)
+
+      # Evaluate all existing estimators on the sample datasets and update the
+      # corresponding rows in the estimates table.
+      update_estimates_rows(new_rows, react_values)
+
+      showNotification("Datasets added successfully.",
+        duration = 3, id = "notify-success"
+      )
+    }
   })
 }
 
index 621c7859bbf2700622ac3023e1f486315d4b2f7d..f4d5e756508b13f2021a0c0d2eee82388b6fafd3 100644 (file)
@@ -8,6 +8,10 @@
   </div>
 </div>
 <hr>
-<form>
+<form class="mb-5">
   {{ htmlTemplate("templates/content/data/enter-data/bulk-entry.html") }}
 </form>
+<hr>
+<form>
+  {{ htmlTemplate("templates/content/data/enter-data/load-samples.html") }}
+</form>
index 82a3ccf7d7ab2e251097e6cc05bfe4f8903fd618..30fab065275898cfd7a8f451bd7ae45c1b62dc3a 100644 (file)
@@ -1,13 +1,11 @@
 <h4 class="mb-3">Bulk entry</h4>
 <!-- Button to toggle help text. -->
 <button type="button" class="btn btn-outline-primary btn-sm" id="bulk-help-toggle"
-        data-bs-toggle="collapse" data-bs-target="#bulk-help">
-    Show required format
-</button>
+        data-bs-toggle="collapse" data-bs-target="#bulk-help">Show required format</button>
 <!-- Help text for bulk input format. -->
 <div class="collapse mt-2" id="bulk-help">
   <div class="card card-body border-primary">
-    <p>Enter one or more rows in the following format:</p>
+    <p>Manually enter rows or upload a CSV file in the following format:</p>
     <p class="overflow-x-scroll text-nowrap font-monospace">
       <u>Dataset name</u>,<u>Time units</u>,<u>Case counts</u>
     </p>
   </div>
 </div>
 <!-- Data input area. -->
-<div>
-  <textarea id="data_area" class="form-control shiny-input-textarea my-3" rows="3" wrap="off"></textarea>
-  <small id="data_area_warn" class="form-text text-primary shiny-html-output"></small>
+<div class="my-4">
+  <label class="form-label" for="data_area">Enter manually</label>
+  <textarea id="data_area" class="form-control" rows="3" wrap="off"></textarea>
+  <div>
+    <small id="data_area_warn" class="form-text text-primary shiny-html-output"></small>
+  </div>
+  <button id="data_bulk" type="button" class="btn btn-outline-primary btn-sm action-button mt-3">
+    <span class="glyphicon glyphicon-plus"></span> Add
+  </button>
+</div>
+<!-- File input for data upload (hidden). -->
+<input class="form-control" type="file" id="data_upload" accept="text/csv,text/comma-separated-values,text/plain,.csv">
+<!-- Custom button to trigger file selector for data upload (visible). -->
+<label class="form-label" for="data-upload-select">Upload a CSV file</label>
+<div class="input-group">
+  <button id="data-upload-select" type="button" class="btn btn-outline-primary btn-sm">
+    <span class="glyphicon glyphicon-file"></span> Select file
+  </button>
+  <input type="text" id="data-upload-name" class="form-control" placeholder="No file selected" disabled>
 </div>
-<!-- Submit data. -->
-<button id="data_bulk" type="button" class="btn btn-outline-primary btn-sm action-button">
-  <span class="glyphicon glyphicon-plus"></span> Add
-</button>
+<small id="data_upload_warn" class="form-text text-primary shiny-html-output"></small>
diff --git a/inst/app/templates/content/data/enter-data/load-samples.html b/inst/app/templates/content/data/enter-data/load-samples.html
new file mode 100644 (file)
index 0000000..a42a8c8
--- /dev/null
@@ -0,0 +1,17 @@
+<h4 class="mb-3">Load samples</h4>
+{{
+  checkboxInput(inputId = "covid_canada", label = "COVID-19 Canada, 2020/03/03 - 2020/03/31 (Weekly)",
+    value = FALSE, width = "100%"
+  )
+}}
+{{
+  checkboxInput(inputId = "covid_ontario", label = "COVID-19 Ontario, 2020/03/03 - 2020/03/31 (Weekly)",
+    value = FALSE, width = "100%"
+  )
+}}
+<div>
+  <small id="data_samples_warn" class="form-text text-primary shiny-text-output"></small>
+</div>
+<button id="data_samples" type="button" class="btn btn-outline-primary btn-sm action-button mt-3">
+  <span class="glyphicon glyphicon-plus"></span> Add
+</button>
diff --git a/inst/app/www/script.js b/inst/app/www/script.js
new file mode 100644 (file)
index 0000000..c31584f
--- /dev/null
@@ -0,0 +1,21 @@
+$(document).ready(() => {
+  // Enable tooltips.
+  $('[data-bs-toggle="tooltip"]').tooltip();
+
+  // Toggle the text in the bulk data help button.
+  $('#bulk-help-toggle').on('click', event => {
+    btn = $(event.target);
+    show_format = 'Show required format';
+    btn.text(btn.text() === show_format ? 'Hide required format' : show_format);
+  });
+
+  // Trigger the file selector via a custom button.
+  $('#data-upload-select').on('click', () => {
+    $('#data_upload').trigger('click');
+  });
+
+  // Display the name of the uploaded file.
+  $('#data_upload').on('change', event => {
+    $('#data-upload-name').attr('placeholder', event.target.files[0].name);
+  });
+});
index f6c4407851f4d7d59a9a8d577232ab4ccb731cd8..d53a38745b291588e9cc9a0d0d49608dde6a3661 100644 (file)
@@ -20,7 +20,11 @@ noscript {
   margin-top: -0.5rem;
 }
 
-td.selected {
+td.selected, .shiny-notification {
   background-color: black;
   color: white;
 }
+
+#data_upload {
+  display: none;
+}
diff --git a/sample-entry.html b/sample-entry.html
deleted file mode 100644 (file)
index 92b9131..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-<h4 class="mb-3">Sample entry</h4>\r
-<!-- Checkboxes -->\r
-\r
-<div>\r
-  \r
-<input type="checkbox" id="march" name="myCheckbox" value="isChecked">\r
-<label for="myCheckbox">Covid-19 March 2020</label>\r
-\r
-</div>\r
-<div>\r
-  \r
-<input type="checkbox" id="april" name="myCheckbox" value="isChecked">\r
-<label for="myCheckbox">Covid-19 April 2020</label>\r
-\r
-</div>\r
-<div>\r
-  \r
-<input type="checkbox" id="may" name="myCheckbox" value="isChecked">\r
-<label for="myCheckbox">Covid-19 May 2020</label>\r
-</div>\r
-\r
-<div>\r
-  \r
-<input type="checkbox" id="june" name="myCheckbox" value="isChecked">\r
-<label for="myCheckbox">Covid-19 June 2020</label></div>\r
-<div><input type="checkbox" id="july" name="myCheckbox" value="isChecked">\r
-<label for="myCheckbox">Covid-19 July 2020</label></div>\r
-\r
-\r
-<div>\r
-\r
-<!-- Submit button -->\r
-<button id="sample_entry" type="button" class="btn btn-outline-primary btn-sm action-button">\r
-  <span class="glyphicon glyphicon-plus"></span> Add\r
-</button>\r
-</div>\r
-<!-- Warning area. -->\r
-<div>\r
-  <small id="sample_area_warn" class="form-text text-primary shiny-html-output"></small>\r
-</div>\r
diff --git a/uploadData-entry.html b/uploadData-entry.html
deleted file mode 100644 (file)
index c1b05c8..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<h4>Upload Data</h4>\r
-<div class="my-3">\r
-  <div id="fileInput" class="col-md mt-5 mt-md-0 d-flex flex-column align-items-left">\r
-    {{ fileInput('upload_csv', 'Choose CSV File', multiple = F, accept= '.csv') }}\r
-  </div>\r
-    <small id="data_load_warn" class="form-text text-primary shiny-html-output"></small>\r
-</div>\r
-<!-- Submit button -->\r
-<button id="data_load" type="button" class="btn btn-outline-primary btn-sm action-button">\r
-  <span class="glyphicon glyphicon-plus"></span> Add\r
-</button>\r
-\r
-<!-- Output message -->\r
-<div class="my-3">\r
-  <p id="output_message"></p>\r
-</div>
\ No newline at end of file