ces

Chambers, J. M. (1998) _Programming with Data. A Guide to the S Language._
Springer.

Ripley, B. D. (2001) Connections. _R News_ , **1/1** , 16--7. [
https://www.r-project.org/doc/Rnews/Rnews_2001-1.pdf
](https://www.r-project.org/doc/Rnews/Rnews_2001-1.pdf)

##  See Also

` [ textConnection ](/link/textConnection?package=base&version=3.6.2) ` , ` [
seek ](/link/seek?package=base&version=3.6.2) ` , ` [ showConnections
](/link/showConnections?package=base&version=3.6.2) ` , ` [ pushBack
](/link/pushBack?package=base&version=3.6.2) ` .

Functions making direct use of connections are (text-mode) ` [ readLines
](/link/readLines?package=base&version=3.6.2) ` , ` [ writeLines
](/link/writeLines?package=base&version=3.6.2) ` , ` [ cat
](/link/cat?package=base&version=3.6.2) ` , ` [ sink
](/link/sink?package=base&version=3.6.2) ` , ` [ scan
](/link/scan?package=base&version=3.6.2) ` , ` [ parse
](/link/parse?package=base&version=3.6.2) ` , ` [ read.dcf
](/link/read.dcf?package=base&version=3.6.2) ` , ` [ dput
](/link/dput?package=base&version=3.6.2) ` , ` [ dump
](/link/dump?package=base&version=3.6.2) ` and (binary-mode) ` [ readBin
](/link/readBin?package=base&version=3.6.2) ` , ` [ readChar
](/link/readChar?package=base&version=3.6.2) ` , ` [ writeBin
](/link/writeBin?package=base&version=3.6.2) ` , ` [ writeChar
](/link/writeChar?package=base&version=3.6.2) ` , ` [ load
](/link/load?package=base&version=3.6.2) ` and ` [ save
](/link/save?package=base&version=3.6.2) ` .

` [ capabilities ](/link/capabilities?package=base&version=3.6.2) ` to see if
` fifo ` connections are supported by this build of  R  .

` [ gzcon ](/link/gzcon?package=base&version=3.6.2) ` to wrap ` gzip `
(de)compression around a connection.

` [ options ](/link/options?package=base&version=3.6.2) ` ` HTTPUserAgent ` ,
` internet.info ` and ` timeout ` are used by some of the methods for URL
connections.

` [ memCompress ](/link/memCompress?package=base&version=3.6.2) ` for more
ways to (de)compress and references on data compression.

` [ extSoftVersion ](/link/extSoftVersion?package=base&version=3.6.2) ` for
the versions of the ` zlib ` (for ` gzfile ` ), ` bzip2 ` and ` xz ` libraries
in use.

To flush output to the Windows and macOS consoles, see ` [ flush.console
](/link/flush.console?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fconnections&utm_source=r-docs&utm_medium=docs&utm_term=connections&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    zzfil <- tempfile(fileext=".data")
    zz <- file(zzfil, "w")  # open an output file connection
    cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
    cat("One more line\n", file = zz)
    close(zz)
    readLines(zzfil)
    unlink(zzfil)
    
    zzfil <- tempfile(fileext=".gz")
    zz <- gzfile(zzfil, "w")  # compressed file
    cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
    close(zz)
    readLines(zz <- gzfile(zzfil))
    close(zz)
    unlink(zzfil)
    zz # an invalid connection
    
    zzfil <- tempfile(fileext=".bz2")
    zz <- bzfile(zzfil, "w")  # bzip2-ed file
    cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
    close(zz)
    zz # print() method: invalid connection
    print(readLines(zz <- bzfile(zzfil)))
    close(zz)
    unlink(zzfil)
    
    ## An example of a file open for reading and writing
    Tpath <- tempfile("test")
    Tfile <- file(Tpath, "w+")
    c(isOpen(Tfile, "r"), isOpen(Tfile, "w")) # both TRUE
    cat("abc\ndef\n", file = Tfile)
    readLines(Tfile)
    seek(Tfile, 0, rw = "r") # reset to beginning
    readLines(Tfile)
    cat("ghi\n", file = Tfile)
    readLines(Tfile)
    Tfile # -> print() :  "valid" connection
    close(Tfile)
    Tfile # -> print() :  "invalid" connection
    unlink(Tpath)
    
    ## We can do the same thing with an anonymous fi