From 29e6ff84792529d1b1711207863734c401249581 Mon Sep 17 00:00:00 2001 From: Costa <47926492N@ICO.SCS.local> Date: Mon, 18 Oct 2021 15:25:26 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20comentarios=20informativos=20del?= =?UTF-8?q?=20script.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RellenarOVBD.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/RellenarOVBD.R b/RellenarOVBD.R index e01177c..3a31987 100644 --- a/RellenarOVBD.R +++ b/RellenarOVBD.R @@ -2,17 +2,21 @@ library(tidyverse) library(RODBC) library(openxlsx) +## Conexión a la base de datos dta<-odbcConnectAccess2007(access.file = "C:/Users/47926492N/OneDrive - IDIBELL - Institut d'Investigació Biomèdica de Bellvitge/RATG-PIULATS/OVARIO.accdb", pwd = .rs.askForPassword("Enter password:")) +## Importamos los NHC de las muestras nuevas nhc.test<-read.xlsx("queryOV.xlsx", sheet = "NHC") %>% pull(NHC) +## Mostramos si ya hay muestras del mismo paciente sqlQuery(dta, "SELECT O.NHC,S.* FROM SAMPLES S INNER JOIN OVID O ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test) %>% group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhc.test),all=T) +## Versión más extensa sqlQuery(dta, "SELECT O.NHC,S.* FROM SAMPLES S INNER JOIN OVID O @@ -30,6 +34,7 @@ rownames(upd.ovid)<-as.character(1:nrow(upd.ovid)) upd.ovid<-filter(upd.ovid, NHC %in% new.nhc) sqlSave(dta, upd.ovid, tablename="OVID", append = T) +## Generar código para las nuevas muestras samples<-sqlFetch(dta, "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 @@ -42,18 +47,21 @@ new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhc.test)), d samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples) samples.exp[is.na(samples.exp)]<-"" +## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos upd.clinics<-sqlFetch(dta, "CLINICS") ovid.new<-sqlFetch(dta, "OVID") %>% filter(NHC %in% nhc.test) upd.clinics<-merge(ovid.new,upd.clinics, all.x=T) 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("queryOV.xlsx") writeData(wb, "NHC", upd.ovid) writeData(wb,"samples",samples.exp) writeData(wb,"CLINICS",upd.clinics) saveWorkbook(wb,"queryOV.xlsx",overwrite = TRUE) +## Añadir código de muestra nueva a la base de datos nsamples<-sqlFetch(dta, "SAMPLES") %>% nrow upd.samples<-read.xlsx("QuerySamplesOV.xlsx") rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character