Scripts relacionados con el acceso y análisis en bases de datos Access.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.6 KiB

  1. library(tidyverse)
  2. library(RODBC)
  3. library(openxlsx)
  4. dta<-odbcConnectAccess2007(access.file = "C:/Users/47926492N/OneDrive - IDIBELL - Institut d'Investigació Biomèdica de Bellvitge/RATG-PIULATS/OVARIO.accdb",
  5. pwd = .rs.askForPassword("Enter password:"))
  6. nhc.test<-read.xlsx("queryOV.xlsx", sheet = "NHC") %>% pull(NHC)
  7. sqlQuery(dta, "SELECT O.NHC,S.*
  8. FROM SAMPLES S
  9. INNER JOIN OVID O
  10. ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test) %>%
  11. group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhc.test),all=T)
  12. sqlQuery(dta, "SELECT O.NHC,S.*
  13. FROM SAMPLES S
  14. INNER JOIN OVID O
  15. ON O.OVID=S.OVID") %>% filter(NHC %in% nhc.test)
  16. ## Generar OVIDs para nuevos NHC
  17. ovid<-sqlFetch(dta,"OVID")
  18. new.nhc<-nhc.test[!nhc.test %in% ovid$NHC]
  19. next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1
  20. last.num<-next.num+(length(new.nhc)-1)
  21. upd.ovid<-rbind(ovid,data.frame("NHC"=new.nhc, "OVID"=sprintf("OVID%04d",next.num:last.num)))
  22. rownames(upd.ovid)<-as.character(1:nrow(upd.ovid))
  23. upd.ovid<-filter(upd.ovid, NHC %in% new.nhc)
  24. sqlSave(dta, upd.ovid, tablename="OVID", append = T)
  25. samples<-sqlFetch(dta, "SAMPLES")
  26. if(sum(grepl(paste0("OV",Sys.time() %>% format("%y")), samples$samples)) > 0){
  27. next.samp<-gsub(paste0("OV",Sys.time() %>% format("%y")),"", samples$samples) %>% as.numeric %>% max(na.rm=T)+1
  28. }else{
  29. next.samp<-1
  30. }
  31. last.samp<-next.samp+(length(nhc.test)-1)
  32. new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
  33. new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhc.test)), data.frame("NHC"=nhc.test, "samples"=new.samp))
  34. samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples)
  35. samples.exp[is.na(samples.exp)]<-""
  36. upd.clinics<-sqlFetch(dta, "CLINICS")
  37. ovid.new<-sqlFetch(dta, "OVID") %>% filter(NHC %in% nhc.test)
  38. upd.clinics<-merge(ovid.new,upd.clinics, all.x=T)
  39. upd.clinics$NHC<-as.character(upd.clinics$NHC)
  40. for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
  41. wb <- loadWorkbook("queryOV.xlsx")
  42. writeData(wb, "NHC", upd.ovid)
  43. writeData(wb,"samples",samples.exp)
  44. writeData(wb,"CLINICS",upd.clinics)
  45. saveWorkbook(wb,"queryOV.xlsx",overwrite = TRUE)
  46. nsamples<-sqlFetch(dta, "SAMPLES") %>% nrow
  47. upd.samples<-read.xlsx("QuerySamplesOV.xlsx")
  48. rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character
  49. sqlSave(dta, upd.samples, tablename="SAMPLES", append = T)