[R] Invalid connection error message when trying to write a file

John Karon jkaron at earthlink.net
Sun Dec 15 20:13:31 CET 2013


The response below asks what I actually did.

I defined a function (details omitted; it computes the data frame 
LRtest.out); arguments include "path\\filename.csv" to which I want to write 
a data frame using write.csv(  ).  Repeated executions of the function 
(without the file(  ) and close(  ) instructions) were successful until 2 
days ago, when I received the error message below.  I simplified the code to 
write a file and received the error message below (same message as before) 
in response to the commands

zz<-file(description="c:\\LRtest.txt","w")
write.table(LRtest.out, file="c:\\LRtest.txt", sep="\t")
close(zz)

Error in file(description = "c:\\LRtest.txt", "w") :
  cannot open the connection
In addition: Warning message:
In file(description = "c:\\LRtest.txt", "w") :
  cannot open file 'c:\LRtest.txt': Permission denied

This happens whether there is no previous file with that name or an 
essentially empty file with that name.  In previous executions of code with 
a path to a folder, executing the file(  )  command would create an empty 
file.  Now no empty file is created.  The problem persists after rebooting 
the computer.

I also tried writing to the clipboard (description ="clipboard" in the 
file(  ) command); that was unsuccessful, with file="clipboard" or no file 
statement in the write.table(  ) command (Word showed there was something to 
paste, but pasting into an empty Word document did not put text into the 
document; with no file statement, the data frame was written to the 
console).

I question whether there is a setting that forbids writing to a file. 
Information on putting the data frame on the clipboard would also help. 
Thanks for any help.  John Karon

-----Original Message----- 
From: Uwe Ligges
Sent: Saturday, December 14, 2013 10:05 AM
To: J Karon ; r-help at r-project.org
Subject: Re: [R] Invalid connection error message when trying to write a 
file



On 13.12.2013 20:11, J Karon wrote:
> I get an invalid connection method error message when trying to write an R
> object from a user-defined function to my hard drive (running Windows 7)
> using write.csv.  I have previously not had this problem with the same
> user-defined function.  The error message is
>
> Error in isOpen(file, "w") : invalid connection
> In addition: Warning message:
> In if (file == "") file <- stdout() else if (is.character(file)) { :
>    the condition has length > 1 and only the first element will be used
>
> Using
> zz<-file(description="path","w")
> write.csv(  )
> close(zz)
>
> creates an empty file but yields the same error message when I execute
> write.csv.


Please tell us what you actually did.

This works for me:

zz <- file(description="path", "w")
write.csv(iris, zz)
close(zz)

Best,
Uwe Ligges



More information about the R-help mailing list