[R] The try() function with read.delim().

Rolf Turner
Wed, 14 May 2008 01:12:02 -0000

I have written a function which reads data from files using read.delim
().
The names of these files are complicated and are built using arguments
to the wrapper function.  Since the files in question may or may not
exist, I thought to enclose the read.delim() call in a try():

	file <- <complicated expression>
	temp <- try(read.delim(file))
	if(inherits(temp,"try-error")) {
		(Do something)
	} else {
		(Do something else)
	}

But this doesn't work.  If file doesn't exist, then I get an error,
and the wrapper function terminates, despite the presence of the try().
The object ``temp'' never gets created.

I can of course easily get around the problem by testing with

	file.exists(file)

I just wondered --- why isn't try() working as expected here?  Am I
being
silly to expect it to work?  If so, what am I not understanding about
try()?

Thanks for any insights.

	cheers,

		Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}