R/connection.R

Defines functions local_connection with_connection

Documented in local_connection with_connection

#' Connections which close themselves
#'
#' R file connections which are automatically closed.
#'
#' @template with
#' @param con For `with_connection()` a named list with the connection(s) to
#' create. For `local_connection()` the code to create a single connection,
#' which is then returned.
#' @param .local_envir `[environment]`\cr The environment to use for scoping.
#' @importFrom stats setNames
#' @examples
#' with_connection(list(con = file("foo", "w")), {
#'   writeLines(c("foo", "bar"), con)
#' })
#'
#' read_foo <- function() {
#'   readLines(local_connection(file("foo", "r")))
#' }
#' read_foo()
#'
#' unlink("foo")
#' @export
with_connection <- function(con, code) {

  stopifnot(all(is.named(con)))

  on.exit({
    for (connection in con) close(connection)
  })
  eval(substitute(code), envir = con, enclos = parent.frame())
}

#' @rdname with_connection
#' @export
local_connection <- function(con, .local_envir = parent.frame()) {
  defer(close(con), envir = .local_envir)
  con
}

Try the withr package in your browser

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

withr documentation built on March 18, 2022, 7:47 p.m.