This has to do with using pipe() and grep and read.csv() I have a .csv file that I grep using pipe() and read.csv() as follows: read.csv(pipe('grep foo bar.csv')) However, is there a way to have this command run when for example, there is no "foo" text in the bar.csv file? I get an error message (appropriately): Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input Is there a way to "inspect" the output of pipe before passing it on to read.csv()? Thanks, Andrew
On Mon, 4 Apr 2011, Andrew Yee wrote:> This has to do with using pipe() and grep and read.csv() > > I have a .csv file that I grep using pipe() and read.csv() as follows: > > read.csv(pipe('grep foo bar.csv')) > > However, is there a way to have this command run when for example, > there is no "foo" text in the bar.csv file? I get an error message > (appropriately): > > Error in read.table(file = file, header = header, sep = sep, quote = quote, : > no lines available in input > > Is there a way to "inspect" the output of pipe before passing it on to > read.csv()?You have to read from a pipe to 'inspect' it. So tmp <- readLines(pipe('grep foo bar.csv')) if(!length(tmp)) do something else else { res <- read.csv(con <- textConnection(tmp)) close(con) } OTOH, unless the file is enormous you could simply read it into R and use grep(value = TRUE) on the character vector.> > Thanks, > Andrew > > ______________________________________________ > 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. >-- 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
Perhaps use tryCatch... --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Andrew Yee <yee@post.harvard.edu> wrote: This has to do with using pipe() and grep and read.csv() I have a .csv file that I grep using pipe() and read.csv() as follows: read.csv(pipe('grep foo bar.csv')) However, is there a way to have this command run when for example, there is no "foo" text in the bar.csv file? I get an error message (appropriately): Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input Is there a way to "inspect" the output of pipe before passing it on to read.csv()? Thanks, Andrew_____________________________________________ 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 and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]