connection               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, close and position connections.

_U_s_a_g_e:

     file(description, open = "", blocking = TRUE)
     pipe(description, open = "")

     open(con, open = "rt", blocking = TRUE)
     close(con, type = "rw")
     seek(con, where = NA, rw = "")

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

_A_r_g_u_m_e_n_t_s:

description: character. A description of the connection. For `file'
          this is a path to the file to be opened. For a
          `textConnection' it is an R character vector object. 

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

blocking: logical. Currently ignored.

    type: character. Currently ignored.

   where: integer.  A file position, or `NA'.

      rw: character.  Currently ignored.

_D_e_t_a_i_l_s:

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

     `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.

     `seek' with `where = NA' returns the current byte offset of a
     connection (from the beginning), and with a positive `where'
     argument the connection is re-positioned (if possible) to the
     specified position. `isSeekable' returns whether the connection in
     principle supports `seek': currently only file connections do.

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

     `"_r"' or `"rt"'. Open for reading in text mode.

     `"_w"' or `"wt"'. Open for writing in text mode.

     `"_a"' or `"at"'. 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.

     These are likely to change.  Some connections (e.g. text
     connections) can only be opened for reading or writing.  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.

_V_a_l_u_e:

     `file' and `pipe' return a connection object which inherits from
     class `"connection"'.

     `seek' returns the current position, as a byte offset, if
     relevant.

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

     `isIncomplete' returns a logical value, whether last read attempt
     was blocked (currently always false), ot for an output text
     connection whether there is unflushed output.

_N_o_t_e:

     R's connections are modelled on those in S version 4 (see
     Chambers, 1998), but the implementation is currently incomplete. 
     In particular:

        *  Pipes are only implemented on Unix and `Rterm' on Windows.
           Fifos are not yet implemented. We also envisage having
           connections to sockets.

        *  Svr4 has separate read and write positions for files: R does
           not.

        *  Svr4 allow an empty description to `file' to indicate an
           anonymous (temporary) file.

_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', `readLines', `showConnections', `pushBack'

_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")

     ## Unix examples of use of pipes

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

     # remove trailing commas. Suppose
     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)

