Skip to content
Prev 255791 / 398506 Next

how to handle no lines in input with pipe()

On Mon, 4 Apr 2011, Andrew Yee wrote:

            
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.