Browse Source

Add the documentation for the rest of the functions in "sqlFunctions.R".

main
Costa 3 years ago
parent
commit
29d0fefe69
2 changed files with 615 additions and 0 deletions
  1. +255
    -0
      Docs/sqlFunctions-doc.Rmd
  2. +360
    -0
      Docs/sqlFunctions-doc.html

+ 255
- 0
Docs/sqlFunctions-doc.Rmd

@ -85,3 +85,258 @@ sqlInitialize<-function(){
} }
``` ```
---
## sqlShowSamples
### Description
Shows if there are already samples from the specified NHCs.
### Usage
sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)
### Arguments
Argument|Description
---|---
conn|connection handle returned by odbcConnect.
nhcs|Character vector with the NHCs to test.
verb|Verbose: if TRUE, all the columns from "SAMPLES" table are printed.
### Details
Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients.
### Value
A data.frame with information about the patients.
### Examples
```r
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlShowSamples()
```
### Function
```r
sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){
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)
}else{
sqlQuery(conn, "SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID") %>% filter(NHC %in% nhcs)
}
}
```
---
## sqlGenOVID
### Description
Generates new consecutive OVID code for the patients that are not found in the DB.
### Usage
sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)
### Arguments
Argument|Description
---|---
conn|connection handle returned by odbcConnect.
nhcs|Character vector with the NHCs to test.
verb|Verbose: if TRUE (default), it prints the data.frame with the generated OVID codes.
sinc|If TRUE (default is FALSE for security), it adds the new entries to the "OVID" table in the DB.
### Details
Generates new consecutive OVID code for the patients that are not found in the DB.
### Value
If verb is TRUE, it returns a data.frame.
### Examples
```r
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlGenOVID(sinc=T)
```
### Function
```r
sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
ovid<-sqlFetch(conn,"OVID")
new.nhc<-nhcs[!nhcs %in% ovid$NHC]
next.num<-gsub("OVID","",ovid$OVID) %>% 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))
if (sinc){
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.ovid, tablename="OVID", append = T)
print("La base ha sido actualizada.")
}
if (verb){
return(upd.ovid)
}
}
```
---
## sqlWriteTemp
### Description
Fills the Query Template file with the OVID and OV newly generated codes.
### Usage
sqlWriteTemp(conn=dta, nhcs=nhc.test, file="queryOV.xlsx", samples.mod=T, clinics.mod=T)
### Arguments
Argument|Description
---|---
conn|connection handle returned by odbcConnect.
nhcs|Character vector with the NHCs to test.
file|Template file that will be used to interact with the DB.
samples.mod|If TRUE (default), it fills the "samples" template sheet.
clinics.mod|If TRUE (default), it fills the "CLINICS" template sheet.
### Details
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.
### Value
Invisibly for success (and failures cause errors).
### Examples
```r
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlGenOVID(sinc=T)
sqlWriteTemp()
```
### Function
```r
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
}
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)
}
```
---
## sqlSincBD
### Description
Updates the DB with the information filled in the template file.
### Usage
sqlSincBD(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clinics=F)
### Arguments
Argument|Description
---|---
conn|connection handle returned by odbcConnect.
filetemp|Template file that will be used to interact with the DB.
sinc.samples|If TRUE (default is FALSE for security), it updates the SAMPLES table in the DB with the information in the "samples" template sheet.
clinics.mod|If TRUE (default is FALSE for security), it updates the CLINICS table in the DB with the information in the "CLINICS" template sheet.
### Details
Updates the DB with the information filled in the template file. All the "samples" entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the "CLINICS" sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location.
### Value
Invisibly for success (and failures cause errors).
### Examples
```r
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlGenOVID(sinc=T)
sqlWriteTemp()
sqlSincBD(sinc.samples=T, sinc.clinics=T)
```
### Function
```r
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}
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 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])
}
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])
}
sqlSave(conn, clinics.new, tablename="CLINICS", append = T, varTypes = varTypes)
print("Tabla CLINICS sincronizada.")
}
}
```

+ 360
- 0
Docs/sqlFunctions-doc.html

