[R] Potential bug in readLines when reading empty lines
Heuvel, E.G. van den (Guido)
g@v@ndenheuve| @end|ng |rom cb@@n|
Wed Jun 25 08:59:58 CEST 2025
Hi all,
I encountered some weird behaviour with readLines() recently, and I am wondering if this might be a bug, or, if it is not, how to resolve it. The issue is as follows:
If I have a text file where a line ends with just a carriage return (\r, CR) while the next line is empty and ends in a carriage return / linefeed (\r\n, CR LF), then the empty line is skipped when reading the file with readLines. The following code contains a test case:
---
print(R.version)
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# crt ucrt
# system x86_64, mingw32
# status
# major 4
# minor 4.0
# year 2024
# month 04
# day 24
# svn rev 86474
# language R
# version.string R version 4.4.0 (2024-04-24 ucrt)
# nickname Puppy Cup
txt_original <- paste0("Line 1\r", "\r\n", "Line 3\r\n")
# Write txt_original as binary to avoid unwanted conversion of end of line markers
writeBin(charToRaw(txt_original), "test.txt")
txt_actual <- readLines("test.txt")
print(txt_actual)
# [1] "Line 1" "Line 3"
---
I included the output of this script on my machine in the comments. I would expect txt_actual to be equal to c("Line 1", "", "Line 3"), but the empty line is skipped.
Is this a bug? And if not, how should I read test.txt in such a way that the empty 2nd line is left intact?
Best regards,
Guido van den Heuvel
Statistics Netherlands
More information about the R-help
mailing list