textConnection             package:base             R Documentation

_T_e_x_t _C_o_n_n_e_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Input and output text connections.

_U_s_a_g_e:

     textConnection(object, open = "r")

_A_r_g_u_m_e_n_t_s:

  object: character.  A description of the connection.  For an input is
          an R character vector object, and for an output connection
          the name for the R character vector to receive the output. 

    open: character.  Either `"r"' (or equivalently `""') for an input
          connection or `"w"' or `"a"'for an output connection.

_D_e_t_a_i_l_s:

     An input text connection is opened and the character vector is
     copied at time the connection object is created, and `close'
     destroys the copy.

     An output text connection is opened and creates an R character
     vector of the given name in the user's workspace.  This object
     will at all times hold the completed lines of output to the
     connection, and `isIncomplete' will indicate if there is an
     incomplete final line.  Closing the connection will output the
     final line, complete or not.

     Opening a text connection with `mode = "a"' will attempt to append
     to an existing character vector with the given name in the user's
     workspace.  If none is found (even if an object exists of the
     right name but the wrong type) a new character vector wil be
     created, with a warning.

     You cannot `seek' on a text connection, and `seek' will always
     return zero as the position.

_V_a_l_u_e:

     A connection object of class `"textConnection"' which inherits
     from class `"connection"'.

_S_e_e _A_l_s_o:

     `connections', `showConnections', `pushBack'

_E_x_a_m_p_l_e_s:

     zz <- textConnection(LETTERS)
     readLines(zz, 2)
     scan(zz, "", 4)
     pushBack(c("aa", "bb"), zz)
     scan(zz, "", 4)
     close(zz)

     zz <- textConnection("foo", "w")
     writeLines(c("testit1", "testit2"), zz)
     cat("testit3 ", file=zz)
     isIncomplete(zz)
     cat("testit4\n", file=zz)
     isIncomplete(zz)
     close(zz)
     foo

     # capture R output: use part of example from help(lm)
     zz <- textConnection("foo", "w")
     ctl <- c(4.17, 5.58, 5.18, 6.11, 4.5, 4.61, 5.17, 4.53, 5.33, 5.14)
     trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69)
     group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
     weight <- c(ctl, trt)
     sink(zz)
     anova(lm.D9 <- lm(weight ~ group))
     cat("\nSummary of Residuals:\n\n")
     summary(resid(lm.D9))
     sink()
     close(zz)
     cat(foo, sep = "\n")

