> I would like to read a connection line by line with scan but > don't know how to tell when to quit trying. Is there any > way that you can ask the connection object if it is at the end?
I found that adding the argument blank.lines.skip=FALSE to the scan command will do the trick when nlines=1. If the line is empty it returns a vector of one empty character string; at end-of-file it returns a zero-long character vector. E.g., > cat(t <- "OneA OneB\n\n\"Third\nLine A\" \"Line3B\" Line3C\n", file= tf <- > tempfile()) > str({tcon <- file(tf, "r"); lapply(1:5, function(i)scan(tcon, nlines=1, > what=list(""), blank.lines.skip=FALSE, quiet=TRUE))}) List of 5 $ :List of 1 ..$ : chr [1:2] "OneA" "OneB" $ :List of 1 ..$ : chr "" $ :List of 1 ..$ : chr [1:3] "Third\nLine A" "Line3B" "Line3C" $ :List of 1 ..$ : chr(0) $ :List of 1 ..$ : chr(0) > str({tcon <- file(tf, "r"); lapply(1:5, function(i)scan(tcon, nlines=1, > what=list(""), blank.lines.skip=TRUE, quiet=TRUE))}) List of 5 $ :List of 1 ..$ : chr [1:2] "OneA" "OneB" $ :List of 1 ..$ : chr(0) $ :List of 1 ..$ : chr [1:3] "Third\nLine A" "Line3B" "Line3C" $ :List of 1 ..$ : chr(0) $ :List of 1 ..$ : chr(0) > str({tcon <- file(tf, "r"); lapply(1:5, function(i)scan(tcon, nlines=1, > what="", blank.lines.skip=FALSE, quiet=TRUE))}) List of 5 $ : chr [1:2] "OneA" "OneB" $ : chr "" $ : chr [1:3] "Third\nLine A" "Line3B" "Line3C" $ : chr(0) $ : chr(0) > str({tcon <- file(tf, "r"); lapply(1:5, function(i)scan(tcon, nlines=1, > what="", blank.lines.skip=TRUE, quiet=TRUE))}) List of 5 $ : chr [1:2] "OneA" "OneB" $ : chr(0) $ : chr [1:3] "Third\nLine A" "Line3B" "Line3C" $ : chr(0) $ : chr(0) Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Oct 15, 2015 at 1:16 PM, William Dunlap <wdun...@tibco.com> wrote: > I would like to read a connection line by line with scan but > don't know how to tell when to quit trying. Is there any > way that you can ask the connection object if it is at the end? > > E.g., > > t <- 'A "Two line\nentry"\n\n"Three\nline\nentry" D E\n' > tfile <- tempfile() > cat(t, file=tfile) > tcon <- file(tfile, "r") # or tcon <- textConnection(t) > scan(tcon, what="", nlines=1) > #Read 2 items > #[1] "A" "Two line\nentry" >> scan(tcon, what="", nlines=1) # empty line > #Read 0 items > #character(0) > scan(tcon, what="", nlines=1) > #Read 3 items > #[1] "Three\nline\nentry" "D" "E" > scan(tcon, what="", nlines=1) # end of file > #Read 0 items > #character(0) > scan(tcon, what="", nlines=1) # end of file > #Read 0 items > #character(0) > > I am reading virtual line by virtual line because the lines > may have different numbers of fields. > > Bill Dunlap > TIBCO Software > wdunlap tibco.com ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.