Home » excel » excel – Remove a file that has been opened within R studio with read.xlsx

excel – Remove a file that has been opened within R studio with read.xlsx

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am doing a loop on different file and need to read a file extract the information and then delete it.

In R I use the package openxlsx

datatemp = openxlsx::read.xlsx(xlsxFile = "./source/aFile.xlsx")

However I can’t remove the file from windows, it tells me it is open in R studio, I tried to point read.xlsx to another file, and to closeAllConnections but it did not work.

Is there a way to disconnect R from the file without having to close R studio ?

Thanks

Romain

How to&Answers:

I am on Windows 7 and I used unlink which let me delete the file

datatemp = openxlsx::read.xlsx(xlsxFile = "temp.xlsx")
unlink(datatemp)

Then I was able to manually delete the file without a problem