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.

133 lines
5.3 KiB

  1. require(RODBC)
  2. sqlDropLast<-function(conn, tablename, droplast=1){
  3. table<-sqlFetch(conn, tablename)
  4. table<-table[1:(nrow(table)-droplast),]
  5. sqlSave(conn, table, tablename = tablename, safer = F)
  6. }
  7. sqlInitialize<-function(){
  8. library(tidyverse)
  9. library(RODBC)
  10. library(openxlsx)
  11. ## Conexión a la base de datos
  12. source("ruta_database.R", encoding = "UTF-8")
  13. }
  14. sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){
  15. if (isFALSE(verb)){
  16. sqlQuery(conn, "SELECT O.NHC,S.*
  17. FROM SAMPLES S
  18. INNER JOIN OVID O
  19. ON O.OVID=S.OVID") %>% filter(NHC %in% nhcs) %>%
  20. group_by(NHC,OVID) %>% summarise(Samples=length(samples), Names=paste0(samples, collapse = ";")) %>% merge(data.frame(NHC=nhcs),all=T)
  21. }else{
  22. sqlQuery(conn, "SELECT O.NHC,S.*
  23. FROM SAMPLES S
  24. INNER JOIN OVID O
  25. ON O.OVID=S.OVID") %>% filter(NHC %in% nhcs)
  26. }
  27. }
  28. sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
  29. ovid<-sqlFetch(conn,"OVID")
  30. new.nhc<-nhcs[!nhcs %in% ovid$NHC]
  31. next.num<-gsub("OVID","",ovid$OVID) %>% as.numeric %>% max(na.rm=T)+1
  32. last.num<-next.num+(length(new.nhc)-1)
  33. upd.ovid<-rbind(ovid,data.frame("NHC"=new.nhc, "OVID"=sprintf("OVID%04d",next.num:last.num)))
  34. rownames(upd.ovid)<-as.character(1:nrow(upd.ovid))
  35. upd.ovid<-filter(upd.ovid, NHC %in% new.nhc) %>% mutate(NHC=as.character(NHC))
  36. if (sinc){
  37. ### !! Atención, esto cambia la base de datos:
  38. sqlSave(conn, upd.ovid, tablename="OVID", append = T)
  39. print("La base ha sido actualizada.")
  40. }
  41. if (verb){
  42. return(upd.ovid)
  43. }
  44. }
  45. sqlWriteTemp<-function(conn=dta, nhcs=nhc.test, file="queryOV.xlsx", samples.mod=T, clinics.mod=T){
  46. upd.ovid<-sqlFetch(conn, "OVID") %>% filter(NHC %in% nhcs)
  47. if (samples.mod){
  48. ## Generar código para las nuevas muestras
  49. samples<-sqlFetch(conn, "SAMPLES")
  50. if(sum(grepl(paste0("OV",Sys.time() %>% format("%y")), samples$samples)) > 0){
  51. next.samp<-gsub(paste0("OV",Sys.time() %>% format("%y")),"", samples$samples) %>% as.numeric %>% max(na.rm=T)+1
  52. }else{
  53. next.samp<-1
  54. }
  55. last.samp<-next.samp+(length(nhcs)-1)
  56. new.samp<-sprintf("OV%s%02d",Sys.time() %>% format("%y"),next.samp:last.samp)
  57. new.samp.df<-merge(sqlFetch(dta,"OVID") %>% merge(data.frame("NHC"=nhcs)), data.frame("NHC"=nhcs, "samples"=new.samp))
  58. samples.exp<-merge(samples %>% slice(0), new.samp.df %>% select(-NHC), all=T) %>% select(colnames(samples)) %>% arrange(samples)
  59. }
  60. if (clinics.mod){
  61. ## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
  62. upd.clinics<-sqlFetch(conn, "CLINICS")
  63. ovid.new<-sqlFetch(conn, "OVID") %>% filter(NHC %in% nhcs)
  64. upd.clinics<-merge(ovid.new,upd.clinics, all.x=T, by="OVID")
  65. upd.clinics$NHC<-as.character(upd.clinics$NHC)
  66. for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]<-as.Date(upd.clinics[,i])}
  67. }
  68. ## Exportar tablas a la plantilla de entrada para su rellenado
  69. wb <- loadWorkbook(file)
  70. writeData(wb, "NHC", upd.ovid)
  71. if (samples.mod){writeData(wb,"samples",samples.exp)}
  72. if (clinics.mod){writeData(wb,"CLINICS",upd.clinics)}
  73. saveWorkbook(wb,file,overwrite = TRUE)
  74. }
  75. sqlSincBD<-function(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clinics=F){
  76. ## Añadir código de muestra nueva a la base de datos
  77. nsamples<-sqlFetch(conn, "SAMPLES") %>% nrow
  78. upd.samples<-read.xlsx(filetemp, sheet = "samples", detectDates = T)
  79. if (nrow(upd.samples) > 0){rownames(upd.samples)<-(nsamples+1):(nsamples+nrow(upd.samples)) %>% as.character}
  80. if (sinc.samples & nrow(upd.samples) > 0){
  81. upd.samples$IQ_date<-as.Date(upd.samples$IQ_date)
  82. ### !! Atención, esto cambia la base de datos:
  83. sqlSave(conn, upd.samples, tablename="SAMPLES", append = T, varTypes = c("IQ_date"="date"))
  84. print("Tabla SAMPLES sincronizada.")
  85. }
  86. ## Añadir datos clínicos modificados a la base de datos
  87. upd.clinics<-read.xlsx(filetemp, sheet = "CLINICS",detectDates = T)
  88. ovid.mod<-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, "CLINICS") %>% pull(OVID))]
  89. rnames<-sqlFetch(conn, "CLINICS") %>% filter(OVID %in% ovid.mod) %>% rownames
  90. clinics.mod<-upd.clinics %>% filter(OVID %in% ovid.mod) %>% select(-NHC)
  91. rownames(clinics.mod)<-rnames
  92. ### !! Atención, esto cambia la base de datos:
  93. if (sinc.clinics){
  94. fechas<-colnames(clinics.mod)[grepl("DO|date", colnames(clinics.mod))]
  95. for (i in fechas){
  96. clinics.mod[,i]<-as.Date(clinics.mod[,i])
  97. }
  98. sqlUpdate(conn, clinics.mod,"CLINICS")
  99. print("Tabla CLINICS modificada.")
  100. }
  101. ## Añadir datos clínicos nuevos a la base de datos
  102. nsamples.clin<-sqlFetch(conn, "CLINICS") %>% nrow
  103. ovid.new<-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(conn, "CLINICS") %>% pull(OVID))]
  104. clinics.new<-upd.clinics %>% filter(OVID %in% ovid.new) %>% select(-NHC)
  105. if (length(ovid.new) > 0){rownames(clinics.new)<-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %>% as.character}
  106. ### !! Atención, esto cambia la base de datos:
  107. if (sinc.clinics){
  108. fechas<-colnames(clinics.new)[grepl("DO|date", colnames(clinics.new))]
  109. varTypes<-rep("Date",length(fechas))
  110. names(varTypes)<-fechas
  111. for (i in fechas){
  112. clinics.new[,i]<-as.Date(clinics.new[,i])
  113. }
  114. sqlSave(conn, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes)
  115. print("Tabla CLINICS sincronizada.")
  116. }
  117. }