sample_lines: Read in random lines from a text file

Description Usage Arguments Details Value See Also Examples

View source: R/textutils.R

Description

Read in random lines from a text file

Usage

1
sample_lines(filename, n, nlines = NULL)

Arguments

filename

character containing the filename of the file from which the lines should be read.

n

The number of lines that should be sampled from the file.

nlines

The total number of lines in the file. If not specified or NULL the number of lines is first determined using determine_nlines.

Details

When nlines is not specified, the total number of lines is first determined. This can take quite some time. Therefore, specifying the number of lines can cause a significant speed up. It can also be used to sample lines from the first nlines line by specifying a value for nlines that is smaller than the number of lines in the file.

Value

Returns a character vector with the sampled lines.

See Also

See readLines to read in all lines a text file; get_lines can be used to read in specified lines.

Examples

1
2
3
4
5
6
7
8
# Create temporary filename
tmpcsv  <- tempfile(fileext="csv")

writeLines(letters[1:20], con=tmpcsv)
sample_lines(tmpcsv, 10)

# Cleanup
file.remove(tmpcsv)

djvanderlaan/LaF documentation built on April 2, 2020, 7:10 a.m.