[R] Looking for simple line-splitting code
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Thu Feb 6 00:04:30 CET 2025
On 2025-02-05 5:20 p.m., Rolf Turner wrote:
>
> On Wed, 5 Feb 2025 08:44:12 -0500
> Duncan Murdoch <murdoch.duncan using 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
More information about the R-help
mailing list