Skip to contents

Reads the first n lines of a file as text, tolerating the encodings research data actually ships in: a UTF-8/UTF-16 BOM, UTF-16 (E-Prime exports), and Latin-1 bytes that would otherwise make readLines() output error in later string operations. Intended for cheap format sniffing – deciding what a file is before committing to a reader – not for reading data.

Usage

text_peek(path, n = 20L)

Arguments

path

path to a file.

n

maximum number of lines to read.

Value

a character vector of at most n lines (UTF-8), or character(0).

Details

Returns character(0) for a missing, empty or unreadable file rather than erroring, so a caller can treat "cannot peek" as "not my format".

Examples

f <- tempfile(fileext = ".txt")
writeLines(c("*** Header Start ***", "Experiment: naming"), f)
text_peek(f, n = 2)
#> [1] "*** Header Start ***" "Experiment: naming"