R: Write to file without trailing newline

Viewed 2001

I'm trying to write values to separate files (to include them in my TeX file). But all of the methods I stubled across insert a new line at the end of the file (which results into an undesired space down the road). The manual (e.g. ?write) didn't provide any helpful information either.

# 1st try
write(x = "1", file = "test")

# 2nd try
fileConn<-file("test")
writeLines(c("1"), fileConn)
close(fileConn)

Thanks! Any idea?

2 Answers

You need to specify that you want to write the file in binary mode, when you open the connection.

text = c("a", "b", "c")
file_connection = file(file_path, open='wb')
writeLines(text = text, con = file_connection)
close(file_connection)
Related