@ -171,6 +171,10 @@ pre code {
<ul> <ul>
<li><a href="#sqldroplast">sqlDropLast</a></li> <li><a href="#sqldroplast">sqlDropLast</a></li>
<li><a href="#sqlinitizalize">sqlInitizalize</a></li> <li><a href="#sqlinitizalize">sqlInitizalize</a></li>
<li><a href="#sqlshowsamples">sqlShowSamples</a></li>
<li><a href="#sqlgenovid">sqlGenOVID</a></li>
<li><a href="#sqlwritetemp">sqlWriteTemp</a></li>
<li><a href="#sqlsincbd">sqlSincBD</a></li>
</ul> </ul>
</div> </div>
@ -282,6 +286,362 @@ sqlDropLast(dta, "TableTest")
## Conexión a la base de datos ## Conexión a la base de datos
source(&quot;ruta_database.R&quot;, encoding = &quot;UTF-8&quot;) source(&quot;ruta_database.R&quot;, encoding = &quot;UTF-8&quot;)
}</code></pre> }</code></pre>
<hr />
</div>
</div>
<div id="sqlshowsamples" class="section level2">
<h2>sqlShowSamples</h2>
<div id="description-2" class="section level3">
<h3>Description</h3>
<p>Shows if there are already samples from the specified NHCs.</p>
</div>
<div id="usage-2" class="section level3">
<h3>Usage</h3>
<p>sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)</p>
</div>
<div id="arguments-2" class="section level3">
<h3>Arguments</h3>
<table>
<thead>
<tr class="header">
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>conn</td>
<td>connection handle returned by odbcConnect.</td>
</tr>
<tr class="even">
<td>nhcs</td>
<td>Character vector with the NHCs to test.</td>
</tr>
<tr class="odd">
<td>verb</td>
<td>Verbose: if TRUE, all the columns from “SAMPLES” table are printed.</td>
</tr>
</tbody>
</table>
</div>
<div id="details-2" class="section level3">
<h3>Details</h3>
<p>Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients.</p>
</div>
<div id="value-2" class="section level3">
<h3>Value</h3>
<p>A data.frame with information about the patients.</p>
</div>
<div id="examples-2" class="section level3">
<h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlShowSamples()</code></pre>
</div>
<div id="function-2" class="section level3">
<h3>Function</h3>
<pre class="r"><code>sqlShowSamples&lt;-function(conn=dta, nhcs=nhc.test, verb=F){
if (isFALSE(verb)){
sqlQuery(conn, &quot;SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID&quot;) %&gt;% filter(NHC %in% nhcs) %&gt;%
group_by(NHC,OVID) %&gt;% summarise(Samples=length(samples), Names=paste0(samples, collapse = &quot;;&quot;)) %&gt;% merge(data.frame(NHC=nhcs),all=T)
}else{
sqlQuery(conn, &quot;SELECT O.NHC,S.*
FROM SAMPLES S
INNER JOIN OVID O
ON O.OVID=S.OVID&quot;) %&gt;% filter(NHC %in% nhcs)
}
}</code></pre>
<hr />
</div>
</div>
<div id="sqlgenovid" class="section level2">
<h2>sqlGenOVID</h2>
<div id="description-3" class="section level3">
<h3>Description</h3>
<p>Generates new consecutive OVID code for the patients that are not found in the DB.</p>
</div>
<div id="usage-3" class="section level3">
<h3>Usage</h3>
<p>sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)</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>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>conn</td>
<td>connection handle returned by odbcConnect.</td>
</tr>
<tr class="even">
<td>nhcs</td>
<td>Character vector with the NHCs to test.</td>
</tr>
<tr class="odd">
<td>verb</td>
<td>Verbose: if TRUE (default), it prints the data.frame with the generated OVID codes.</td>
</tr>
<tr class="even">
<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>
</tbody>
</table>
</div>
<div id="details-3" class="section level3">
<h3>Details</h3>
<p>Generates new consecutive OVID code for the patients that are not found in the DB.</p>
</div>
<div id="value-3" class="section level3">
<h3>Value</h3>
<p>If verb is TRUE, it returns a data.frame.</p>
</div>
<div id="examples-3" class="section level3">
<h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlGenOVID(sinc=T)</code></pre>
</div>
<div id="function-3" class="section level3">
<h3>Function</h3>
<pre class="r"><code>sqlGenOVID&lt;-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
ovid&lt;-sqlFetch(conn,&quot;OVID&quot;)
new.nhc&lt;-nhcs[!nhcs %in% ovid$NHC]
next.num&lt;-gsub(&quot;OVID&quot;,&quot;&quot;,ovid$OVID) %&gt;% as.numeric %&gt;% max(na.rm=T)+1
last.num&lt;-next.num+(length(new.nhc)-1)
upd.ovid&lt;-rbind(ovid,data.frame(&quot;NHC&quot;=new.nhc, &quot;OVID&quot;=sprintf(&quot;OVID%04d&quot;,next.num:last.num)))
rownames(upd.ovid)&lt;-as.character(1:nrow(upd.ovid))
upd.ovid&lt;-filter(upd.ovid, NHC %in% new.nhc) %&gt;% mutate(NHC=as.character(NHC))
if (sinc){
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.ovid, tablename=&quot;OVID&quot;, append = T)
print(&quot;La base ha sido actualizada.&quot;)
}
if (verb){
return(upd.ovid)
}
}</code></pre>
<hr />
</div>
</div>
<div id="sqlwritetemp" class="section level2">
<h2>sqlWriteTemp</h2>
<div id="description-4" class="section level3">
<h3>Description</h3>
<p>Fills the Query Template file with the OVID and OV newly generated codes.</p>
</div>
<div id="usage-4" class="section level3">
<h3>Usage</h3>
<p>sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T)</p>
</div>
<div id="arguments-4" class="section level3">
<h3>Arguments</h3>
<table>
<thead>
<tr class="header">
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>conn</td>
<td>connection handle returned by odbcConnect.</td>
</tr>
<tr class="even">
<td>nhcs</td>
<td>Character vector with the NHCs to test.</td>
</tr>
<tr class="odd">
<td>file</td>
<td>Template file that will be used to interact with the DB.</td>
</tr>
<tr class="even">
<td>samples.mod</td>
<td>If TRUE (default), it fills the “samples” template sheet.</td>
</tr>
<tr class="odd">
<td>clinics.mod</td>
<td>If TRUE (default), it fills the “CLINICS” template sheet.</td>
</tr>
</tbody>
</table>
</div>
<div id="details-4" 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>
</div>
<div id="value-4" class="section level3">
<h3>Value</h3>
<p>Invisibly for success (and failures cause errors).</p>
</div>
<div id="examples-4" class="section level3">
<h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlGenOVID(sinc=T)
sqlWriteTemp()</code></pre>
</div>
<div id="function-4" class="section level3">
<h3>Function</h3>
<pre class="r"><code>sqlWriteTemp&lt;-function(conn=dta, nhcs=nhc.test, file=&quot;queryOV.xlsx&quot;, samples.mod=T, clinics.mod=T){
upd.ovid&lt;-sqlFetch(conn, &quot;OVID&quot;) %&gt;% filter(NHC %in% nhcs)
if (samples.mod){
## Generar código para las nuevas muestras
samples&lt;-sqlFetch(conn, &quot;SAMPLES&quot;)
if(sum(grepl(paste0(&quot;OV&quot;,Sys.time() %&gt;% format(&quot;%y&quot;)), samples$samples)) &gt; 0){
next.samp&lt;-gsub(paste0(&quot;OV&quot;,Sys.time() %&gt;% format(&quot;%y&quot;)),&quot;&quot;, samples$samples) %&gt;% as.numeric %&gt;% max(na.rm=T)+1
}else{
next.samp&lt;-1
}
last.samp&lt;-next.samp+(length(nhcs)-1)
new.samp&lt;-sprintf(&quot;OV%s%02d&quot;,Sys.time() %&gt;% format(&quot;%y&quot;),next.samp:last.samp)
new.samp.df&lt;-merge(sqlFetch(dta,&quot;OVID&quot;) %&gt;% merge(data.frame(&quot;NHC&quot;=nhcs)), data.frame(&quot;NHC&quot;=nhcs, &quot;samples&quot;=new.samp))
samples.exp&lt;-merge(samples %&gt;% slice(0), new.samp.df %&gt;% select(-NHC), all=T) %&gt;% select(colnames(samples)) %&gt;% arrange(samples)
}
if (clinics.mod){
## Importar los datos clínicos de pacientes existentes y generar nueva entrada par los nuevos
upd.clinics&lt;-sqlFetch(conn, &quot;CLINICS&quot;)
ovid.new&lt;-sqlFetch(conn, &quot;OVID&quot;) %&gt;% filter(NHC %in% nhcs)
upd.clinics&lt;-merge(ovid.new,upd.clinics, all.x=T, by=&quot;OVID&quot;)
upd.clinics$NHC&lt;-as.character(upd.clinics$NHC)
for (i in colnames(upd.clinics)[sapply(upd.clinics, lubridate::is.POSIXct)]){upd.clinics[,i]&lt;-as.Date(upd.clinics[,i])}
}
## Exportar tablas a la plantilla de entrada para su rellenado
wb &lt;- loadWorkbook(file)
writeData(wb, &quot;NHC&quot;, upd.ovid)
if (samples.mod){writeData(wb,&quot;samples&quot;,samples.exp)}
if (clinics.mod){writeData(wb,&quot;CLINICS&quot;,upd.clinics)}
saveWorkbook(wb,file,overwrite = TRUE)
}</code></pre>
<hr />
</div>
</div>
<div id="sqlsincbd" class="section level2">
<h2>sqlSincBD</h2>
<div id="description-5" class="section level3">
<h3>Description</h3>
<p>Updates the DB with the information filled in the template file.</p>
</div>
<div id="usage-5" class="section level3">
<h3>Usage</h3>
<p>sqlSincBD(conn=dta, filetemp=“QueryOV.xlsx”, sinc.samples=F, sinc.clinics=F)</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>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>conn</td>
<td>connection handle returned by odbcConnect.</td>
</tr>
<tr class="even">
<td>filetemp</td>
<td>Template file that will be used to interact with the DB.</td>
</tr>
<tr class="odd">
<td>sinc.samples</td>
<td>If TRUE (default is FALSE for security), it updates the SAMPLES table in the DB with the information in the “samples” template sheet.</td>
</tr>
<tr class="even">
<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>
</tbody>
</table>
</div>
<div id="details-5" class="section level3">
<h3>Details</h3>
<p>Updates the DB with the information filled in the template file. All the “samples” entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the “CLINICS” sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location.</p>
</div>
<div id="value-5" class="section level3">
<h3>Value</h3>
<p>Invisibly for success (and failures cause errors).</p>
</div>
<div id="examples-5" class="section level3">
<h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlGenOVID(sinc=T)
sqlWriteTemp()
sqlSincBD(sinc.samples=T, sinc.clinics=T)</code></pre>
</div>
<div id="function-5" class="section level3">
<h3>Function</h3>
<pre class="r"><code>sqlSincBD&lt;-function(conn=dta, filetemp=&quot;QueryOV.xlsx&quot;, sinc.samples=F, sinc.clinics=F){
## Añadir código de muestra nueva a la base de datos
nsamples&lt;-sqlFetch(conn, &quot;SAMPLES&quot;) %&gt;% nrow
upd.samples&lt;-read.xlsx(filetemp, sheet = &quot;samples&quot;, detectDates = T)
if (nrow(upd.samples) &gt; 0){rownames(upd.samples)&lt;-(nsamples+1):(nsamples+nrow(upd.samples)) %&gt;% as.character}
if (sinc.samples &amp; nrow(upd.samples) &gt; 0){
upd.samples$IQ_date&lt;-as.Date(upd.samples$IQ_date)
### !! Atención, esto cambia la base de datos:
sqlSave(conn, upd.samples, tablename=&quot;SAMPLES&quot;, append = T, varTypes = c(&quot;IQ_date&quot;=&quot;date&quot;))
print(&quot;Tabla SAMPLES sincronizada.&quot;)
}
## Añadir datos clínicos modificados a la base de datos
upd.clinics&lt;-read.xlsx(filetemp, sheet = &quot;CLINICS&quot;,detectDates = T)
ovid.mod&lt;-upd.clinics$OVID[upd.clinics$OVID %in% (sqlFetch(dta, &quot;CLINICS&quot;) %&gt;% pull(OVID))]
rnames&lt;-sqlFetch(conn, &quot;CLINICS&quot;) %&gt;% filter(OVID %in% ovid.mod) %&gt;% rownames
clinics.mod&lt;-upd.clinics %&gt;% filter(OVID %in% ovid.mod) %&gt;% select(-NHC)
rownames(clinics.mod)&lt;-rnames
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas&lt;-colnames(clinics.mod)[grepl(&quot;DO|date&quot;, colnames(clinics.mod))]
for (i in fechas){
clinics.mod[,i]&lt;-as.Date(clinics.mod[,i])
}
sqlUpdate(conn, clinics.mod,&quot;CLINICS&quot;)
print(&quot;Tabla CLINICS modificada.&quot;)
}
## Añadir datos clínicos nuevos a la base de datos
nsamples.clin&lt;-sqlFetch(conn, &quot;CLINICS&quot;) %&gt;% nrow
ovid.new&lt;-upd.clinics$OVID[!upd.clinics$OVID %in% (sqlFetch(conn, &quot;CLINICS&quot;) %&gt;% pull(OVID))]
clinics.new&lt;-upd.clinics %&gt;% filter(OVID %in% ovid.new) %&gt;% select(-NHC)
if (length(ovid.new) &gt; 0){rownames(clinics.new)&lt;-(nsamples.clin+1):(nsamples.clin+nrow(clinics.new)) %&gt;% as.character}
### !! Atención, esto cambia la base de datos:
if (sinc.clinics){
fechas&lt;-colnames(clinics.new)[grepl(&quot;DO|date&quot;, colnames(clinics.new))]
varTypes&lt;-rep(&quot;Date&quot;,length(fechas))
names(varTypes)&lt;-fechas
for (i in fechas){
clinics.new[,i]&lt;-as.Date(clinics.new[,i])
}
sqlSave(conn, clinics.new, tablename=&quot;CLINICS&quot;, append = T, varTypes = varTypes)
print(&quot;Tabla CLINICS sincronizada.&quot;)
}
}</code></pre>
</div> </div>
</div> </div>

Loading…
Cancel
Save