1

I am writing a csv file to a folder using write.table() function. After opening the file, when I try again to write the file to the folder it gives me this error.

 Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
  cannot open file 'D:/My_Work/output/Mydata.csv': Permission denied

I understood that this error comes because the file is already opened. Once I close the file and try writing to the folder, it works fine. I wanted to know if there is any way that I could write the file to the folder even when the file is already open and want to update the opened file with whatever changes made to the new file.

Below is the code: I am reading a sheet from an Excel workbook and saving it as a separate CSV file to a folder, file type as Microsoft Excel Comma Separated Values File.

config_file = loadWorkbook("D:/My_Work/Input/Main.xls")
where_tab = readWorksheet(config_file, sheet = "where", header = TRUE)
write.table(where_tab,"D:/My_Work/output/Mydata.csv",sep = ",",col.names = NA)

Using this package:

if(!require(XLConnect)){
  install.packages("XLConnect")

}
library(XLConnect)
R Bud
  • 261
  • 1
  • 4
  • 16
  • Can you post your code that creates the csv file and the code you're using to open it? (You aren't opening it with anything else such as Excel, are you?) – r2evans Jun 29 '16 at 15:32
  • @r2evans I have edited my question with the code. – R Bud Jun 29 '16 at 15:38
  • Are `where_tab` and `whereTable` supposed to be the same? – r2evans Jun 29 '16 at 15:39
  • Are you using `XLConnect`? (Please include the calls to `library` on which your code depends.) Does this happen if you use a data.frame not read in by `readWorksheet`? For instance, use `mtcars` or something similar, and see if you still have a file-locked problem. – r2evans Jun 29 '16 at 15:46
  • I am using XLConnect package, whereTable is supposed to be where_tab (sorry for the confusion). I tried writing a dataframe also, It is the same case. – R Bud Jun 29 '16 at 16:01
  • 1
    When posting questions to SO, it really to have a [minimal working example](http://stackoverflow.com/help/mcve), and to make them fully [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to those who might answer. I recommend you restart R (to unload all packages), trigger the problem using a known data set (like `mtcars`), and post all code used to do so. **Bottom line**: I cannot reproduce your problem with what you've provided, but it could be for many reasons. – r2evans Jun 29 '16 at 16:05

0 Answers0