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}}
> -----Original Message----- > From: r-help-bounces at r-project.org on behalf of Rolf Turner > Sent: Tue 5/13/2008 6:12 PM > To: R help forum > Subject: [R] The try() function with read.delim(). > > > 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()?It is working for me using R-2.7.0 on a Mac running OS X 10.4 and it also runs pretty much as shown below on my Windoze machine (R-2.7.0 on Windows XP) So you may be encountering a bug - not sure what your sessionInfo() etc. is. Here's my output of your example> file <- "foo.bar" > file.exists(file)[1] FALSE> temp <- try(read.delim(file))Warning in file(file, "r") : cannot open file 'foo.bar': No such file or directory Error in file(file, "r") : cannot open the connection> temp[1] "Error in file(file, \"r\") : cannot open the connection\n" attr(,"class") [1] "try-error"> rm("temp") > tempError: object "temp" not found> temp <- try(read.delim(file))Warning in file(file, "r") : cannot open file 'foo.bar': No such file or directory Error in file(file, "r") : cannot open the connection> temp[1] "Error in file(file, \"r\") : cannot open the connection\n" attr(,"class") [1] "try-error"> class(temp)[1] "try-error"> sessionInfo()R version 2.7.0 (2008-04-22) powerpc-apple-darwin8.10.1 locale: en_CA.UTF-8/en_CA.UTF-8/C/C/en_CA.UTF-8/en_CA.UTF-8 attached base packages: [1] tcltk splines stats graphics grDevices utils datasets [8] methods base other attached packages: [1] relimp_0.9-8 abind_1.1-0 rpart_3.1-41 Rcmdr_1.3-14 [5] car_1.2-8 lme4_0.999375-14 Matrix_0.999375-9 lattice_0.17-6 [9] survival_2.34-1 loaded via a namespace (and not attached): [1] grid_2.7.0 tools_2.7.0>Best Steve McKinney> > Thanks for any insights. > > cheers, > > Rolf > > ###################################################################### > Attention:\ This e-mail message is privileged and confid...{{dropped:9}} > > ______________________________________________ > 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. > >
>>>>> "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(e, "error") { message("Caught error:", e at message, "\n") ## 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.