[R] Can scan() detect end-of-file?

William Dunlap wdunlap at tibco.com
Tue Oct 20 01:27:01 CEST 2015


> 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 <wdunlap at 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



More information about the R-help mailing list