From 4e4ba2d5ff6c0833b4dbd0f195c96dd00a98ae5b Mon Sep 17 00:00:00 2001 From: Adam Sadowski Date: Thu, 15 Feb 2024 09:30:21 -0500 Subject: [PATCH] Remove date column; working app --- .Rproj.user/19FD65EA/pcs/files-pane.pper | 2 +- .../19FD65EA/pcs/find-replace-in-files.pper | 4 +- .Rproj.user/19FD65EA/pcs/source-pane.pper | 2 +- .../19FD65EA/pcs/windowlayoutstate.pper | 12 +-- Dockerfile | 4 +- app/logic/constant.R | 27 +------ app/logic/hot_format.R | 10 ++- app/logic/import-join-structure.R | 49 ------------ app/logic/read_table.R | 16 ++++ app/main.R | 79 ++++++------------- app/view/table.R | 41 ---------- renv.lock | 2 +- 12 files changed, 63 insertions(+), 185 deletions(-) delete mode 100644 app/logic/import-join-structure.R create mode 100644 app/logic/read_table.R delete mode 100644 app/view/table.R diff --git a/.Rproj.user/19FD65EA/pcs/files-pane.pper b/.Rproj.user/19FD65EA/pcs/files-pane.pper index e2b88e0..e42679c 100644 --- a/.Rproj.user/19FD65EA/pcs/files-pane.pper +++ b/.Rproj.user/19FD65EA/pcs/files-pane.pper @@ -5,5 +5,5 @@ "ascending": true } ], - "path": "C:/Users/asadowsk/repo/police-maintenance/app/logic" + "path": "C:/Users/asadowsk/repo/police-maintenance/app/view" } \ No newline at end of file diff --git a/.Rproj.user/19FD65EA/pcs/find-replace-in-files.pper b/.Rproj.user/19FD65EA/pcs/find-replace-in-files.pper index 0797d09..0739190 100644 --- a/.Rproj.user/19FD65EA/pcs/find-replace-in-files.pper +++ b/.Rproj.user/19FD65EA/pcs/find-replace-in-files.pper @@ -1,6 +1,6 @@ { "dialog-state": { - "query": "poolClose", + "query": "clock", "path": "", "regex": false, "caseSensitive": false, @@ -9,7 +9,7 @@ "useGitGrep": true, "excludeGitIgnore": true, "excludeFilePatterns": [], - "resultsCount": 2, + "resultsCount": 0, "errorCount": 0, "replaceErrors": "", "projectRelative": true diff --git a/.Rproj.user/19FD65EA/pcs/source-pane.pper b/.Rproj.user/19FD65EA/pcs/source-pane.pper index bc7dc99..902cc6f 100644 --- a/.Rproj.user/19FD65EA/pcs/source-pane.pper +++ b/.Rproj.user/19FD65EA/pcs/source-pane.pper @@ -1,3 +1,3 @@ { - "activeTab": 4 + "activeTab": 0 } \ No newline at end of file diff --git a/.Rproj.user/19FD65EA/pcs/windowlayoutstate.pper b/.Rproj.user/19FD65EA/pcs/windowlayoutstate.pper index 2c61c7c..96a10aa 100644 --- a/.Rproj.user/19FD65EA/pcs/windowlayoutstate.pper +++ b/.Rproj.user/19FD65EA/pcs/windowlayoutstate.pper @@ -1,14 +1,14 @@ { "left": { - "splitterpos": 123, + "splitterpos": 122, "topwindowstate": "MAXIMIZE", - "panelheight": 704, - "windowheight": 742 + "panelheight": 703, + "windowheight": 741 }, "right": { - "splitterpos": 357, + "splitterpos": 502, "topwindowstate": "NORMAL", - "panelheight": 704, - "windowheight": 742 + "panelheight": 703, + "windowheight": 741 } } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4971025..d2257fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ ## Using older version of R due to rhandsontable date bug -## renv.lock was changed to reflect 4.1.3 -FROM openanalytics/r-ver:4.1.3 +## renv.lock was changed to reflect 4.2.2 +FROM openanalytics/r-ver:4.2.2 LABEL maintainer="Adam Sadowski " diff --git a/app/logic/constant.R b/app/logic/constant.R index edc2c85..3da4b5f 100644 --- a/app/logic/constant.R +++ b/app/logic/constant.R @@ -39,6 +39,7 @@ onStop(function() { employee <- dbReadTable(pool, "employee") |> mutate(name = glue("{given_names} {surname}")) |> as.data.frame() + #' @export trade <- dbReadTable(pool, "trade") |> mutate(trade = glue("{trade} - {trade_desc}")) |> @@ -51,8 +52,9 @@ building <- dbReadTable(pool, "workorder_building") |> trade_factor <- factor(NA_character_, levels = str_sort(trade$trade)) staff_factor <- factor(NA_character_, levels = str_sort(unique(employee$name))) +## Source of the initial database table #' @export -DFF <- tibble( +DF <- tibble( hour = NA_integer_, minute = NA_integer_, am_pm = factor(NA_character_, levels = c("am", "pm")), @@ -78,26 +80,3 @@ DFF <- tibble( rename_with(\(x) x |> str_replace_all("_", " ") |> str_to_title()) |> rename("am/pm" = "Am Pm") |> add_rows(2) - -#' @export -DF <- dbReadTable(pool, "police_maintenance") |> - rename_with( - \(x) str_replace_all(x, "\\.", " ") |> str_trim(), - everything() - ) |> - rename("am/pm" = "am pm", "Work Order #" = "Work Order") - - -DF <- dbReadTable(conn, "police_maintenance") |> - rename_with( - \(x) str_replace_all(x, "\\.", " ") |> str_trim(), - everything() - ) |> - rename("am/pm" = "am pm", "Work Order #" = "Work Order") - -#' @export -current <- function() { - dm <- dm_from_con(conn, "police_maintenance") - # dbDisconnect(conn) - return(dm) -} diff --git a/app/logic/hot_format.R b/app/logic/hot_format.R index 8a39f97..49fe243 100644 --- a/app/logic/hot_format.R +++ b/app/logic/hot_format.R @@ -13,7 +13,7 @@ hot_format <- function(.data) { allowInvalid = TRUE, copyable = TRUE, strict = FALSE, - colWidths = c(50, 70, 75, 100, 75, 250, 70, 200, 100, + colWidths = c(50, 70, 75, 100, 250, 70, 200, 100, rep(200, 7), 130, 130), renderer = " function (instance, td, row, col, prop, value, cellProperties) { @@ -25,11 +25,14 @@ hot_format <- function(.data) { halign = "htCenter" ) |> hot_col("Issue", halign = "htLeft") |> + # hot_col(col = "am/pm", type = "dropdown", source = c("am", "pm")) |> hot_col( col = c( "Trade Responsible", "Trade Attended 1", "Trade Attended 2", - "Staff Attended 1", "Staff Attended 2"), + "Staff Attended 1", "Staff Attended 2", + "Staff Called 1", "Staff Called 2" + ), type = "dropdown", source = str_sort(trade$trade) ) |> # , @@ -38,5 +41,6 @@ hot_format <- function(.data) { hot_table(highlightCol = TRUE, highlightRow = TRUE) |> hot_validate_numeric(cols = "Work Order #", min = 100000, max = 999999) |> hot_validate_numeric(cols = "Hour", min = 0, max = 12) |> - hot_validate_numeric(cols = "Minute", min = 0, max = 60) + hot_validate_numeric(cols = "Minute", min = 0, max = 60) |> + hot_validate_character(col = "am/pm", choices = c("am", "pm")) } diff --git a/app/logic/import-join-structure.R b/app/logic/import-join-structure.R deleted file mode 100644 index d29f28d..0000000 --- a/app/logic/import-join-structure.R +++ /dev/null @@ -1,49 +0,0 @@ -box::use( - DBI[dbReadTable], - tidyverse[...] -) - -library(tidyverse) -library(tibble) -library(rhandsontable) -library(stringr) -library(glue) - -employee <- dbReadTable(conn, "employee") |> - mutate(name = glue("{given_names} {surname}")) |> - as.data.frame() - -trade <- dbReadTable(conn, "trade") |> - mutate(trade = glue("{trade_code} - {trade_description}")) |> - distinct(trade, .keep_all = T) - -building <- dbReadTable(conn, "workorder_building") |> - mutate(building = glue("{building_number} - {building_name}")) - -trade_factor <- factor(NA_character_, levels = str_sort(trade$trade)) -staff_factor <- factor(NA_character_, levels = str_sort(unique(employee$name))) - -DF <- tibble( - hour = NA_integer_, - minute = NA_integer_, - am_pm = factor(NA_character_, levels = c("am", "pm")), - date = NA_Date_, - "Work Order #" = NA_integer_, - building = factor(NA_character_, levels = str_sort(building$building)), - room = NA_integer_, - issue = NA_character_, - reporter = NA_character_, - trade_responsible = trade_factor, - trade_attended_1 = trade_factor, - trade_attended_2 = trade_factor, - staff_called_1 = staff_factor, - staff_attended_1 = staff_factor, - staff_called_2 = staff_factor, - staff_attended_2 = staff_factor, - contractor_1 = NA_character_, - contractor_2 = NA_character_ - ) |> - relocate(trade_attended_2, .after = staff_attended_1) |> - rename_with(\(x) x |> str_replace_all("_", " ") |> str_to_title()) |> - rename("am/pm" = "Am Pm") |> - add_rows(3) diff --git a/app/logic/read_table.R b/app/logic/read_table.R new file mode 100644 index 0000000..bd1c5ae --- /dev/null +++ b/app/logic/read_table.R @@ -0,0 +1,16 @@ +box::use( + DBI[dbConnect, dbReadTable, dbDisconnect], + app/logic/constant[pool, conn], + dplyr[...], + lubridate[NA_Date_], + stringr[...] +) + +read_table <- function() { + dbReadTable(conn, "police_maintenance") |> + rename_with( + \(x) str_replace_all(x, "\\.", " ") |> str_trim(), + everything() + ) |> + rename("am/pm" = "am pm", "Work Order #" = "Work Order") +} diff --git a/app/main.R b/app/main.R index 237f977..afd3937 100644 --- a/app/main.R +++ b/app/main.R @@ -1,16 +1,15 @@ box::use( shiny[...], - app/view/table, rhandsontable[...], DBI[...], dm[...], dplyr[...], lubridate[...], - stringr[...], + tibble[column_to_rownames, rownames_to_column], app/logic/add_rows[add_rows], - app/logic/constant[pool, conn, current], - # app/logic/constant[DF], + app/logic/constant[pool, conn], app/logic/hot_format[hot_format], + app/logic/read_table[read_table], ) #' @export @@ -19,76 +18,46 @@ ui <- function(id) { fluidPage( titlePanel("Grey columns are optional."), helpText("Right click to undo. Grey columns are optional. Changes to the table will be automatically saved to the source file."), - ## Uncomment line below to use action button to commit changes - actionButton(ns("saveBtn"), "Save"), - dateInput(ns("date"), "Date"), - numericInput(ns("minute"), "Minute", value = 2), + dateInput(ns("date"), "Date", value = Sys.Date()), + # numericInput(ns("minute"), "Minute", value = 2), rHandsontableOutput(ns("hot")), ) } -DF_read <- function() { - dbReadTable(conn, "police_maintenance") |> - rename_with( - \(x) str_replace_all(x, "\\.", " ") |> str_trim(), - everything() - ) |> - rename("am/pm" = "am pm", "Work Order #" = "Work Order") - - # dbDisconnect(conn) -} - #' @export server <- function(id) { moduleServer(id, function(input, output, session) { + observe({ - ## Remove button and isolate to update file automatically - ## after each table change - # input$saveBtn - # hot = isolate(input$hot) if (!is.null(input$hot)) { - # if (!is.null(hot)) { - - updated <- dm(police_maintenance = hot_to_r(input$hot)) - updated <- copy_dm_to(conn, updated, temporary = TRUE) - final <- dm_rows_upsert(current(), updated, in_place = TRUE) + x <- input$hot |> + hot_to_r() + x <- x |> + mutate( + Date = input$date, + Id = row_number() + as.numeric(Date) * nrow(!!x) + ) + dm <- dm(police_maintenance = x) + dm <- copy_dm_to(conn, dm, temporary = TRUE) + dm_db <- dm_from_con(conn, "police_maintenance") + final <- dm_rows_upsert(dm_db, dm, in_place = TRUE) print("saved") - # dbDisconnect(conn) - } }) - hott <- reactive({ - # if (!is.null(input$hot)) { - # x <- hot_to_r(input$hot) |> - # filter(Minute == input$minute) - # if (nrow(x) == 0) { - # x <- x |> - # add_rows(3) |> - # mutate(Minute = input$minute) - # } - # return(x |> mutate(Id = today() |> as.integer() + row_number()+ Minute)) - # - # } else { - ## INITIATLIZE - x <- DF_read() |> - filter(Minute == input$minute) + df_init <- reactive({ + x <- read_table() |> + filter(Date == input$date |> as.character()) |> + select(- c(Date, Id)) if (nrow(x) == 0) { - x <- x |> - add_rows(3) |> - mutate(Minute = input$minute, - Id = today() |> as.integer() + row_number()+ Minute) + x <- x |> add_rows(3) } return(x) - # } - # return(x) }) |> - bindEvent(input$minute) + bindEvent(input$date) output$hot = renderRHandsontable({ - # if (!is.null(hott())) { - hott() |> hot_format() - # } + df_init() |> hot_format() }) }) } diff --git a/app/view/table.R b/app/view/table.R deleted file mode 100644 index 90db168..0000000 --- a/app/view/table.R +++ /dev/null @@ -1,41 +0,0 @@ -#' # app/view/table.R -#' -#' box::use( -#' rhandsontable[...], -#' DBI[...], -#' shiny[h3, moduleServer, NS, tagList], -#' ) -#' -#' con <- DBI::dbConnect( -#' RPostgres::Postgres(), -#' dbname = "postgres", -#' user = Sys.getenv("POSTGRES_USER"), -#' host = "data.pr.uoguelph.ca", -#' port = "5432", -#' password = Sys.getenv("POSTGRES_PASS"), -#' ) -#' -#' #' @export -#' ui <- function(id) { -#' ns <- NS(id) -#' tagList( -#' h3("Grey columns are optional. Work Order # must be 6 numbers long."), -#' rHandsontableOutput(ns("hot")) -#' ) -#' } -#' -#' #' @export -#' server <- function(id) { -#' moduleServer(id, function(input, output, session) { -#' output$hot = renderRHandsontable({ -#' if (!is.null(input$hot)) { -#' DF = hot_to_r(input$hot) -#' } else { -#' DF = DBI$dbReadTable(conn, "mtcars") -#' } -#' -#' rhandsontable(DF) |> -#' hot_table(highlightCol = TRUE, highlightRow = TRUE) -#' }) -#' }) -#' } diff --git a/renv.lock b/renv.lock index 208548a..d29cf8b 100644 --- a/renv.lock +++ b/renv.lock @@ -1,6 +1,6 @@ { "R": { - "Version": "4.3.2", + "Version": "4.2.2", "Repositories": [ { "Name": "CRAN",