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.") } }