Christian.Stratowa@vie.boehringer-ingelheim.com
2003-Jan-28 16:04 UTC
[R] reading non-existent files
Dear R-experts I would like to read all files from a directory, the files have names "myname0001.txt" etc. I paste the directory plus file names and use "read.delim()". My problem is that some file names are missing, so I get an error and my program stops. Is there a way to check for a null pointer analogous to C, so that I can simply skip non-existent filenames? Please do "Reply to all" since in the company I am not subscribed to the R-help list. Thank you in advance Best regards Christian Stratowa =============================================Christian Stratowa, PhD Boehringer Ingelheim Austria Dept NCE Lead Discovery - Bioinformatics Dr. Boehringergasse 5-11 A-1121 Vienna, Austria Tel.: ++43-1-80105-2470 Fax: ++43-1-80105-2683 email: christian.stratowa at vie.boehringer-ingelheim.com
?file.exists On Tue, 28 Jan 2003 Christian.Stratowa at vie.boehringer-ingelheim.com wrote:> Dear R-experts > > I would like to read all files from a directory, the files have names > "myname0001.txt" etc. I paste the directory plus file names and > use "read.delim()". > My problem is that some file names are missing, so I get an error > and my program stops. > Is there a way to check for a null pointer analogous to C, so that > I can simply skip non-existent filenames? > > Please do "Reply to all" since in the company I am not subscribed > to the R-help list. > > Thank you in advance > Best regards > Christian Stratowa > > =============================================> Christian Stratowa, PhD > Boehringer Ingelheim Austria > Dept NCE Lead Discovery - Bioinformatics > Dr. Boehringergasse 5-11 > A-1121 Vienna, Austria > Tel.: ++43-1-80105-2470 > Fax: ++43-1-80105-2683 > email: christian.stratowa at vie.boehringer-ingelheim.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
Christian.Stratowa@vie.boehringer-ingelheim.com
2003-Jan-28 16:12 UTC
[R] reading non-existent files
Great, thank you for the fast reply Regards Christian Stratowa =============================================Christian Stratowa, PhD Boehringer Ingelheim Austria Dept NCE Lead Discovery - Bioinformatics Dr. Boehringergasse 5-11 A-1121 Vienna, Austria Tel.: ++43-1-80105-2470 Fax: ++43-1-80105-2683 email: christian.stratowa at vie.boehringer-ingelheim.com> -----Original Message----- > From: Ben Bolker [SMTP:ben at zoo.ufl.edu] > Sent: Tuesday, January 28, 2003 4:15 PM > To: Stratowa,Dr,Christian FEX BIG-AT-V > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] reading non-existent files > > > ?file.exists > > On Tue, 28 Jan 2003 Christian.Stratowa at vie.boehringer-ingelheim.com wrote: > > > Dear R-experts > > > > I would like to read all files from a directory, the files have names > > "myname0001.txt" etc. I paste the directory plus file names and > > use "read.delim()". > > My problem is that some file names are missing, so I get an error > > and my program stops. > > Is there a way to check for a null pointer analogous to C, so that > > I can simply skip non-existent filenames? > > > > Please do "Reply to all" since in the company I am not subscribed > > to the R-help list. > > > > Thank you in advance > > Best regards > > Christian Stratowa > > > > =============================================> > Christian Stratowa, PhD > > Boehringer Ingelheim Austria > > Dept NCE Lead Discovery - Bioinformatics > > Dr. Boehringergasse 5-11 > > A-1121 Vienna, Austria > > Tel.: ++43-1-80105-2470 > > Fax: ++43-1-80105-2683 > > email: christian.stratowa at vie.boehringer-ingelheim.com > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > -- > 318 Carr Hall bolker at zoo.ufl.edu > Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker > Box 118525 (ph) 352-392-5697 > Gainesville, FL 32611-8525 (fax) 352-392-3704
Hi Christian,
I suppose that you are doing this in a loop and is stopping.
Have a look at the function try()
?try
Try an Expression Allowing Error Recovery.
Description:
`try' is a wrapper to run an expression that might fail and allow
the user's code to handle error-recovery.
Usage:
try(expr, first = TRUE)
regards,
Jesus
--------------------------------------------------------------
Jes?s Mar?a Fr?as Celayeta
School of Food Sci. and Env. Health.
Faculty of Tourism and Food
Dublin Institute of Technology
Cathal Bruha St., Dublin 1. Ireland
Phone: +353 1 4024459 Fax: +353 1 4024495
http://www.dit.ie/DIT/tourismfood/science/staff/frias.html
--------------------------------------------------------------
> -----Original Message-----
> From: r-help-admin at stat.math.ethz.ch
> [mailto:r-help-admin at stat.math.ethz.ch]On Behalf Of
> Christian.Stratowa at vie.boehringer-ingelheim.com
> Sent: 28 January 2003 14:59
> To: r-help at stat.math.ethz.ch
> Subject: [R] reading non-existent files
>
>
> Dear R-experts
>
> I would like to read all files from a directory, the files have names
> "myname0001.txt" etc. I paste the directory plus file names and
> use "read.delim()".
> My problem is that some file names are missing, so I get an error
> and my program stops.
> Is there a way to check for a null pointer analogous to C, so that
> I can simply skip non-existent filenames?
>
> Please do "Reply to all" since in the company I am not subscribed
> to the R-help list.
>
> Thank you in advance
> Best regards
> Christian Stratowa
>
> =============================================> Christian Stratowa, PhD
> Boehringer Ingelheim Austria
> Dept NCE Lead Discovery - Bioinformatics
> Dr. Boehringergasse 5-11
> A-1121 Vienna, Austria
> Tel.: ++43-1-80105-2470
> Fax: ++43-1-80105-2683
> email: christian.stratowa at vie.boehringer-ingelheim.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> --
> This message has been scanned for viruses by
> the DIT Computer Centre MailScanner Service,
> and is believed to be clean.
>
--
This message has been scanned for viruses by
the DIT Computer Centre MailScanner Service,
and is believed to be clean.
On Tue, 28 Jan 2003 Christian.Stratowa at vie.boehringer-ingelheim.com wrote:> I would like to read all files from a directory, the files have names > "myname0001.txt" etc. I paste the directory plus file names and > use "read.delim()". > My problem is that some file names are missing, so I get an error > and my program stops.How can some of `all files from a directory' be missing? Why not read them via e.g. list.files()?> Is there a way to check for a null pointer analogous to C, so that > I can simply skip non-existent filenames??file.exists -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
>-----Original Message----- >From: r-help-admin at stat.math.ethz.ch >[mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of >Christian.Stratowa at vie.boehringer-ingelheim.com >Sent: Tuesday, January 28, 2003 8:59 AM >To: r-help at stat.math.ethz.ch >Subject: [R] reading non-existent files > > >Dear R-experts > >I would like to read all files from a directory, the files >have names "myname0001.txt" etc. I paste the directory plus >file names and use "read.delim()". My problem is that some >file names are missing, so I get an error and my program >stops. Is there a way to check for a null pointer analogous to >C, so that I can simply skip non-existent filenames? > >Please do "Reply to all" since in the company I am not >subscribed to the R-help list. > >Thank you in advance >Best regards >Christian StratowaYou could use file.exists() which returns a TRUE/FALSE to see if the file is present or perhaps list.files() which will return a character vector of existing files based upon a regex pattern matching string. HTH, Marc
Christian.Stratowa@vie.boehringer-ingelheim.com
2003-Jan-28 16:29 UTC
[R] reading non-existent files
Dear All Thank you all for your fast help, the solutions that I have received are: file.exists() try() allFiles<-list.files(mydir) Best regards Christian Stratowa =============================================Christian Stratowa, PhD Boehringer Ingelheim Austria Dept NCE Lead Discovery - Bioinformatics Dr. Boehringergasse 5-11 A-1121 Vienna, Austria Tel.: ++43-1-80105-2470 Fax: ++43-1-80105-2683 email: christian.stratowa at vie.boehringer-ingelheim.com> -----Original Message----- > From: ripley at stats.ox.ac.uk [SMTP:ripley at stats.ox.ac.uk] > Sent: Tuesday, January 28, 2003 4:20 PM > To: Stratowa,Dr,Christian FEX BIG-AT-V > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] reading non-existent files > > On Tue, 28 Jan 2003 Christian.Stratowa at vie.boehringer-ingelheim.com wrote: > > > I would like to read all files from a directory, the files have names > > "myname0001.txt" etc. I paste the directory plus file names and > > use "read.delim()". > > My problem is that some file names are missing, so I get an error > > and my program stops. > > How can some of `all files from a directory' be missing? Why not read > them via e.g. list.files()? > > > Is there a way to check for a null pointer analogous to C, so that > > I can simply skip non-existent filenames? > > ?file.exists > > -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 >