connections               package:base               R Documentation

_F_u_n_c_t_i_o_n_s _t_o _M_a_n_i_p_u_l_a_t_e _C_o_n_n_e_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Functions to create, open and close connections.

_U_s_a_g_e:

     file(description = "", open = "", blocking = TRUE,
          encoding = getOption("encoding"))
     pipe(description, open = "", encoding = getOption("encoding"))
     fifo(description = "", open = "", blocking = FALSE,
          encoding = getOption("encoding"))
     gzfile(description, open = "", encoding = getOption("encoding"),
            compression = 6)
     unz(description, filename, open = "", encoding = getOption("encoding"))
     bzfile(description, open = "", encoding = getOption("encoding"))
     url(description, open = "", blocking = TRUE,
         encoding = getOption("encoding"))
     socketConnection(host = "localhost", port, server = FALSE,
                      blocking = FALSE, open = "a+",
                      encoding = getOption("encoding"))

     open(con, open = "r", blocking = TRUE, ...)
     close(con, type = "rw", ...)

     isOpen(con, rw = "")
     isIncomplete(con)

_A_r_g_u_m_e_n_t_s:

description: character. A description of the connection. For `file' and
          `pipe' this is a path to the file to be opened. For `url' it
          is a complete URL, including schemes (`http://', `ftp://' or
          `file://').  `file' also accepts complete URLs. 

filename: a filename within a zip file.

     con: a connection.

    host: character.  Host name for port.

    port: integer.  The TCP port number.

  server: logical.  Should the socket be a client or a server?

    open: character.  A description of how to open the connection (if
          at all). See Details for possible values.

blocking: logical.  See `Blocking' section below.

encoding: An integer vector of length 256.

compression: integer in 0-9.  The amount of compression to be applied
          when writing, from none to maximal.  The default is a good
          space/time compromise.

    type: character. Currently ignored.

   where: integer.  A file position (relative to the origin specified
          by `origin'), or `NA'.

      rw: character.  Empty or `"read"' or `"write"', partial matches
          allowed.

     ...: arguments passed to or from other methods.

_D_e_t_a_i_l_s:

     The first eight functions create connections.  By default the
     connection is not opened (except for `socketConnection'), but may
     be opened by setting a non-empty value of argument `open'.

     `gzfile' applies to files compressed by `gzip', and `bzfile' to
     those compressed by `bzip2': such connections can only be binary.

     `unz' reads (only) single files within zip files, in binary mode.
     The description is the full path, with `.zip' extension if
     required.

     All platforms support (gz)file connections and `url("file://")'
     connections.  The other types may be partially implemented or not
     implemented at all.  (They do work on most Unix platforms, and all
     but `fifo' on Windows.)

     Proxies can be specified for `url' connections: see
     `download.file'.

     `open', `close' and `seek' are generic functions: the following
     applies to the methods relevant to connections.

     `open' opens a connection.  In general functions using connections
     will open them if they are not open, but then close them again, so
     to leave a connection open call `open' explicitly.

     `close' closes and destroys a connection.

     Possible values for the mode `open' to open a connection are

     `"_r"' _o_r `"_r_t"' Open for reading in text mode.

     `"_w"' _o_r `"_w_t"' Open for writing in text mode.

     `"_a"' _o_r `"_a_t"' Open for appending in text mode.

     `"_r_b"' Open for reading in binary mode.

     `"_w_b"' Open for writing in binary mode.

     `"_a_b"' Open for appending in binary mode.

     `"_r+"', `"_r+_b"' Open for reading and writing.

     `"_w+"', `"_w+_b"' Open for reading and writing.

     `"_r+"', `"_r+_b"' Open for reading and writing, truncating file
          initially.

     `"_a+"', `"_a+_b"' Open for reading and appending.

     Not all modes are applicable to all connections: for example URLs
     can only be opened for reading.  Only file and socket connections
     can be opened for reading and writing/appending. For many
     connections there is little or no difference between text and
     binary modes, but there is for file-like connections on Windows,
     and `pushBack' is text-oriented and is only allowed on connections
     open for reading in text mode.

     If for a `file' connection the description is `""', the file is
     immediately opened in `"w+"' mode and unlinked from the file
     system.  This provides a temporary file to write to and then read
     from.

     The encoding vector is used to map the input from a file or pipe
     to the platform's native character set.  Supplied examples are
     `native.enc' as well as `MacRoman', `WinAnsi' and `ISOLatin1',
     whose actual encoding is platform-dependent. Missing characters
     are mapped to a space in these encodings.

_V_a_l_u_e:

     `file', `pipe', `fifo', `url', `gzfile' and `socketConnection'
     return a connection object which inherits from class
     `"connection"' and has a first more specific class.

     `isOpen' returns a logical value, whether the connection is
     currently open.

     `isIncomplete' returns a logical value, whether last read attempt
     was blocked, or for an output text connection whether there is
     unflushed output.

_B_l_o_c_k_i_n_g:

     The default condition for all but fifo and socket connections is
     to be in blocking mode.  In that mode, functions do not return to
     the R evaluator until they are complete.   In non-blocking mode,
     operations return as soon as possible, so on input they will
     return with whatever input is available (possibly none) and for
     output they will return whether or not the write succeeded.

     The function `readLines' behaves differently in respect of
     incomplete last lines in the two modes: see its help page.

     Even when a connection is in blocking mode, attempts are made to
     ensure that it does not block the event loop and hence the
     operation of GUI parts of R.  These do not always succeed, and the
     whole process will be blocked during a DNS lookup on Unix, for
     example.

     Most blocking operations on URLs and sockets are subject to the
     timeout set by `options("timeout")'.  Note that this is a timeout
     for no response at all, not for the whole operation.

_F_i_f_o_s:

     Fifos default to non-blocking.  That follows Svr4 and it probably
     most natural, but it does have some implications. In particular,
     opening a non-blocking fifo connection for writing (only) will
     fail unless some other process is reading on the fifo.

     Opening a fifo for both reading and writing (in any mode: one can
     only append to fifos) connects both sides of the fifo to the R
     process, and provides an similar facility to `file()'.

_N_o_t_e:

     R's connections are modelled on those in S version 4 (see
     Chambers, 1998). However R goes well beyond the Svr4 model, for
     example in output text connections and URL, gzfile, bzfile and
     socket connections.

     The default mode in R is `"r"' except for socket connections. This
     differs from Svr4, where it is the equivalent of `"r+"', known as
     `"*"'.

_R_e_f_e_r_e_n_c_e_s:

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

_S_e_e _A_l_s_o:

     `textConnection', `seek', `readLines', `showConnections',
     `pushBack'.

     `capabilities' to see if `gzfile', `url', `fifo' and
     `socketConnection' are supported by this build of R.

_E_x_a_m_p_l_e_s:

     zz <- file("ex.data", "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("ex.data")
     unlink("ex.data")

     zz <- gzfile("ex.gz", "w")  # compressed file
     cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
     close(zz)
     readLines(gzfile("ex.gz"))
     unlink("ex.gz")

     if(capabilities("bzip2")) {
       zz <- bzfile("ex.bz2", "w")  # bzip2-ed file
       cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
       close(zz)
       print(readLines(bzfile("ex.bz2")))
       unlink("ex.bz2")
     }

     ## An example of a file open for reading and writing
     Tfile <- file("test1", "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)
     close(Tfile)
     unlink("test1")

     ## We can do the same thing with an anonymous file.
     Tfile <- file()
     cat("abc\ndef\n", file=Tfile)
     readLines(Tfile)
     close(Tfile)

     if(capabilities("fifo")) {
       zz <- fifo("foo", "w+")
       writeLines("abc", zz)
       print(readLines(zz))
       close(zz)
       unlink("foo")
     }

     ## Unix examples of use of pipes

     # read listing of current directory
     readLines(pipe("ls -1"))

     # remove trailing commas. Suppose
     % cat data2
     450, 390, 467, 654,  30, 542, 334, 432, 421,
     357, 497, 493, 550, 549, 467, 575, 578, 342,
     446, 547, 534, 495, 979, 479
     # Then read this by
     scan(pipe("sed -e s/,$// data2"), sep=",")

     # convert decimal point to comma in output
     zz <- pipe(paste("sed s/\\./,/ >", "outfile"), "w")
     cat(format(round(rnorm(100), 4)), sep = "\n", file = zz)
     close(zz)
     file.show("outfile", delete.file=TRUE)

     ## example for Unix machine running a finger daemon

     con <- socketConnection(port = 79, blocking = TRUE)
     writeLines(paste(system("whoami", intern=TRUE), "\r", sep=""), con)
     gsub(" *$", "", readLines(con))
     close(con)

     ## two R processes communicating via non-blocking sockets
     # R process 1
     con1 <- socketConnection(port = 6011, server=TRUE)
     writeLines(LETTERS, con1)
     close(con1)

     # R process 2
     con2 <- socketConnection(Sys.info()["nodename"], port = 6011)
     # as non-blocking, may need to loop for input
     readLines(con2)
     while(isIncomplete(con2)) {Sys.sleep(1); readLines(con2)}
     close(con2)

