From d863315655e29dc57e18db0d1a70a5c429ffbabb Mon Sep 17 00:00:00 2001 From: Costa <47926492N@ICO.SCS.local> Date: Wed, 17 Nov 2021 15:20:44 +0100 Subject: [PATCH] =?UTF-8?q?He=20creado=20la=20funci=C3=B3n=20sqlMultiSampl?= =?UTF-8?q?es.=20Tambi=C3=A9n=20he=20adaptado=20la=20funci=C3=B3n=20sqlGen?= =?UTF-8?q?OVID=20para=20que=20sea=20usable=20para=20UM.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlFunctions.R | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/sqlFunctions.R b/sqlFunctions.R index 32aa5f6..a5ad506 100644 --- a/sqlFunctions.R +++ b/sqlFunctions.R @@ -47,23 +47,37 @@ sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F, dbtype){ } } -sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){ - ovid<-sqlFetch(conn,"OVID") +sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F, dbtype){ + if (dbtype == "OV"){ + db<-c("dbcode"="OVID") + } + if (dbtype == "UM"){ + db<-c("dbcode"="UMID") + } + + dbid<-sqlFetch(conn,db["dbcode"]) - new.nhc<-nhcs[!nhcs %in% ovid$NHC] - next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1 + new.nhc<-nhcs[!nhcs %in% dbid$NHC] + next.num<-gsub(db["dbcode"],"",dbid[,db["dbcode"]]) %>% 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)) + newtab<-data.frame("NHC"=new.nhc, "ID"=sprintf("%s%04d",db["dbcode"],next.num:last.num)) %>% rename(!!db["dbcode"]:="ID") + if(dbtype=="OV"){ + dbid<-rbind(dbid,newtab) + } + if(dbtype=="UM"){ + dbid<-merge(dbid, newtab, all=T) %>% select(Id,NHC,UMID) %>% arrange(Id) + dbid$Id<-rownames(dbid) + } + rownames(dbid)<-as.character(1:nrow(dbid)) + dbid<-filter(dbid, 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) + sqlSave(conn, dbid, tablename=db["dbcode"], append = T) print("La base ha sido actualizada.") } if (verb){ - return(upd.ovid) + return(dbid) } } @@ -149,3 +163,21 @@ sqlSincBD<-function(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clin print("Tabla CLINICS sincronizada.") } } + +sqlMultiSamples<-function(kbl=F, NHC=F){ + query<-sqlQuery(dta, "SELECT M.UMID,U.NHC,M.FECHA_RECEPCION,M.CODIGO,C.CODIGO,R.CODIGO + FROM ((MUESTRAS M + LEFT OUTER JOIN CNAG C ON M.CODIGO=C.CODIGO) + LEFT OUTER JOIN RNADNA R ON M.CODIGO=R.CODIGO) + LEFT OUTER JOIN UMID U ON U.UMID=M.UMID") %>% rename("CNAG"="CODIGO.1","RNADNA"="CODIGO.2") + + query<-query %>% mutate( + CNAG=case_when(!is.na(CNAG)~"X",TRUE~""), + RNADNA=case_when(!is.na(RNADNA)~"X",TRUE~"") + ) + if (kbl==T){ + query %>% kableExtra::kbl() %>% kableExtra::kable_styling(full_width = F, bootstrap_options = c("striped")) + }else{ + return(query) + } +}