DHSC-Capstone/ML/0-greeter-dbcreation.R

72 lines
1.5 KiB
R
Raw Permalink Normal View History

2023-01-04 08:17:55 -05:00
# The follow script rebuilds a local copy of the MIMIC-IV database
# for the purpose of this project only the patients, labitems, and labevents
# tables are included, and the csv's need to be in the data-unshared folder
2023-01-04 08:27:49 -05:00
# this script only needs to be run once on any computer performing analysis
rm(list = ls(all.names = TRUE)) # Clear the memory of variables from previous run.
cat("\014") # Clear the console
# load packages -----------------------------------------------------------
box::use(
magrittr[`%>%`]
2023-01-04 10:09:07 -05:00
,RSQLite
2023-01-04 11:15:36 -05:00
,DBI[dbConnect,dbDisconnect,dbWriteTable]
2023-01-04 10:09:07 -05:00
,here[here]
2023-01-04 11:15:36 -05:00
,readr
2023-01-04 08:27:49 -05:00
)
2023-01-04 10:09:07 -05:00
# globals -----------------------------------------------------------------
# create database ---------------------------------------------------------
2023-01-04 11:15:36 -05:00
mimicDB <- dbConnect(
2023-01-04 10:09:07 -05:00
RSQLite$SQLite()
,here("ML","data-unshared","mimicDB.sqlite")
)
2023-01-04 11:15:36 -05:00
# create labevents table
readr$read_csv_chunked(
here("ML","data-unshared", "labevents.csv")
,callback = function(chunk,dummy){
dbWriteTable(mimicDB, "labevents", chunk, append = TRUE)
}
,chunk_size = 10000
2023-01-23 07:22:03 -05:00
,col_types = "_dddd_TT_d______"
2023-01-04 11:15:36 -05:00
)
# create patient table
readr$read_csv(
here("ML","data-unshared","patients.csv")
) %>%
dbWriteTable(mimicDB,"patients", .)
2023-01-04 10:09:07 -05:00
2023-01-05 09:50:36 -05:00
# create labitems table
2023-01-04 10:09:07 -05:00
2023-01-05 09:50:36 -05:00
readr$read_csv(
here("ML","data-unshared","d_labitems.csv")
) %>%
dbWriteTable(mimicDB,"labitems", .)
2023-01-04 10:09:07 -05:00
2023-01-23 07:22:03 -05:00
readr$read_csv(
here("ML","data-unshared","admissions.csv")
) %>%
dbWriteTable(mimicDB,"admissions", .)
2023-01-04 10:09:07 -05:00
# close database ----------------------------------------------------------
2023-01-04 11:15:36 -05:00
dbDisconnect(mimicDB)
2023-01-04 10:09:07 -05:00