R's try() does not catch errors produced by file.remove() -
i trying catch error thrown file.remove()
, when example excel file opened , therefore cannot removed. seems r's try()
function doesn't work in case. following code still produces error console although argument silent
set true
, nothing stored variable:
removal.error <- try(file.remove("testfile.xlsx"), silent = t)
removal.error [1] false
any suggestions why case or workarounds catch such error?
nor try
or trycatch
functions catching error because remove.file
function working (as said @bhas) , throws false
value used handle error shown below:
write.csv2(x = data.frame(v1 = 1:10, v2 = letters[1:10]), file = 'file.csv') shell('start excel file.csv') # opens excel throw error rem <- try(file.remove('file.csv')) rem # [1] false if (rem == false) { shell('taskkill /f /im excel.exe') # handle error pointed rem_2 <- file.remove('file.csv') if(rem_2 == false) { # if not case, warn user warning(warnings()) class(rem) <- c(class(rem), "try-error") # , add try-error class 'rem' } }
after that, you'll able handle you've planned.
obs: run code line line, otherwise r
going faster excel , you're not going see error in console.
Comments
Post a Comment