-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwrite_lines.R
More file actions
33 lines (31 loc) · 1014 Bytes
/
write_lines.R
File metadata and controls
33 lines (31 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#' Write lines to a file
#'
#' The text is converted to UTF-8 encoding before writing.
#'
#' The files are opened in binary mode, so they always use exactly the string
#' given in `eol` as the line separator.
#'
#' To write a file with windows line endings use `write_lines(eol = "\r\n")`
#' @param text A character vector to write
#' @param path A character string giving the file path to write to.
#' @param eol The end of line characters to use between lines.
#' @return The UTF-8 encoded input text (invisibly).
#' @export
#' @examples
#' tf <- tempfile()
#'
#' write_lines(rownames(mtcars), tf)
#'
#' # Write with Windows style line endings
#' write_lines(rownames(mtcars), tf, eol = "\r\n")
#'
#' unlink(tf)
write_lines <- function(text, path, eol = "\n") {
if (length(path) != 1) {
stop("`path` must be a single element", call. = FALSE)
}
text <- as.character(text)
text <- enc2utf8(text)
path <- normalizePath(path, mustWork = FALSE)
invisible(.Call(brio_write_lines, text, path, eol))
}