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.
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"
