-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.R
More file actions
84 lines (56 loc) · 2.39 KB
/
preprocess.R
File metadata and controls
84 lines (56 loc) · 2.39 KB
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
## Preprocessing the post-con notes
#pool for database connections
library(pool)
#odbc for database connections
library(odbc)
#tidyverse for data manipulations
library(tidyverse)
#shinythemes for colors
library(shinythemes)
#lubridate to work with dates
library(lubridate)
#shinyjs() to use easy java script functions
library(shinyjs)
#DT for datatables
library(DT)
#reactable for reactable tables
library(reactable)
library(readxl)
library(DBI)
#Not in logical
`%!in%` <- Negate(`%in%`)
poolConn <- dbPool(odbc(), dsn = "mars14_datav2", uid = Sys.getenv("shiny_uid"), pwd = Sys.getenv("shiny_pwd"))
# Parse dates
notes <- read_excel("\\\\pwdoows\\oows\\Watershed Sciences\\GSI Monitoring\\07 Databases and Tracking Spreadsheets\\11 Post-Construction Status-Tracking Table\\Post-Con Status Table.xlsx", sheet = 4)
df <- str_split_fixed(notes$Notes, ": ", 2)
df <- as.data.frame(df)
df_db <- bind_cols(notes, df)
df_db <- df_db %>%
mutate(date = case_when (nchar(V1)<11 ~ mdy(V1)))
df_db <- df_db %>%
mutate(notes = ifelse(V2 == "", V1, V2))
df_db_final <- df_db %>%
select(system_id = `System ID`, status = `Post-Construction Status`, status_date = date, notes)
postcon_status_lookup <- df_db_final %>%
select(status) %>%
distinct()
postcon_status_lookup["postcon_status_lookup_uid"] <- 1:8
df_db_final <- df_db_final %>%
inner_join(postcon_status_lookup, by = "status") %>%
select(-status)
### Tables for DB
#df_db_final["postcon_status_uid"] <- 1:nrow(tbl_postcon_status)
tbl_postcon_status <- df_db_final %>%
arrange(desc(status_date)) %>%
group_by(system_id, postcon_status_lookup_uid) %>%
summarise(status_date = status_date[1], postcon_status_lookup_uid = max(postcon_status_lookup_uid))
tbl_postcon_status['postcon_status_uid'] <- 1:469
tbl_postcon_notes <- df_db_final %>%
select(system_id, note_date = status_date, notes, postcon_status_lookup_uid) %>%
inner_join(tbl_postcon_status, by = c("system_id","postcon_status_lookup_uid")) %>%
select(note_date, notes, postcon_status_uid)
tbl_postcon_status <- tbl_postcon_status %>%
replace_na(list(status_date = as.Date("2012-01-01")))
dbWriteTable(poolConn, SQL("fieldwork.tbl_postcon_status_lookup"), postcon_status_lookup, append = TRUE)
dbWriteTable(poolConn, SQL("fieldwork.tbl_postcon_status"), tbl_postcon_status, append = TRUE)
dbWriteTable(poolConn, SQL("fieldwork.tbl_postcon_notes"), tbl_postcon_notes, append = TRUE)