On Mon, 11 May 2009, Dennis Fisher wrote:
> Colleagues,
>
> I have encountered a problem in version 2.9 of R, running in both Vista and
> OS X. My code is quite lengthy but the critical line is:
> if (file.exists(FILENAME)) readLines(FILENAME))
>
> This triggers the error:
> Error in file(con, "r") : cannot open the connection
> Calls: readLines -> file
> Execution halted
>
> The file that it is reading is being created on the fly, then eventually
> deleted. The conditional was used to ensure that the readLines command
would
> be executed only when the file existed.
>
> Is it possible that the file is deleted AFTER the conditional is evaluated
> but before the readLines command is executed? If so, is there any means to
> prevent an error condition?
It is possible. But so are a lot of other things, and your pretest is
just not enough -- for example another process might have the file
open for exclusive access (quite common in the Windows world) in which
case it exists but you cannot open it for reading.
There is only one sure way to find out if you can read a file, and
that is to try to read it. So I'd simply use try() or tryCatch() on
the readLines call.
>
> Any help will be greatly appreciated. Thanks.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-415-564-2220
> www.PLessThan.com
>
> ______________________________________________
> 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.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595