Write Lines to a Connection

Write text lines to a connection.

file, connection
writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)
A character vector
A connection object or a character string.
character string. A string to be written to the connection after each line of text.
logical. See ‘Details’.

If the con is a character string, the function calls file to obtain a file connection which is opened for the duration of the function call.

If the connection is open it is written from its current position. If it is not open, it is opened for the duration of the call in "wt" mode and then closed again.

Normally writeLines is used with a text-mode connection, and the default separator is converted to the normal separator for that platform (LF on Unix/Linux, CRLF on Windows). For more control, open a binary connection and specify the precise value you want written to the file in sep. For even more control, use writeChar on a binary connection.

useBytes is for expert use. Normally (when false) character strings with marked encodings are converted to the current encoding before being passed to the connection (which might do further re-encoding). useBytes = TRUE suppresses the re-encoding of marked strings so they are passed byte-by-byte to the connection: this can be useful when strings have already been re-encoded by e.g.\ifelse{latex}{\out{~}}{ } iconv. (It is invoked automatically for strings with marked encoding "bytes".)

See Also

connections, writeChar, writeBin, readLines, cat

  • writeLines
Documentation reproduced from package base, version 3.3.0, License: Part of R 3.3.0

Community examples

richie@datacamp.com at Jan 21, 2017 base v3.3.2

## Basic usage `writeLines()` writes character vectors to the console. Each element is written on its own line, without quotes. See [`print()`](https://www.rdocumentation.org/packages/base/topics/print) for comparison. Example content uses [The Twelve Months](http://yourdailypoem.com/listpoem.jsp?poem_id=233) by George Ellis. ```{r} x <- paste(month.name, "is", c("Snowy", "Flowy", "Blowy", "Showery", "Flowery", "Bowery", "Hoppy", "Croppy", "Droppy", "Breezy", "Sneezy", "Freezy")) writeLines(x) print(x) ``` `writeLines()` displays quotes and backslashes as they would be read, rather than as R stores them. Again, compare with [`print()`](https://www.rdocumentation.org/packages/base/topics/print) and [`cat()`](https://www.rdocumentation.org/packages/base/topics/cat). ```{r} x <- c( "\"Am I being quoted?\", she asked the journalist.", "File paths are separated with \\ on Windows.", "CSV files are OK but I prefer to use \t to separate columns.", "Bill: \"What's your opinion on shruggies?\"\nTed: ¯\\_(\u30c4)_/¯ " ) writeLines(x) print(x) cat(x, sep = "\n") ``` The return value from `writeLines()` is `NULL`. It is invoked solely for the side-effect of writing output. ```{r} x <- paste(month.name, "is", c("Snowy", "Flowy", "Blowy", "Showery", "Flowery", "Bowery", "Hoppy", "Croppy", "Droppy", "Breezy", "Sneezy", "Freezy")) output <- writeLines(x) is.null(output) ``` ## `con` argument and writing to files As well as writing output to the console, `writeLines()` can write to files. ```{r} x <- paste(month.name, "is", c("Snowy", "Flowy", "Blowy", "Showery", "Flowery", "Bowery", "Hoppy", "Croppy", "Droppy", "Breezy", "Sneezy", "Freezy")) tmp <- tempfile() writeLines(x, con = tmp) readLines(tmp) ``` `writeLines()` can also make use of [`file()`](https://www.rdocumentation.org/packages/base/topics/connections) connections. ```{r} x <- paste(month.name, "is", c("Snowy", "Flowy", "Blowy", "Showery", "Flowery", "Bowery", "Hoppy", "Croppy", "Droppy", "Breezy", "Sneezy", "Freezy")) tmp <- tempfile() con <- file(tmp, "w") writeLines(x, con = con) close(con) readLines(tmp) ``` ## `sep` argument The `sep` argument controls the way that elements are separated in the output. Note that a separator is written after the last element of `x` (unlike [`cat()`](https://www.rdocumentation.org/packages/base/topics/cat) output). ```{r} writeLines(letters, sep = "***") cat(letters, sep = "***") ``` `sep` is not vectorized (again, unlike [`cat()`](https://www.rdocumentation.org/packages/base/topics/cat)). ```{r} writeLines(letters, sep = LETTERS) cat(letters, sep = LETTERS) ``` Although most text editors no longer care which line ending is used, you can mimic the traditional line ending on Windows by setting `sep = "\r\n"`. ```{r} x <- paste(month.name, "is", c("Snowy", "Flowy", "Blowy", "Showery", "Flowery", "Bowery", "Hoppy", "Croppy", "Droppy", "Breezy", "Sneezy", "Freezy")) tmp <- tempfile() writeLines(x, con = tmp, sep = "\r\n") readLines(tmp) ```