readr icon indicating copy to clipboard operation
readr copied to clipboard

[Feature request]: add check for newline at end of original file before appending to it with `write_csv()`

Open morrisLuke opened this issue 11 months ago • 0 comments

write_csv() seems to assume when appending to a file that the file concludes with a newline. However, when creating a file in Excel and saving as CSV, that behavior can't be 100% assumed.

Suggestion: When append is TRUE start with a check that the original file ends with a newline, and add one before starting append if not.

Current state of feature:

Take testfile.csv to read

a, b, c, d
1, 2, 3, 4
5, 6, 7, 8

(note lack of newline at end)

and environment variable whatToAdd to read

tribble( ~a, ~b, ~c, ~d, 9, 10, 11, 12, 13, 14, 15, 16 )

when I perform readr::write_csv(whatToAdd, "testfile.csv", append = TRUE) I'm expecting the appending to start on a new line. However I'm instead getting a testfile.csvlooking like

a, b, c, d
1, 2, 3, 4
5, 6, 7, 89, 10, 11, 12
13, 14, 15, 16

Just trying to save the next person from falling into the issue that sucked up a good chunk of my morning. Take care!

morrisLuke avatar Nov 07 '24 02:11 morrisLuke