R/utils-io.R

Defines functions detect_line_ending write_lines read_lines writeLines readLines

readLines <- function(...) stop("Use read_lines!")
writeLines <- function(...) stop("Use write_lines!")

read_lines <- function(path, n = -1L) {
  base::readLines(path, n = n, encoding = "UTF-8", warn = FALSE)
}

write_lines <- function(text, path, line_ending = detect_line_ending(path)) {
  # we need to convert any embedded newlines as well
  text <- gsub("\r?\n", line_ending, text)

  path <- file(path, open = "wb")
  base::writeLines(enc2utf8(text), path, sep = line_ending, useBytes = TRUE)
  close(path)
}

detect_line_ending <- function(path) {
  tryCatch({
    samp <- suppressWarnings(readChar(path, nchars = 500))
    if (isTRUE(grepl("\r\n", samp))) "\r\n" else "\n"
  }, error = function(e) "\n")
}

Try the roxygen2 package in your browser

Any scripts or data that you put into this service are public.

roxygen2 documentation built on Sept. 8, 2021, 9:08 a.m.