Description Usage Arguments Examples
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.
1 | 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 |
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.