Apply a function line-by-line to a connection,
such as an open file connection. This allows
simple processing without loading the entire
file to memory, something like the common
for line in file: ...
approach in python.
by_line(connection, FUN, STOP, safe = TRUE, max_lines = 0, ...)
connection | an open connection, such as a
text connection as created by |
---|---|
FUN | a function to apply to each line that can be read from the connection |
STOP | a function to generate a stop condition for reading lines of the connection |
safe | auto-stop iteraction on reaching an empty line in the connection (attempts to prevent infinite loops) |
... | extra arguments passed to |
not_run({ file <- file("myfile.txt", "r") # use with for automatic connection closing with(file, { # read and print each line sequentially by_line(file, print) }) # stop reading after finding the word 'stop' connex <- file("logfile.txt", "r") by_line(connex, cat, function(line) grepl(line, "stop")) close(connex) })