[R] Debug

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Oct 1 17:21:41 CEST 2009

Hi Tammy,

On Oct 1, 2009, at 11:04 AM, Tammy Ma wrote:

> Hi, R-Users,'
> I have written a programe to process the bunch of files in one folder.
> But there is a error came out;
> How can I know which file returns error when being processed?
> I have tested the first 1-10th files, no any problem. But the  
> problems is I have more than 500 files. How can I know which file  
> has the problem?

See ?tryCatch

Here's some (untested) code that should give you an idea of what to do:

which.files <- dir(path="/directory/with/files", full.names=TRUE)
for (file.name in which.files) {
   result <- tryCatch(process.file(file.name), error=function(err) err)
   if (inherits(result, 'error')) {
     cat("File", file.name, "is hosed\n")

This assumes you have a function called 'process.file' that does what  
you want. If the function throws an error, it will be caught and  
stored in the result variable. In this case, the file.name will be  
written to your workspace.

Instead of just printing to your workspace, you could consider storing  
the bad file names in a list and post-process later.

Hope that helps,

Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the R-help mailing list