[R] The try() function with read.delim().
Steven McKinney
smckinney at bccrc.ca
Wed May 14 04:23:43 CEST 2008
> -----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")
> temp
Error: 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.
>
>
More information about the R-help
mailing list