On 2025-02-05 5:20 p.m., Rolf Turner wrote:

On Wed, 5 Feb 2025 08:44:12 -0500
Duncan Murdoch <murdoch.dun...@gmail.com> wrote:

If I have this object:

    x <- c("abc\ndef", "", "ghi")

and I write it to a file using `writeLines(x, "test.txt")`, my text
editor sees a 5 line file:

    1: abc
    2: def
    3:
    4: ghi
    5:

which is what I'd expect:  the last line in the editor is empty.If I
use `readLines("test.txt")` on that file, I get the vector

    c("abc", "def", "", "ghi")


<SNIP>

Apologies for muddying the waters with my ignorance, but why would you
expect a 5 line file?   I would expect a 4 line file, and that is
indeed what I get when I try the code in question.

If I append an empty line at the end of my test.txt and then
apply readLines() to that file, I get [1] "abc" "def" ""    "ghi" "",
again as *I* would expect.

What am I missing?  Sorry for being a thicko.


That fifth line is more a function of the editor (RStudio) than the file. The last line ends with a newline. The editor shows this by including a blank 5th line. If the last line just stopped at the last letter, the editor would display it as a 4 line file.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to