Dimitri Liakhovitski
2010-Nov-09 16:55 UTC
[R] How to program an error into an if-then statement
Hello! I am running a loop (for a range of dates) and in this loop I am reading in different files - based on a date that is part of the file name. However, for some of the dates, I have no file (no way to know which dates). So, when I try to read it in I get an error: Error in file(file, "rt") : cannot open the connection Question: I'd like to program an if-then statement in my code that says something like this: myfile<-read.csv(myfilename) if "cannot open the connection" - then do X What statement should I use under "if"? Thanks a lot! -- Dimitri Liakhovitski Ninah Consulting www.ninah.com
Henrique Dallazuanna
2010-Nov-09 16:59 UTC
[R] How to program an error into an if-then statement
Take a look in ?try and ?tryCatch myfile <- tryCatch(read.csv(myfilename), error = invisible) myfile$message On Tue, Nov 9, 2010 at 2:55 PM, Dimitri Liakhovitski < dimitri.liakhovitski@gmail.com> wrote:> Hello! > > I am running a loop (for a range of dates) and in this loop I am > reading in different files - based on a date that is part of the file > name. > However, for some of the dates, I have no file (no way to know which > dates). So, when I try to read it in I get an error: > > Error in file(file, "rt") : cannot open the connection > > Question: I'd like to program an if-then statement in my code that > says something like this: > > myfile<-read.csv(myfilename) > if "cannot open the connection" - then do X > > What statement should I use under "if"? > > Thanks a lot! > > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.com > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Nordlund, Dan (DSHS/RDA)
2010-Nov-09 17:20 UTC
[R] How to program an error into an if-then statement
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dimitri Liakhovitski > Sent: Tuesday, November 09, 2010 8:56 AM > To: r-help > Subject: [R] How to program an error into an if-then statement > > Hello! > > I am running a loop (for a range of dates) and in this loop I am > reading in different files - based on a date that is part of the file > name. > However, for some of the dates, I have no file (no way to know which > dates). So, when I try to read it in I get an error: > > Error in file(file, "rt") : cannot open the connection > > Question: I'd like to program an if-then statement in my code that > says something like this: > > myfile<-read.csv(myfilename) > if "cannot open the connection" - then do X > > What statement should I use under "if"? > > Thanks a lot! > >Rather than trying to read a non-existent file and generating an error, you might want to check if the file exists before trying to read it and take your alternative action if it doesn't exist. Check out ?file.exists Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204