Peng Yu
2009-Oct-27 22:22 UTC
[R] Why I get this error? Error in close.connection(f) : invalid connection
I don't understand why I can not close 'f'. This may be very simple, but I don't see why. Could somebody let me know? $ cat gzfile.csv "","V1","V2","V3","V4","V5" "1",1,5,9,13,17 "2",2,6,10,14,18 "3",3,7,11,15,19 "4",4,8,12,16,20 $ Rscript gzfile.R> f = file("gzfile.csv") > A = read.csv(f) > AX V1 V2 V3 V4 V5 1 1 1 5 9 13 17 2 2 2 6 10 14 18 3 3 3 7 11 15 19 4 4 4 8 12 16 20> close(f)Error in close.connection(f) : invalid connection Calls: close -> close.connection Execution halted
William Dunlap
2009-Oct-27 22:34 UTC
[R] Why I get this error? Error in close.connection(f) : invalidconnection
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Peng Yu > Sent: Tuesday, October 27, 2009 3:22 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Why I get this error? Error in > close.connection(f) : invalidconnection > > I don't understand why I can not close 'f'. This may be very simple, > but I don't see why. Could somebody let me know?The short answer is that f is not an open connection. Use f <- file(..., open="r") if you want to open it (and then read.csv should not close it). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > $ cat gzfile.csv > "","V1","V2","V3","V4","V5" > "1",1,5,9,13,17 > "2",2,6,10,14,18 > "3",3,7,11,15,19 > "4",4,8,12,16,20 > $ Rscript gzfile.R > > f = file("gzfile.csv") > > A = read.csv(f) > > A > X V1 V2 V3 V4 V5 > 1 1 1 5 9 13 17 > 2 2 2 6 10 14 18 > 3 3 3 7 11 15 19 > 4 4 4 8 12 16 20 > > close(f) > Error in close.connection(f) : invalid connection > Calls: close -> close.connection > Execution halted > > ______________________________________________ > R-help at 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. >
baptiste auguie
2009-Oct-27 22:37 UTC
[R] Why I get this error? Error in close.connection(f) : invalid connection
Hi,>From ?read.csv"Alternatively, file can be a readable text-mode connection (which will be opened for reading if necessary, and if so closed (and hence destroyed) at the end of the function call)" HTH, baptiste 2009/10/27 Peng Yu <pengyu.ut at gmail.com>:> I don't understand why I can not close 'f'. This may be very simple, > but I don't see why. Could somebody let me know? > > $ cat gzfile.csv > "","V1","V2","V3","V4","V5" > "1",1,5,9,13,17 > "2",2,6,10,14,18 > "3",3,7,11,15,19 > "4",4,8,12,16,20 > $ Rscript gzfile.R >> f = file("gzfile.csv") >> A = read.csv(f) >> A > ?X V1 V2 V3 V4 V5 > 1 1 ?1 ?5 ?9 13 17 > 2 2 ?2 ?6 10 14 18 > 3 3 ?3 ?7 11 15 19 > 4 4 ?4 ?8 12 16 20 >> close(f) > Error in close.connection(f) : invalid connection > Calls: close -> close.connection > Execution halted > > ______________________________________________ > R-help at 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. >