Peng Yu
2010-Jan-01 20:41 UTC
[R] How to not to terminate read.table if the input file is empty?
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
Henrik Bengtsson
2010-Jan-01 20:53 UTC
[R] How to not to terminate read.table if the input file is empty?
Please give reproducible example, more information on your troubleshooting and sessionInfo(). Cannot reproduce:> pathname <- "empty.txt" > cat(file=pathname) > print(file.info(pathname))size isdir mode mtime ctime empty.txt 0 FALSE 666 2010-01-01 12:50:55 2010-01-01 12:45:34 atime exe empty.txt 2010-01-01 12:45:34 no> read.table(pathname)Error in read.table(pathname) : no lines available in input> sessionInfo()R version 2.10.1 Patched (2009-12-24 r50837) i386-pc-mingw32 locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base /Henrik 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 > > ______________________________________________ > 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. >
Dylan Beaudette
2010-Jan-01 20:54 UTC
[R] How to not to terminate read.table if the input file is empty?
?try 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 > > ______________________________________________ > 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. >
(Ted Harding)
2010-Jan-01 20:55 UTC
[R] How to not to terminate read.table if the input file is
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 > EOFIf 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 ------------------------------
Peng Yu
2010-Jan-01 21:12 UTC
[R] How to not to terminate read.table if the input file is empty?
Please reread my original post. You run it in a shell. I run it in a script. On Fri, Jan 1, 2010 at 2:53 PM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:> Please give reproducible example, more information on your > troubleshooting and sessionInfo(). ?Cannot reproduce: > >> pathname <- "empty.txt" >> cat(file=pathname) >> print(file.info(pathname)) > ? ? ? ? ?size isdir mode ? ? ? ? ? ? ? mtime ? ? ? ? ? ? ? ctime > empty.txt ? ?0 FALSE ?666 2010-01-01 12:50:55 2010-01-01 12:45:34 > ? ? ? ? ? ? ? ? ? ? ? ?atime exe > empty.txt 2010-01-01 12:45:34 ?no >> read.table(pathname) > Error in read.table(pathname) : no lines available in input >> sessionInfo() > R version 2.10.1 Patched (2009-12-24 r50837) > i386-pc-mingw32 > > locale: > [1] LC_COLLATE=English_United States.1252 > [2] LC_CTYPE=English_United States.1252 > [3] LC_MONETARY=English_United States.1252 > [4] LC_NUMERIC=C > [5] LC_TIME=English_United States.1252 > > attached base packages: > [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base > > /Henrik > > 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 >> >> ______________________________________________ >> 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. >> >
milton ruser
2010-Jan-01 22:35 UTC
[R] How to not to terminate read.table if the input file is empty?
?try() On Fri, Jan 1, 2010 at 3:41 PM, Peng Yu <pengyu.ut@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 > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]