From d8c3a8943474cec23db4d01c8876915d7f4922ef Mon Sep 17 00:00:00 2001 From: marcelcosta Date: Fri, 29 Oct 2021 17:02:56 +0200 Subject: [PATCH] Add 'Docs/Docs.md' --- Docs/Docs.md | 372 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 372 insertions(+) create mode 100644 Docs/Docs.md diff --git a/Docs/Docs.md b/Docs/Docs.md new file mode 100644 index 0000000..b3d55b2 --- /dev/null +++ b/Docs/Docs.md @@ -0,0 +1,372 @@ +--- + +## sqlDropLast + +### Description +Removes from Database the last (or the amount specified) entry. + +### Usage +sqlDropLast(conn, tablename, droplast=1) + +### Arguments +Argument|Description +---|--- +conn|connection handle returned by odbcConnect. +tablename|character: a database table name accessible from the connected DSN. +droplast|the amount of lines to be removed from the table strating from tail. By default, it removes only 1 line. + +### Details +Removes from Database the last (or the amount specified) entry. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +dta<-odbcConnect("test") +sqlDropLast(dta, "TableTest") +``` + +### Function +```r +sqlDropLast<-function(conn, tablename, droplast=1){ + table<-sqlFetch(conn, tablename) + table<-table[1:(nrow(table)-droplast),] + sqlSave(conn, table, tablename = tablename, safer = F) +} +``` + +--- + +## sqlInitizalize + +### Description +Loads required libraries and gets the db location. + +### Usage +sqlInitialize() + +### Arguments +Argument|Description +---|--- + +### Details +Loads required libraries and gets the db location from "ruta_database.R" file. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +sqlInitialize() +``` + +### Function +```r +sqlInitialize<-function(){ + library(tidyverse) + library(RODBC) + library(openxlsx) + + ## Conexión a la base de datos + source("ruta_database.R", encoding = "UTF-8") +} +``` + +--- + +## sqlBackUp + +### Description +Creates a Back Up copy of the database. + +### Usage +sqlBackUp(dbfile=file,bu.dir="BU_OVARIO") + +### Arguments +Argument|Description +---|--- +dbfile| Database File location. +bu.dir| Directory under the DB file where the back up will be placed. + +### Details +Creates a Back Up copy of the database. It adds the date in front of the back up file. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +sqlInitialize() +sqlBackUp() +``` + +### Function +```r +sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){ + db=strsplit(dbfile, "/")[[1]]%>% tail(n=1) + bu_path<-gsub(db,bu.dir,dbfile) + if (!dir.exists(bu_path)){ + dir.create(bu_path) + print(paste0("Back Up directory ", bu_path, " created")) + } + cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db) + file.copy(dbfile, cp_bu) +} +``` + +--- + +## sqlShowSamples + +### Description +Shows if there are already samples from the specified NHCs. + +### Usage +sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F) + +### Arguments +Argument|Description +---|--- +conn|connection handle returned by odbcConnect. +nhcs|Character vector with the NHCs to test. +verb|Verbose: if TRUE, all the columns from "SAMPLES" table are printed. + +### Details +Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients. + +### Value +A data.frame with information about the patients. + +### Examples +```r +dta<-odbcConnect("test") +nhc.test<-c("XXXXXXXX","XXXXXXX") +sqlShowSamples() +``` + +### Function +```r +sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){ + if (isFALSE(verb)){ + sqlQuery(conn, "SELECT O.NHC,S.* + FROM SAMPLES S + INNER JOIN OVID O + ON O.OVID=S.OVID") %>% filter(NHC %in% nhcs) %>% + group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhcs),all=T) + }else{ + sqlQuery(conn, "SELECT O.NHC,S.* + FROM SAMPLES S + INNER JOIN OVID O + ON O.OVID=S.OVID") %>% filter(NHC %in% nhcs) + } +} +``` + +--- + +## sqlGenOVID + +### Description +Generates new consecutive OVID code for the patients that are not found in the DB. + +### Usage +sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F) + +### Arguments +Argument|Description +---|--- +conn|connection handle returned by odbcConnect. +nhcs|Character vector with the NHCs to test. +verb|Verbose: if TRUE (default), it prints the data.frame with the generated OVID codes. +sinc|If TRUE (default is FALSE for security), it adds the new entries to the "OVID" table in the DB. + +### Details +Generates new consecutive OVID code for the patients that are not found in the DB. + +### Value +If verb is TRUE, it returns a data.frame. + +### Examples +```r +dta<-odbcConnect("test") +nhc.test<-c("XXXXXXXX","XXXXXXX") +sqlGenOVID(sinc=T) +``` + +### Function +```r +sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){ + ovid<-sqlFetch(conn,"OVID") + + new.nhc<-nhcs[!nhcs %in% ovid$NHC] + next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1 + last.num<-next.num+(length(new.nhc)-1) + upd.ovid<-rbind(ovid,data.frame("NHC"=new.nhc, "OVID"=sprintf("OVID%04d",next.num:last.num))) + rownames(upd.ovid)<-as.character(1:nrow(upd.ovid)) + upd.ovid<-filter(upd.ovid, NHC %in% new.nhc) %>% mutate(NHC=as.character(NHC)) + + if (sinc){ + ### !! Atención, esto cambia la base de datos: + sqlSave(conn, upd.ovid, tablename="OVID", append = T) + print("La base ha sido actualizada.") + } + if (verb){ + return(upd.ovid) + } +} +``` + +--- + +## sqlWriteTemp + +### Description +Fills the Query Template file with the OVID and OV newly generated codes. + +### Usage +sqlWriteTemp(conn=dta, nhcs=nhc.test, file="queryOV.xlsx", samples.mod=T, clinics.mod=T) + +### Arguments +Argument|Description +---|--- +conn|connection handle returned by odbcConnect. +nhcs|Character vector with the NHCs to test. +file|Template file that will be used to interact with the DB. +samples.mod|If TRUE (default), it fills the "samples" template sheet. +clinics.mod|If TRUE (default), it fills the "CLINICS" template sheet. + +### Details +Fills the Query Template file with the OVID and OV newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of "CLINICS" table, if there were already an entry in the DB for that OVID code, the template file is filled with that information. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +dta<-odbcConnect("test") +nhc.test<-c("XXXXXXXX","XXXXXXX") +sqlGenOVID(sinc=T) +sqlWriteTemp() +``` + +### Function +```r +sqlWriteTemp<-function(conn=dta, nhcs=nhc.test, file="queryOV.xlsx", samples.mod=T, clinics.mod=T){ + upd.ovid<-sqlFetch(conn, "OVID") %>% filter(NHC %in% nhcs) + if (samples.mod){ + ## Generar código para las nuevas muestras + samples<-sqlFetch(conn, "SAMPLES") + if(sum(grepl(paste0("OV",Sys.time() %>% format("%y")), samples$samples)) > 0){ + next.samp<-gsub(paste0("OV",Sys.time() %>% format("%y")),"", samples$samples) %>% as.numeric %>% max(na.rm=T)+1 + }else{ + next.samp<-1 + } + last.samp<-next.samp+(length(nhcs)-1) + new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp) + new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhcs)), data.frame("NHC"=nhcs, "samples"=new.samp)) + samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples) + } + + if (clinics.mod){ + ## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos + upd.clinics<-sqlFetch(conn, "CLINICS") + ovid.new<-sqlFetch(conn, "OVID") %>% filter(NHC %in% nhcs) + upd.clinics<-merge(ovid.new,upd.clinics, all.x=T, by="OVID") + upd.clinics$NHC<-as.character(upd.clinics$NHC) + for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])} + } + + ## Exportar tablas a la plantilla de entrada para su rellenado + wb <- loadWorkbook(file) + writeData(wb, "NHC", upd.ovid) + if (samples.mod){writeData(wb,"samples",samples.exp)} + if (clinics.mod){writeData(wb,"CLINICS",upd.clinics)} + saveWorkbook(wb,file,overwrite = TRUE) +} +``` + +--- + +## sqlSincBD + +### Description +Updates the DB with the information filled in the template file. + +### Usage +sqlSincBD(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clinics=F) + +### Arguments +Argument|Description +---|--- +conn|connection handle returned by odbcConnect. +filetemp|Template file that will be used to interact with the DB. +sinc.samples|If TRUE (default is FALSE for security), it updates the SAMPLES table in the DB with the information in the "samples" template sheet. +clinics.mod|If TRUE (default is FALSE for security), it updates the CLINICS table in the DB with the information in the "CLINICS" template sheet. + +### Details +Updates the DB with the information filled in the template file. All the "samples" entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the "CLINICS" sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +dta<-odbcConnect("test") +nhc.test<-c("XXXXXXXX","XXXXXXX") +sqlGenOVID(sinc=T) +sqlWriteTemp() +sqlSincBD(sinc.samples=T, sinc.clinics=T) +``` + +### Function +```r +sqlSincBD<-function(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clinics=F){ + ## Añadir código de muestra nueva a la base de datos + nsamples<-sqlFetch(conn, "SAMPLES") %>% nrow + upd.samples<-read.xlsx(filetemp, sheet = "samples", detectDates = T) + if (nrow(upd.samples) > 0){rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character} + + if (sinc.samples & nrow(upd.samples) > 0){ + upd.samples$IQ_date<-as.Date(upd.samples$IQ_date) + ### !! Atención, esto cambia la base de datos: + sqlSave(conn, upd.samples, tablename="SAMPLES", append = T, varTypes = c("IQ_date"="date")) + print("Tabla SAMPLES sincronizada.") + } + + ## Añadir datos clínicos modificados a la base de datos + upd.clinics<-read.xlsx(filetemp, sheet = "CLINICS",detectDates = T) + ovid.mod<-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))] + rnames<-sqlFetch(conn, "CLINICS") %>% filter(OVID %in% ovid.mod) %>% rownames + clinics.mod<-upd.clinics %>% filter(OVID %in% ovid.mod) %>% select(-NHC) + rownames(clinics.mod)<-rnames + + ### !! Atención, esto cambia la base de datos: + if (sinc.clinics){ + fechas<-colnames(clinics.mod)[grepl("DO|date", colnames(clinics.mod))] + for (i in fechas){ + clinics.mod[,i]<-as.Date(clinics.mod[,i]) + } + sqlUpdate(conn, clinics.mod,"CLINICS") + print("Tabla CLINICS modificada.") + } + + ## Añadir datos clínicos nuevos a la base de datos + nsamples.clin<-sqlFetch(conn, "CLINICS") %>% nrow + ovid.new<-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(conn, "CLINICS") %>% pull(OVID))] + clinics.new<-upd.clinics %>% filter(OVID %in% ovid.new) %>% select(-NHC) + if (length(ovid.new) > 0){rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character} + + ### !! Atención, esto cambia la base de datos: + if (sinc.clinics){ + fechas<-colnames(clinics.new)[grepl("DO|date", colnames(clinics.new))] + varTypes<-rep("Date",length(fechas)) + names(varTypes)<-fechas + for (i in fechas){ + clinics.new[,i]<-as.Date(clinics.new[,i]) + } + sqlSave(conn, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes) + print("Tabla CLINICS sincronizada.") + } +} +``` \ No newline at end of file