Skip to content
Back to formatted view

Raw Message

Message-ID: <349704EB-EA61-49AC-B1F0-F20406D4ED24@comcast.net>
Date: 2010-01-01T22:11:16Z
From: David Winsemius
Subject: How to not to terminate read.table if the input file is empty?
In-Reply-To: <366c6f341001011342s4c1a9f72g5787635435f4eb89@mail.gmail.com>

On Jan 1, 2010, at 4:42 PM, Peng Yu wrote:

> On Fri, Jan 1, 2010 at 3:19 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>> On Fri, Jan 1, 2010 at 2:54 PM, Dylan Beaudette
>> <dylan.beaudette at gmail.com> wrote:
>>> ?try
>>
>> This works. Thank you!
>>
>>> f=try(read.table("empty_data.txt"))
>> Error in read.table("empty_data.txt") : no lines available in input
>
> One more question. Is there a way to suppress the output to the screen
> 'Error in read.table("empty_data.txt") : no lines available in input'?

As always, you should try reading the full help page. I know it must  
seem onerous but in this case (re-?)read all of the help page for "try".

-- 
David.
>
>>> if(class(f)=='try-error') {
>> +   f=NULL
>> + }
>>> print(f)
>> NULL
>>> g=try(read.table("data.txt"))
>>> print(g)
>>    V1     V2     V3     V4
>> 1 name title1 title2 title3
>> 2 row1   10.1   20.1   30.1
>> 3 row2   12.1   22.1   32.1
>>>
>>
>>
>>> On Fri, Jan 1, 2010 at 12:41 PM, Peng Yu <pengyu.ut at gmail.com>  
>>> 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

>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT