My R script has to do a lot of Excel open/write/close/open/read/close, with things in the middle, and I use for that the RODBC package. There seems to be an issue with odbcCloseAll which won’t close opened Excel connections after a number of times.
Simple example (hopefully reproducible and not just because my PC is weird):
require(RODBC)
filename <- tempfile(fileext='.xls')
for(i in 1:100) {
xlsFile <- odbcConnectExcel(filename, readOnly=FALSE)
sqlSave(xlsFile, USArrests, rownames = FALSE)
odbcCloseAll()
xlsFile <- odbcConnectExcel(filename, readOnly=FALSE)
template <- sqlFetch(xlsFile, "USArrests")
odbcCloseAll()
file.remove(filename)
}
And at some point (around i = 50 in my case), the loop crashes with:
Error in sqlSave(xlsFile, USArrests, rownames = FALSE) :
table ‘USArrests’ already exists
In addition: Warning message:
In file.remove(filename) :
cannot remove file 'c:\DOCUME~1\user\LOCALS~1\Temp\RtmpSFDDiG\file43522f58.xls', reason 'Permission denied'
The problem can easily be solved using odbcClose(xlsFile), or any other package linking to Excel, but for the sake of correctness, I’m wondering what is wrong with odbcCloseAll…
Found it, in the C source, there is an error in the C code:
should be
I’ll email the developer. In the mean time you can rebuild the package with this change.
The error is on line 1235.