[R] how to use conditional statements to handle exceptions?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Mar 9 19:33:48 CET 2004


On Tue, 9 Mar 2004, Susan Lin wrote:

> I have a problem to handle the following statements.
>   
>   for(i in [1:3])
>   {
>     file=paste("file", i, ".dat")

Don't you mean paste("file", i, ".dat", sep="") ?  Or are your files
called `file 1 .dat'?

>     bb <- read.table(file)
>     x11()
>     plot(bb)
>     dev.off()

This opens a device, plots and closes it instantly.  Is that what you 
want?  I would have had

par(ask=TRUE)

outside the loop, which will automatically launch as graphics device if 
none is open.

> 
>   }
> When the input .dat file is empty, the program stops
> running and an error message appears. Could someone
> tell me how to handle this exception?

?try

-- 
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




More information about the R-help mailing list