rguides

writeLines()

writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)

writeLines() writes each element of a character vector as a separate line to a file or connection. It handles line endings automatically, converting the separator to the platform-native format by default. This makes it a straightforward choice for exporting text data from R.

Syntax

writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)

Parameters

ParameterTypeDefaultDescription
textcharacterA character vector whose elements will become separate lines
conconnection or characterstdout()File path (character string) or an active connection
sepcharacter"\\n"String appended after each line
useByteslogicalFALSEIf TRUE, pass strings byte-by-byte without re-encoding

Return Value

Returns NULL invisibly. The function is called primarily for its side effect of writing to a connection or file.

Examples

Writing to a file

# Basic file output
lines <- c("First line", "Second line", "Third line")
writeLines(lines, "output.txt")

# Read back to confirm
readLines("output.txt")
# [1] "First line"  "Second line" "Third line"

Controlling the line separator

Unix systems use \n (LF) as the line terminator, while Windows uses \r\n (CRLF). By default, writeLines() converts to the platform-native format.

# Explicit LF separator (Unix-style even on Windows)
writeLines(c("line a", "line b"), "unix.txt", sep = "\n")

# CRLF for Windows compatibility
writeLines(c("line a", "line b"), "windows.txt", sep = "\r\n")

Using a file path vs. a connection

When you pass a character string as con, writeLines() opens a file connection for the duration of the call, then closes it. If you pass an already-open connection, it writes from the current position.

# Opening a connection manually gives more control
con <- file("log.txt", open = "wt")
writeLines("Application started", con)
writeLines("Data loaded successfully", con)
close(con)

# Passing a file path is simpler for one-off writes
writeLines(c("entry one", "entry two"), "simple.txt")

Working with encodings

By default, R converts strings with marked encodings to the current locale before writing. Set useBytes = TRUE to suppress this re-encoding, which is useful when you’ve already handled encoding with iconv().

# Force byte-level output without re-encoding
writeLines("\u00e9cole", "french.txt", useBytes = TRUE)

Appending to existing files

writeLines() overwrites by default. To append, open the connection in append mode:

writeLines("First batch", "log.txt")
# Open in append mode
con <- file("log.txt", open = "a")
writeLines("Second batch", con)
close(con)

readLines("log.txt")
# [1] "First batch"  "Second batch"

Common Patterns

Exporting a character vector, one element per line:

words <- c("apple", "banana", "cherry")
writeLines(words, "fruit.txt")
# File contains:
# apple
# banana
# cherry

Writing lines from a loop with progress:

for (i in seq_along(files)) {
  writeLines(paste("Processing", files[i]), "progress.log", append = TRUE)
}

See Also

  • readLines() — read lines from a file or connection
  • cat() — concatenate and print with more formatting control
  • print() — print R objects with quotation marks