From 8c8065fe9d615ee6496bac19c515b57939a59e69 Mon Sep 17 00:00:00 2001 From: Costa <47926492N@ICO.SCS.local> Date: Mon, 20 Dec 2021 16:17:37 +0100 Subject: [PATCH] =?UTF-8?q?Corregido=20el=20error=20en=20el=20que=20no=20h?= =?UTF-8?q?ab=C3=ADa=20pacientes=20nuevos=20en=20la=20funci=C3=B3n=20sqlGe?= =?UTF-8?q?nOVID.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlFunctions.R | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/sqlFunctions.R b/sqlFunctions.R index a0b91fb..c13e39e 100644 --- a/sqlFunctions.R +++ b/sqlFunctions.R @@ -74,27 +74,31 @@ sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F, dbtype=NULL){ dbid<-sqlFetch(conn,db["dbcode"]) 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) - 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<-as.numeric(rownames(dbid)) - dbid$NHC<-as.numeric(dbid$NHC) - } - 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, dbid, tablename=db["dbcode"], append = T) - print("La base ha sido actualizada.") - } - if (verb){ - return(dbid) + if(length(new.nhc) > 0){ + next.num<-gsub(db["dbcode"],"",dbid[,db["dbcode"]]) %>% as.numeric %>% max(na.rm=T)+1 + last.num<-next.num+(length(new.nhc)-1) + 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<-as.numeric(rownames(dbid)) + dbid$NHC<-as.numeric(dbid$NHC) + } + 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, dbid, tablename=db["dbcode"], append = T) + print("La base ha sido actualizada.") + } + if (verb){ + return(dbid) + } + }else{ + print("No hay pacientes nuevos.") } }