by_line: Iterate over a connection by line

Description Usage Arguments Examples

View source: R/helpers.R

Description

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.

Usage

1
by_line(connection, FUN, STOP, safe = TRUE, max_lines = 0, ...)

Arguments

connection

an open connection, such as a text connection as created by file

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 FUN

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## 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)

## End(Not run)

blmoore/pythonistr documentation built on May 20, 2019, 3:34 p.m.