[R] How to not to terminate read.table if the input file is

Peng Yu pengyu.ut at gmail.com
Fri Jan 1 22:36:50 CET 2010


On Fri, Jan 1, 2010 at 3:03 PM, jim holtman <jholtman at gmail.com> wrote:
> ?try
>
> df <- try(read.table(...))
> if (inherits(df, 'try-error')){  what to do if file empty }

For the case of using try, it is better to use inherits() rather than
class(), or it doesn't matter?

> On Fri, Jan 1, 2010 at 3:55 PM, Ted Harding <Ted.Harding at manchester.ac.uk>
> wrote:
>>
>> On 01-Jan-10 20:41:52, Peng Yu wrote:
>> > read.table terminates the program if the input file is empty. Is there
>> > way to let the program continue and return me a NULL instead of
>> > terminating the program?
>> >
>> > $ Rscript read_empty.R
>> >> read.table("empty_data.txt")
>> > Error in read.table("empty_data.txt") : no lines available in input
>> > Execution halted
>> > $ cat read_empty.R
>> > read.table("empty_data.txt")
>> > $ cat empty_data.txt; echo EOF
>> > EOF
>>
>> If I understand the point of your query correctly, the following,
>> from "?read.table", would be relevant:
>>
>> Value:
>>  A data frame (?data.frame?) containing a representation of the
>>  data in the file.
>>
>>  Empty input is an error unless "col.names" is specified, when a
>>  0-row data frame is returned: similarly giving just a header line
>>  if "header = TRUE" results in a 0-row data frame.  Note that in
>>  either case the columns will logical unless ?colClasses? was
>>  supplied.
>>
>> The result would not be NULL, but can be tested for zero rows.
>>
>> Hoping this helps,
>> Ted.
>>
>> --------------------------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 01-Jan-10                                       Time: 20:55:35
>> ------------------------------ XFMail ------------------------------
>>
>> ______________________________________________
>> 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.
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>



More information about the R-help mailing list