@ -176,6 +176,7 @@ pre code {
< li > < a href = "#sqlgenovid" > sqlGenOVID< / a > < / li >
< li > < a href = "#sqlwritetemp" > sqlWriteTemp< / a > < / li >
< li > < a href = "#sqlsincbd" > sqlSincBD< / a > < / li >
< li > < a href = "#sqlmultisamples" > sqlMultiSamples< / a > < / li >
< / ul >
< / div >
@ -188,7 +189,7 @@ pre code {
< / div >
< div id = "usage" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlDropLast(conn, tablename, droplast=1)< / p >
< p > sqlDropLast(conn, tablename, droplast=1, dbtype=NULL )< / p >
< / div >
< div id = "arguments" class = "section level3" >
< h3 > Arguments< / h3 >
@ -216,6 +217,10 @@ pre code {
< td > droplast< / td >
< td > the amount of lines to be removed from the table strating from tail. By default, it removes only 1 line.< / td >
< / tr >
< tr class = "even" >
< td > dbtype< / td >
< td > used to manually specify the database type. It defaults to NULL and the type is deduced.< / td >
< / tr >
< / tbody >
< / table >
< / div >
@ -234,10 +239,17 @@ sqlDropLast(dta, "TableTest")
< / div >
< div id = "function" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlDropLast< -function(conn, tablename, droplast=1){
< pre class = "r" > < code > sqlDropLast< -function(conn, tablename, droplast=1,dbtype=NULL){
if(sqlTables(conn) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){dbtype< -" UM" }
if(sqlTables(conn) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" OV" }
table< -sqlFetch(conn, tablename)
table< -table[1:(nrow(table)-droplast),]
sqlSave(conn, table, tablename = tablename, safer = F)
if (dbtype == " OV" ){sqlSave(conn, table, tablename = tablename, safer = F)}
if (dbtype == " UM" ){
sqlDrop(conn, tablename)
sqlSave(conn, table, tablename = tablename, safer = F)}
}< / code > < / pre >
< hr / >
< / div >
@ -298,11 +310,15 @@ sqlDropLast(dta, "TableTest")
< / div >
< div id = "usage-2" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlBackUp(dbfile=file,bu.dir=“BU_OVARIO” )< / p >
< p > sqlBackUp(dbfile=file,conn=dta,bu.dir=NULL )< / p >
< / div >
< div id = "arguments-2" class = "section level3" >
< h3 > Arguments< / h3 >
< table >
< colgroup >
< col width = "50%" / >
< col width = "50%" / >
< / colgroup >
< thead >
< tr class = "header" >
< th > Argument< / th >
@ -315,8 +331,12 @@ sqlDropLast(dta, "TableTest")
< td > Database File location.< / td >
< / tr >
< tr class = "even" >
< td > conn< / td >
< td > connection handle returned by odbcConnect.< / td >
< / tr >
< tr class = "odd" >
< td > bu.dir< / td >
< td > Directory under the DB file where the back up will be placed.< / td >
< td > Directory under the DB file where the back up will be placed. It defaults to NULL and is deduced from conn database. < / td >
< / tr >
< / tbody >
< / table >
@ -336,7 +356,10 @@ sqlBackUp()
< / div >
< div id = "function-2" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlBackUp< -function(dbfile=file,bu.dir=" BU_OVARIO" ){
< pre class = "r" > < code > sqlBackUp< -function(dbfile=file,conn=dta,bu.dir=NULL){
if(sqlTables(conn) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){bu.dir< -" BU_UM" }
if(sqlTables(conn) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" BU_OVARIO" }
db=strsplit(dbfile, " /" )[[1]]%> % tail(n=1)
bu_path< -gsub(db,bu.dir,dbfile)
if (!dir.exists(bu_path)){
@ -357,11 +380,15 @@ sqlBackUp()
< / div >
< div id = "usage-3" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)< / p >
< p > sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F, dbtype=NULL )< / p >
< / div >
< div id = "arguments-3" class = "section level3" >
< h3 > Arguments< / h3 >
< table >
< colgroup >
< col width = "50%" / >
< col width = "50%" / >
< / colgroup >
< thead >
< tr class = "header" >
< th > Argument< / th >
@ -381,6 +408,10 @@ sqlBackUp()
< td > verb< / td >
< td > Verbose: if TRUE, all the columns from “SAMPLES” table are printed.< / td >
< / tr >
< tr class = "even" >
< td > dbtype< / td >
< td > used to manually specify the database type. It defaults to NULL and the type is deduced.< / td >
< / tr >
< / tbody >
< / table >
< / div >
@ -400,18 +431,27 @@ sqlShowSamples()
< / div >
< div id = "function-3" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlShowSamples< -function(conn=dta, nhcs=nhc.test, verb=F){
< pre class = "r" > < code > sqlShowSamples< -function(conn=dta, nhcs=nhc.test, verb=F, dbtype=NULL){
if(sqlTables(dta) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){dbtype< -" UM" }
if(sqlTables(dta) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" OV" }
if (dbtype == " OV" ){
db< -c(" dbtables" =" SAMPLES" , " dbcode" =" OVID" , " dbsamples" =" samples" )
query< -paste0(" SELECT O.NHC,S.* FROM " ,db[" dbtables" ]," S INNER JOIN " ,db[" dbcode" ]," O ON O." ,db[" dbcode" ]," =S." ,db[" dbcode" ])
}
if (dbtype == " UM" ){
db< -c(" dbtables" =" MUESTRAS" , " dbcode" =" UMID" , " dbsamples" =" CODIGO" )
query< -paste0(" SELECT O.NHC,S.* FROM " ,db[" dbtables" ]," S INNER JOIN " ,db[" dbcode" ]," O ON O." ,db[" dbcode" ]," =S." ,db[" dbcode" ])
}
if (nrow(sqlQuery(conn, query) %> % filter(NHC %in% nhcs)) == 0){
return(" No hay muestras de ningún paciente." )
}
if (isFALSE(verb)){
sqlQuery(conn, " SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID" ) %> % filter(NHC %in% nhcs) %> %
group_by(NHC,OVID) %> % summarise(Samples=length(samples), Names=paste0(samples, collapse = " ;" )) %> % merge(data.frame(NHC=nhcs),all=T)
sqlQuery(conn, query) %> % filter(NHC %in% nhcs) %> %
group_by(NHC,UQ(rlang::sym(db[" dbcode" ]))) %> % summarise(Samples=length(UQ(rlang::sym(db[" dbsamples" ]))), Names=paste0(UQ(rlang::sym(db[" dbsamples" ])), collapse = " ;" )) %> %
merge(data.frame(NHC=nhcs),all=T) %> % mutate(NHC=factor(NHC,levels = nhcs)) %> % arrange(NHC)
}else{
sqlQuery(conn, " SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID" ) %> % filter(NHC %in% nhcs)
sqlQuery(conn, query) %> % filter(NHC %in% nhcs)
}
}< / code > < / pre >
< hr / >
@ -421,11 +461,11 @@ sqlShowSamples()
< h2 > sqlGenOVID< / h2 >
< div id = "description-4" class = "section level3" >
< h3 > Description< / h3 >
< p > Generates new consecutive OVID code for the patients that are not found in the DB.< / p >
< p > Generates new consecutive OVID or UMID code for the patients that are not found in the DB.< / p >
< / div >
< div id = "usage-4" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)< / p >
< p > sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F, dbtype=NULL )< / p >
< / div >
< div id = "arguments-4" class = "section level3" >
< h3 > Arguments< / h3 >
@ -457,12 +497,16 @@ sqlShowSamples()
< td > sinc< / td >
< td > If TRUE (default is FALSE for security), it adds the new entries to the “OVID” table in the DB.< / td >
< / tr >
< tr class = "odd" >
< td > dbtype< / td >
< td > used to manually specify the database type. It defaults to NULL and the type is deduced.< / td >
< / tr >
< / tbody >
< / table >
< / div >
< div id = "details-4" class = "section level3" >
< h3 > Details< / h3 >
< p > Generates new consecutive OVID code for the patients that are not found in the DB.< / p >
< p > Generates new consecutive OVID or UMID code for the patients that are not found in the DB.< / p >
< / div >
< div id = "value-4" class = "section level3" >
< h3 > Value< / h3 >
@ -476,23 +520,41 @@ sqlGenOVID(sinc=T)
< / div >
< div id = "function-4" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlGenOVID< -function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
ovid< -sqlFetch(conn," OVID" )
< pre class = "r" > < code > sqlGenOVID< -function(conn=dta, nhcs=nhc.test, verb=T, sinc=F, dbtype=NULL){
if(sqlTables(dta) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){dbtype< -" UM" }
if(sqlTables(dta) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" OV" }
if (dbtype == " OV" ){
db< -c(" dbcode" =" OVID" )
}
if (dbtype == " UM" ){
db< -c(" dbcode" =" UMID" )
}
new.nhc< -nhcs[!nhcs %in% ovid$NHC]
next.num< -gsub(" OVID" ," " ,ovid$OVID) %> % as.numeric %> % max(na.rm=T)+1
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)
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< -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, 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(db id)
}
}< / code > < / pre >
< hr / >
@ -502,15 +564,19 @@ sqlGenOVID(sinc=T)
< h2 > sqlWriteTemp< / h2 >
< div id = "description-5" class = "section level3" >
< h3 > Description< / h3 >
< p > Fills the Query Template file with the OVID and OV newly generated codes.< / p >
< p > Fills the Query Template file with the OVID or UMID and OV or UM newly generated codes.< / p >
< / div >
< div id = "usage-5" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T)< / p >
< p > sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T, dbtype=NULL )< / p >
< / div >
< div id = "arguments-5" class = "section level3" >
< h3 > Arguments< / h3 >
< table >
< colgroup >
< col width = "50%" / >
< col width = "50%" / >
< / colgroup >
< thead >
< tr class = "header" >
< th > Argument< / th >
@ -538,12 +604,16 @@ sqlGenOVID(sinc=T)
< td > clinics.mod< / td >
< td > If TRUE (default), it fills the “CLINICS” template sheet.< / td >
< / tr >
< tr class = "even" >
< td > dbtype< / td >
< td > used to manually specify the database type. It defaults to NULL and the type is deduced.< / td >
< / tr >
< / tbody >
< / table >
< / div >
< div id = "details-5" class = "section level3" >
< h3 > Details< / h3 >
< p > Fills the Query Template file with the OVID and OV newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of “CLINICS” table, if there were already an entry in the DB for that OVID code, the template file is filled with that information.< / p >
< p > Fills the Query Template file with the OVID and OV or UMID and UM newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of “CLINICS” table, if there were already an entry in the DB for that OVID/UM ID code, the template file is filled with that information.< / p >
< / div >
< div id = "value-5" class = "section level3" >
< h3 > Value< / h3 >
@ -558,37 +628,74 @@ sqlWriteTemp()
< / div >
< div id = "function-5" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlWriteTemp< -function(conn=dta, nhcs=nhc.test, file=" queryOV.xlsx" , samples.mod=T, clinics.mod=T){
upd.ovid< -sqlFetch(conn, " OVID" ) %> % filter(NHC %in% nhcs)
if (samples.mod){
## Generar código para las nuevas muestras
samples< -sqlFetch(conn, " SAMPLES" )
if(sum(grepl(paste0(" OV" ,Sys.time() %> % format(" %y" )), samples$samples)) > 0){
next.samp< -gsub(paste0(" OV" ,Sys.time() %> % format(" %y" ))," " , samples$samples) %> % as.numeric %> % max(na.rm=T)+1
}else{
next.samp< -1
< pre class = "r" > < code > sqlWriteTemp< -function(conn=dta, nhcs=nhc.test, file=" queryOV.xlsx" , samples.mod=T, clinics.mod=T, dbtype=NULL){
if(sqlTables(dta) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){dbtype< -" UM" }
if(sqlTables(dta) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" OV" }
if (dbtype==" OV" ){
upd.ovid< -sqlFetch(conn, " OVID" ) %> % filter(NHC %in% nhcs)
if (samples.mod){
## Generar código para las nuevas muestras
samples< -sqlFetch(conn, " SAMPLES" )
if(sum(grepl(paste0(" OV" ,Sys.time() %> % format(" %y" )), samples$samples)) > 0){
next.samp< -gsub(paste0(" OV" ,Sys.time() %> % format(" %y" ))," " , samples$samples) %> % as.numeric %> % max(na.rm=T)+1
}else{
next.samp< -1
}
last.samp< -next.samp+(length(nhcs)-1)
new.samp< -sprintf(" OV%s%02d" ,Sys.time() %> % format(" %y" ),next.samp:last.samp)
new.samp.df< -merge(sqlFetch(dta," OVID" ) %> % merge(data.frame(" NHC" =nhcs)), data.frame(" NHC" =nhcs, " samples" =new.samp))
samples.exp< -merge(samples %> % slice(0), new.samp.df %> % select(-NHC), all=T) %> % select(colnames(samples)) %> % arrange(samples)
}
if (clinics.mod){
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
upd.clinics< -sqlFetch(conn, " CLINICS" )
ovid.new< -sqlFetch(conn, " OVID" ) %> % filter(NHC %in% nhcs)
upd.clinics< -merge(ovid.new,upd.clinics, all.x=T, by=" OVID" )
upd.clinics$NHC< -as.character(upd.clinics$NHC)
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]< -as.Date(upd.clinics[,i])}
}
last.samp< -next.samp+(length(nhcs)-1)
new.samp< -sprintf(" OV%s%02d" ,Sys.time() %> % format(" %y" ),next.samp:last.samp)
new.samp.df< -merge(sqlFetch(dta," OVID" ) %> % merge(data.frame(" NHC" =nhcs)), data.frame(" NHC" =nhcs, " samples" =new.samp))
samples.exp< -merge(samples %> % slice(0), new.samp.df %> % select(-NHC), all=T) %> % select(colnames(samples)) %> % arrange(samples)
}
if (clinics.mod){
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
upd.clinics< -sqlFetch(conn, " CLINICS" )
ovid.new< -sqlFetch(conn, " OVID" ) %> % filter(NHC %in% nhcs)
upd.clinics< -merge(ovid.new,upd.clinics, all.x=T, by=" OVID" )
upd.clinics$NHC< -as.character(upd.clinics$NHC)
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]< -as.Date(upd.clinics[,i])}
## Exportar tablas a la plantilla de entrada para su rellenado
wb < - loadWorkbook(file)
writeData(wb, " NHC" , upd.ovid)
if (samples.mod){writeData(wb," samples" ,samples.exp)}
if (clinics.mod){writeData(wb," CLINICS" ,upd.clinics)}
saveWorkbook(wb,file,overwrite = TRUE)
}
if (dbtype==" UM" ){
upd.umid< -sqlFetch(conn, " UMID" ) %> % filter(NHC %in% nhcs)
if (samples.mod){
## Generar código para las nuevas muestras
samples< -sqlFetch(conn, " MUESTRAS" )
if(sum(grepl(paste0(" UM" ,Sys.time() %> % format(" %y" )), samples$CODIGO)) > 0){
next.samp< -gsub(paste0(" UM" ,Sys.time() %> % format(" %y" ))," " , samples$CODIGO) %> % as.numeric %> % max(na.rm=T)+1
}else{
next.samp< -1
}
last.samp< -next.samp+(length(nhcs)-1)
new.samp< -sprintf(" UM%s%02d" ,Sys.time() %> % format(" %y" ),next.samp:last.samp)
new.samp.df< -merge(sqlFetch(dta," UMID" ) %> % merge(data.frame(" NHC" =nhcs)), data.frame(" NHC" =nhcs, " CODIGO" =new.samp))
samples.exp< -merge(samples %> % slice(0), new.samp.df %> % select(-NHC), all=T) %> % select(colnames(samples)) %> % arrange(CODIGO)
}
if (clinics.mod){
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
upd.clinics< -sqlFetch(conn, " CLINICOS" )
umid.new< -sqlFetch(conn, " UMID" ) %> % filter(NHC %in% nhcs)
upd.clinics< -merge(umid.new,upd.clinics %> % select(-Id), all.x=T, by=" UMID" )
upd.clinics$NHC< -as.character(upd.clinics$NHC)
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]< -as.Date(upd.clinics[,i])}
}
## Exportar tablas a la plantilla de entrada para su rellenado
wb < - loadWorkbook(file)
writeData(wb, " NHC" , upd.umid)
if (samples.mod){writeData(wb," samples" ,samples.exp)}
if (clinics.mod){writeData(wb," CLINICS" ,upd.clinics)}
saveWorkbook(wb,file,overwrite = TRUE)
}
## Exportar tablas a la plantilla de entrada para su rellenado
wb < - loadWorkbook(file)
writeData(wb, " NHC" , upd.ovid)
if (samples.mod){writeData(wb," samples" ,samples.exp)}
if (clinics.mod){writeData(wb," CLINICS" ,upd.clinics)}
saveWorkbook(wb,file,overwrite = TRUE)
}< / code > < / pre >
< hr / >
< / div >
@ -601,7 +708,7 @@ sqlWriteTemp()
< / div >
< div id = "usage-6" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlSincBD(conn=dta, filetemp=“QueryOV.xlsx”, sinc.samples=F, sinc.clinics=F )< / p >
< p > sqlSincBD(conn=dta, filetemp=“queryOV.xlsx”, sinc.samples=F, sinc.clinics=F, dbtype=NULL )< / p >
< / div >
< div id = "arguments-6" class = "section level3" >
< h3 > Arguments< / h3 >
@ -633,6 +740,10 @@ sqlWriteTemp()
< td > clinics.mod< / td >
< td > If TRUE (default is FALSE for security), it updates the CLINICS table in the DB with the information in the “CLINICS” template sheet.< / td >
< / tr >
< tr class = "odd" >
< td > dbtype< / td >
< td > used to manually specify the database type. It defaults to NULL and the type is deduced.< / td >
< / tr >
< / tbody >
< / table >
< / div >
@ -654,53 +765,193 @@ sqlSincBD(sinc.samples=T, sinc.clinics=T)
< / div >
< div id = "function-6" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlSincBD< -function(conn=dta, filetemp=" QueryOV.xlsx" , sinc.samples=F, sinc.clinics=F){
## Añadir código de muestra nueva a la base de datos
nsamples< -sqlFetch(conn, " SAMPLES" ) %> % nrow
upd.samples< -read.xlsx(filetemp, sheet = " samples" , detectDates = T)
if (nrow(upd.samples) > 0){rownames(upd.samples)< -(nsamples+1):(nsamples+nrow(upd.samples)) %> % as.character}
< pre class = "r" > < code > sqlSincBD< -function(conn=dta, filetemp=" queryOV.xlsx" , sinc.samples=F, sinc.clinics=F, dbtype=NULL){
if(sqlTables(dta) %> % filter(TABLE_NAME == " UMID" ) %> % nrow > 0){dbtype< -" UM" }
if(sqlTables(dta) %> % filter(TABLE_NAME == " OVID" ) %> % nrow > 0){dbtype< -" OV" }
if (sinc.samples & nrow(upd.samples) > 0){
upd.samples$IQ_date< -as.Date(upd.samples$IQ_date)
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.samples, tablename=" SAMPLES" , append = T, varTypes = c(" IQ_date" =" date" ))
print(" Tabla SAMPLES sincronizada." )
## Añadir código de muestra nueva a la base de datos
if (dbtype == " OV" ){
print(" DB OV detectada" )
nsamples< -sqlFetch(conn, " SAMPLES" ) %> % nrow
upd.samples< -read.xlsx(filetemp, sheet = " samples" , detectDates = T)
if (nrow(upd.samples) > 0){rownames(upd.samples)< -(nsamples+1):(nsamples+nrow(upd.samples)) %> % as.character}
if (sinc.samples & nrow(upd.samples) > 0){
upd.samples$IQ_date< -as.Date(upd.samples$IQ_date)
upd.samples$TIL_date< -as.Date(upd.samples$TIL_date)
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.samples, tablename=" SAMPLES" , append = T, varTypes = c(" IQ_date" =" date" ," TIL_date" =" date" ))
print(" Tabla SAMPLES sincronizada." )
}
}
if (dbtype == " UM" ){
print(" DB UM detectada" )
nsamples< -sqlFetch(conn, " MUESTRAS" ) %> % nrow
upd.samples< -read.xlsx(filetemp, sheet = " samples" , detectDates = T)
if (nrow(upd.samples) > 0){rownames(upd.samples)< -(nsamples+1):(nsamples+nrow(upd.samples)) %> % as.character}
if (sinc.samples & nrow(upd.samples) > 0){
upd.samples$FECHA_RECEPCION< -as.Date(upd.samples$FECHA_RECEPCION)
upd.samples$TIPO< -as.character(upd.samples$TIPO)
upd.samples$OBS< -as.character(upd.samples$OBS)
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.samples, tablename=" MUESTRAS" , append = T, varTypes = c(" FECHA_RECEPCION" =" Date" ), rownames = F)
print(" Tabla MUESTRAS sincronizada." )
}
}
## Añadir datos clínicos modificados a la base de datos
upd.clinics< -read.xlsx(filetemp, sheet = " CLINICS" ,detectDates = T)
ovid.mod< -upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, " CLINICS" ) %> % pull(OVID))]
rnames< -sqlFetch(conn, " CLINICS" ) %> % filter(OVID %in% ovid.mod) %> % rownames
clinics.mod< -upd.clinics %> % filter(OVID %in% ovid.mod) %> % select(-NHC)
rownames(clinics.mod)< -rnames
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.mod)[grepl(" DO|date" , colnames(clinics.mod))]
for (i in fechas){
clinics.mod[,i]< -as.Date(clinics.mod[,i])
if (dbtype == " OV" ){
upd.clinics< -read.xlsx(filetemp, sheet = " CLINICS" ,detectDates = T)
ovid.mod< -upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, " CLINICS" ) %> % pull(OVID))]
rnames< -sqlFetch(conn, " CLINICS" ) %> % filter(OVID %in% ovid.mod) %> % rownames
clinics.mod< -upd.clinics %> % filter(OVID %in% ovid.mod) %> % select(-NHC)
rownames(clinics.mod)< -rnames
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.mod)[grepl(" DO|date" , colnames(clinics.mod))]
for (i in fechas){
clinics.mod[,i]< -as.Date(clinics.mod[,i])
}
sqlUpdate(conn, clinics.mod," CLINICS" )
print(" Tabla CLINICS modificada." )
}
}
if (dbtype == " UM" ){
upd.clinics< -read.xlsx(filetemp, sheet = " CLINICS" ,detectDates = T)
umid.mod< -upd.clinics$UMID[upd.clinics$UMID %in% (sqlFetch(dta, " CLINICOS" ) %> % pull(UMID))]
rnames< -sqlFetch(conn, " CLINICOS" ) %> % filter(UMID %in% umid.mod) %> % rownames
clinics.mod< -upd.clinics %> % filter(UMID %in% umid.mod) %> % select(-NHC)
rownames(clinics.mod)< -rnames
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.mod)[grepl(" date|MET_DX|DoB" , colnames(clinics.mod), ignore.case = T)]
for (i in fechas){
clinics.mod[,i]< -as.Date(clinics.mod[,i])
}
sqlUpdate(conn, clinics.mod," CLINICOS" )
print(" Tabla CLINICOS modificada." )
}
sqlUpdate(conn, clinics.mod," CLINICS" )
print(" Tabla CLINICS modificada." )
}
## Añadir datos clínicos nuevos a la base de datos
nsamples.clin< -sqlFetch(conn, " CLINICS" ) %> % nrow
ovid.new< -upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(conn, " CLINICS" ) %> % pull(OVID))]
clinics.new< -upd.clinics %> % filter(OVID %in% ovid.new) %> % select(-NHC)
if (length(ovid.new) > 0){rownames(clinics.new)< -(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %> % as.character}
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.new)[grepl(" DO|date" , colnames(clinics.new))]
varTypes< -rep(" Date" ,length(fechas))
names(varTypes)< -fechas
for (i in fechas){
clinics.new[,i]< -as.Date(clinics.new[,i])
if (dbtype == " OV" ){
nsamples.clin< -sqlFetch(conn, " CLINICS" ) %> % nrow
ovid.new< -upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(conn, " CLINICS" ) %> % pull(OVID))]
clinics.new< -upd.clinics %> % filter(OVID %in% ovid.new) %> % select(-NHC)
if (length(ovid.new) > 0){rownames(clinics.new)< -(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %> % as.character}
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.new)[grepl(" DO|date" , colnames(clinics.new))]
varTypes< -rep(" Date" ,length(fechas))
names(varTypes)< -fechas
for (i in fechas){
clinics.new[,i]< -as.Date(clinics.new[,i])
}
sqlSave(conn, clinics.new, tablename=" CLINICS" , append = T, varTypes = varTypes)
print(" Tabla CLINICS sincronizada." )
}
sqlSave(conn, clinics.new, tablename=" CLINICS" , append = T, varTypes = varTypes)
print(" Tabla CLINICS sincronizada." )
}
if (dbtype == " UM" ){
nsamples.clin< -sqlFetch(conn, " CLINICOS" ) %> % nrow
umid.new< -upd.clinics$UMID[!upd.clinics$UMID %in% (sqlFetch(conn, " CLINICOS" ) %> % pull(UMID))]
clinics.new< -upd.clinics %> % filter(UMID %in% umid.new) %> % select(-NHC)
if (length(umid.new) > 0){rownames(clinics.new)< -(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %> % as.character}
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas< -colnames(clinics.new)[grepl(" date|MET_DX|DoB" , colnames(clinics.new), ignore.case = T)]
varTypes< -rep(" Date" ,length(fechas))
names(varTypes)< -fechas
for (i in fechas){
clinics.new[,i]< -as.Date(clinics.new[,i])
}
sqlSave(conn, clinics.new, tablename=" CLINICOS" , append = T, varTypes = varTypes)
print(" Tabla CLINICOS sincronizada." )
}
}
}< / code > < / pre >
< hr / >
< / div >
< / div >
< div id = "sqlmultisamples" class = "section level2" >
< h2 > sqlMultiSamples< / h2 >
< div id = "description-7" class = "section level3" >
< h3 > Description< / h3 >
< p > Prints a table compiling information about laboratory samples, scRNAseq samples and RNA/DNA samples.< / p >
< / div >
< div id = "usage-7" class = "section level3" >
< h3 > Usage< / h3 >
< p > sqlMultiSamples(kbl=F, NHC=F, full=F)< / p >
< / div >
< div id = "arguments-7" class = "section level3" >
< h3 > Arguments< / h3 >
< table >
< colgroup >
< col width = "50%" / >
< col width = "50%" / >
< / colgroup >
< thead >
< tr class = "header" >
< th > Argument< / th >
< th > Description< / th >
< / tr >
< / thead >
< tbody >
< tr class = "odd" >
< td > kbl< / td >
< td > formats the output table with the kableEstra style. Defaults to F.< / td >
< / tr >
< tr class = "even" >
< td > NHC< / td >
< td > adds the NHC to the table in addition to the UMID. Defaults to F.< / td >
< / tr >
< tr class = "odd" >
< td > full< / td >
< td > prints also the patients that doesn’t appear in the MUESTRAS table. Defaults to F.< / td >
< / tr >
< / tbody >
< / table >
< / div >
< div id = "details-7" class = "section level3" >
< h3 > Details< / h3 >
< p > Prints a table compiling information about laboratory samples, scRNAseq samples and RNA/DNA samples.< / p >
< / div >
< div id = "value-7" class = "section level3" >
< h3 > Value< / h3 >
< p > A data.frame or a kableExtra table.< / p >
< / div >
< div id = "examples-7" class = "section level3" >
< h3 > Examples< / h3 >
< pre class = "r" > < code > dta< -odbcConnect(" test" )
sqlMultiSamples()< / code > < / pre >
< / div >
< div id = "function-7" class = "section level3" >
< h3 > Function< / h3 >
< pre class = "r" > < code > sqlMultiSamples< -function(kbl=F, NHC=F, full=F){
query< -sqlQuery(dta, " SELECT U.UMID,U.NHC,M.FECHA_RECEPCION,M.TIPO,M.CODIGO,C.CODIGO,R.CODIGO,R.ESTADO
FROM ((UMID U
LEFT OUTER JOIN MUESTRAS M ON U.UMID=M.UMID)
LEFT OUTER JOIN CNAG C ON M.CODIGO=C.CODIGO)
LEFT OUTER JOIN RNADNA R ON M.CODIGO=R.CODIGO" ) %> % rename(" CNAG" =" CODIGO.1" ," RNADNA" =" CODIGO.2" )
if (full==F){query< - query %> % filter(!is.na(CODIGO))}
if (NHC==F){query< - query %> % select(-NHC)}
query< -query %> % mutate(
CNAG=case_when(!is.na(CNAG)~" X" ,TRUE~" " ),
RNADNA=case_when((!is.na(RNADNA) & (ESTADO==" ENV" ))~" X" ,
(!is.na(RNADNA) & (is.na(ESTADO)))~" ." ,
TRUE~" " )
) %> % select(-ESTADO)
if (kbl==T){
query %> % kableExtra::kbl() %> % kableExtra::kable_styling(full_width = F, bootstrap_options = c(" striped" ))
}else{
return(query)
}
}< / code > < / pre >
< / div >
< / div >