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

688 views
Skip to first unread message

J Karon

unread,
Dec 13, 2013, 2:11:09 PM12/13/13
to r-h...@r-project.org
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.




--
View this message in context: http://r.789695.n4.nabble.com/Invalid-connection-error-message-when-trying-to-write-a-file-tp4682149.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Uwe Ligges

unread,
Dec 14, 2013, 12:05:46 PM12/14/13
to J Karon, r-h...@r-project.org


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

John Karon

unread,
Dec 15, 2013, 2:13:31 PM12/15/13
to Uwe Ligges, r-h...@r-project.org
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

Uwe Ligges

unread,
Dec 15, 2013, 2:30:43 PM12/15/13
to John Karon, r-h...@r-project.org


On 15.12.2013 20:13, John Karon wrote:
> 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)

Wrong, *either* use

write.table(LRtest.out, file="c:\\LRtest.txt", sep="\t")

or

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

Best,
Uwe Ligges

John Karon

unread,
Dec 16, 2013, 10:40:27 AM12/16/13
to Uwe Ligges, r-h...@r-project.org
Thanks for pointing out my error after specifying the destination in the
file( ) function. What you proposed also did not work.
It turns out the solution is to give the file name but not include the path;
the resulting file is written in the working directory.
The mystery is that including the path had previously work.

Uwe Ligges

unread,
Dec 16, 2013, 10:41:31 AM12/16/13
to John Karon, r-h...@r-project.org
I guess your problem is that you cannot write toplevel into "c:\" with
your permissions?

Best,
Uwe Ligges

J Karon

unread,
Dec 16, 2013, 6:38:02 PM12/16/13
to r-h...@r-project.org
Indeed, I presume something changed. I would get an error message with the file(description=.....) command. The mystery is that everything worked fine before. Thanks for your comments. john karon

From: Uwe Ligges-3 [via R]
Sent: Monday, December 16, 2013 8:44 AM
To: J Karon
Subject: Re: Invalid connection error message when trying to write a file

I guess your problem is that you cannot write toplevel into "c:\" with
your permissions?

Best,
Uwe Ligges


On 16.12.2013 16:40, John Karon wrote:

> Thanks for pointing out my error after specifying the destination in the
> file( ) function. What you proposed also did not work.
> It turns out the solution is to give the file name but not include the
> path; the resulting file is written in the working directory.
> The mystery is that including the path had previously work.
> John Karon
>
> -----Original Message----- From: Uwe Ligges
> Sent: Sunday, December 15, 2013 12:30 PM
> To: John Karon ; [hidden email]
>> To: J Karon ; [hidden email]
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--------------------------------------------------------------------------------

If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/Invalid-connection-error-message-when-trying-to-write-a-file-tp4682149p4682284.html
To unsubscribe from Invalid connection error message when trying to write a file, click here.
NAML



--
View this message in context: http://r.789695.n4.nabble.com/Invalid-connection-error-message-when-trying-to-write-a-file-tp4682149p4682305.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]
Reply all
Reply to author
Forward
0 new messages