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

Martin Maechler maechler at stat.math.ethz.ch
Wed May 14 11:11:24 CEST 2008


Oops, there where some typos / thinkos in my post,
in the "more sophisticated"  tryCatch() example. 
I append the full message with corrected example.
--
Martin
-------------------------------------------------------------------

>>>>> "RT" == Rolf Turner <r.turner at auckland.ac.nz>
>>>>>     on Wed, 14 May 2008 13:12:02 +1200 writes:

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

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

    RT> But this doesn't work.  If file doesn't exist, then I get an error,

You get an error *message* but not an error, I'd think.

If you don't like the error *message* in this case, use

 try(..., silent=TRUE)

In R,  try() is now using tryCatch(), (type 'try' to see) 
and I would nowadays recommend using tryCatch() inside any function.
Another "advantage" of tryCatch(.) for you is that you can
decide yourself what to do with the error message.

The most simple thing would be something like

	  if(is.null(tryCatch(temp <- read.delim(file),
			      error = function(e) NULL))) {
             ## error reading ...			      	      
	  } else {
	     ## work with the 'temp' data frame
          }

Slightly more sophisticated

 	  temp <- tryCatch(read.delim(file), error = function(e) e)
	  if(inherits(temp, "error") {
	     message("Caught error:", temp$message)
             ## error reading ...			      	      
	  } else {
	     ## work with the 'temp' data frame
          }


*However*, there's a small infelicity in the above:

You get an additional warning from file(..)  which you need to
suppress by another  suppressWarnings( ..... ) 
around the call.

Martin

    RT> and the wrapper function terminates, despite the presence of the try().
    RT> The object ``temp'' never gets created.

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

    RT> file.exists(file)

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

    RT> Thanks for any insights.

    RT> cheers,

    RT> Rolf

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

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

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



More information about the R-help mailing list