\HeaderA{connections}{Functions to Manipulate Connections}{connections}
\aliasA{bzfile}{connections}{bzfile}
\aliasA{clipboard}{connections}{clipboard}
\aliasA{close}{connections}{close}
\methaliasA{close.connection}{connections}{close.connection}
\aliasA{connection}{connections}{connection}
\aliasA{fifo}{connections}{fifo}
\aliasA{file}{connections}{file}
\aliasA{flush}{connections}{flush}
\methaliasA{flush.connection}{connections}{flush.connection}
\aliasA{gzfile}{connections}{gzfile}
\aliasA{isIncomplete}{connections}{isIncomplete}
\aliasA{isOpen}{connections}{isOpen}
\aliasA{open}{connections}{open}
\methaliasA{open.connection}{connections}{open.connection}
\aliasA{pipe}{connections}{pipe}
\aliasA{print.connection}{connections}{print.connection}
\aliasA{socketConnection}{connections}{socketConnection}
\aliasA{summary.connection}{connections}{summary.connection}
\aliasA{unz}{connections}{unz}
\aliasA{url}{connections}{url}
\keyword{file}{connections}
\keyword{connection}{connections}
\begin{Description}\relax
Functions to create, open and close connections.
\end{Description}
\begin{Usage}
\begin{verbatim}
file(description = "", open = "", blocking = TRUE,
     encoding = getOption("encoding"))

url(description, open = "", blocking = TRUE,
    encoding = getOption("encoding"))

gzfile(description, open = "", encoding = getOption("encoding"),
       compression = 6)

bzfile(description, open = "", encoding = getOption("encoding"))

unz(description, filename, open = "",
    encoding = getOption("encoding"))

pipe(description, open = "", encoding = getOption("encoding"))

fifo(description, open = "", blocking = FALSE,
     encoding = getOption("encoding"))

socketConnection(host = "localhost", port, server = FALSE,
                 blocking = FALSE, open = "a+",
                 encoding = getOption("encoding"))

open(con, ...)
## S3 method for class 'connection':
open(con, open = "r", blocking = TRUE, ...)

close(con, ...)
## S3 method for class 'connection':
close(con, type = "rw", ...)

flush(con)

isOpen(con, rw = "")
isIncomplete(con)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{description}] character string. A description of the connection:
see \sQuote{Details}.
\item[\code{open}] character.  A description of how to open the connection
(if at all).  See \sQuote{Details} for possible values.
\item[\code{blocking}] logical.  See the \sQuote{Blocking} section below.
\item[\code{encoding}] The name of the encoding to be used.  See the
\sQuote{Encoding} section below.
\item[\code{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.
\item[\code{filename}] a filename within a zip file.
\item[\code{host}] character.  Host name for port.
\item[\code{port}] integer.  The TCP port number.
\item[\code{server}] logical.  Should the socket be a client or a server?
\item[\code{con}] a connection.
\item[\code{type}] character. Currently ignored.
\item[\code{rw}] character.  Empty or \code{"read"} or \code{"write"},
partial matches allowed.
\item[\code{...}] arguments passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The first eight functions create connections.  By default the
connection is not opened (except for \code{socketConnection}), but may
be opened by setting a non-empty value of argument \code{open}.

For \code{file} the description is either a path to the file to be
opened or a complete URL, or \code{""} (the default) or \code{"stdin"}
or \code{"clipboard"} (see below).

For \code{url} the description is a complete URL, including scheme
(such as \code{http://}, \code{ftp://} or \code{file://}).

For \code{gzfile} the description is the path to a file that is
compressed by \code{gzip}: it can also opened uncompressed files.

For \code{bzfile} the description is the path to a file that is
compressed by \code{bzip2}.

\code{unz} reads (only) single files within zip files, in binary mode.
The description is the full path to the zip file, with \file{.zip}
extension if required.

For \code{pipe} the description is the command line to be piped to or
from.

For \code{fifo} the description is the path of the fifo.

\code{file} allows \code{description="stdin"} to refer to the C-level
\code{stdin} of the process (which need not be connected to anything
in a console or embedded version of \R), provided the C99 function
\code{fdopen} is supported on the platform.

\code{gzfile} and \code{bzfile} open the actual file in binary mode
and so no translations are done if the original file was a text file.
(See \code{\LinkA{gzcon}{gzcon}} for a way to add compression to non-file
connections such as URLs.)

All platforms support \code{file}, \code{gzfile}, \code{bzfile},
\code{unz} and \code{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 \code{fifo} on Windows.)

Proxies can be specified for \code{url} connections: see
\code{\LinkA{download.file}{download.file}}.

\code{open}, \code{close} and \code{seek} are generic functions: the
following applies to the methods relevant to connections.

\code{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 \code{open} explicitly.

Possible values for the mode \code{open} to open a connection are
\describe{
\item[\code{"r"} or \code{"rt"}] Open for reading in text mode.
\item[\code{"w"} or \code{"wt"}] Open for writing in text mode.
\item[\code{"a"} or \code{"at"}] Open for appending in text mode.
\item[\code{"rb"}] Open for reading in binary mode.
\item[\code{"wb"}] Open for writing in binary mode.
\item[\code{"ab"}] Open for appending in binary mode.
\item[\code{"r+"}, \code{"r+b"}] Open for reading and writing.
\item[\code{"w+"}, \code{"w+b"}] Open for reading and writing,
truncating file initially.
\item[\code{"a+"}, \code{"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 \code{\LinkA{pushBack}{pushBack}} is
text-oriented and is only allowed on connections open for reading in
text mode.
If a file or fifo is created on a Unix-alike, its permissions will be
the maximal allowed by the current setting of \code{umask} (see
\code{\LinkA{Sys.umask}{Sys.umask}}).

\code{gzfile} connections are an exception, as the file always has to
be opened in binary mode.  Thus modes such as \code{r} are binary, and
\code{rt} is needed to have a text-mode connection.

\code{close} closes and destroys a connection.  Note that this will
happen automatically in due course if there is no \R{} object referring
to the connection.

A maximum of 128 connections can be allocated (not necessarily open)
at any one time.  Three of these are pre-allocated (see
\code{\LinkA{stdout}{stdout}}).   The OS will impose limits on the numbers of
connections of various types, but these are usually larger than 125.

\code{flush} flushes the output stream of a connection open for
write/append (where implemented).

If for a \code{file} or \code{fifo} connection the description is
\code{""}, the file/fifo is immediately opened (in \code{"w+"} mode
unless \code{open="w+b"} is specified) and unlinked from the file
system.  This provides a temporary file/fifo to write to and then read
from.

A note on \code{file://} URLs.  The most general form (from RFC1738) is
\code{file://host/path/to/file}, but \R{} only accepts the form with an
empty \code{host} field referring to the local machine.
This is then \code{file:///path/to/file}, where \code{path/to/file} is
relative to \code{/}.  So although the third slash is strictly part of
the specification not part of the path, this can be regarded as a way
to specify the file \file{/path/to/file}.  It is not possible to
specify a relative path using a file URL.
Also, no attempt is made to decode an encoded URL: call
\code{\LinkA{URLdecode}{URLdecode}} if necessary.

Note that \code{https://} connections are
not supported.
\end{Details}
\begin{Value}
\code{file}, \code{pipe}, \code{fifo}, \code{url}, \code{gzfile},
\code{bzfile}, \code{unz} and \code{socketConnection} return a
connection object which inherits from class \code{"connection"} and
has a first more specific class.

\code{isOpen} returns a logical value, whether the connection is
currently open.

\code{isIncomplete} returns a logical value, whether last read attempt
was blocked, or for an output text connection whether there is
unflushed output.
\end{Value}
\begin{Section}{Encoding}
The encoding of the input/output stream of a connection in \emph{text}
mode can be specified by name, in the same way as it would be given to
\code{\LinkA{iconv}{iconv}}: see that help page for how to find out what names are
recognized on your platform.  Additionally, \code{""} and
\code{"native.enc"} both mean the \sQuote{native} encoding, that is
the internal encoding of the current locale and hence no translation
is done.
Not all builds of \R{} support this, and if yours does not, specifying a
non-default encoding will give an error when the connection is opened.

Re-encoding only works for connections in text mode.

The encoding \code{"UCS-2LE"} is treated specially, as it is the
appropriate value for Windows \sQuote{Unicode} text files.  If the
first two bytes are the Byte Order Mark \code{0xFFFE} then these are
removed as most implementations of \code{\LinkA{iconv}{iconv}} do not accept
BOMs.  Note that some implementations
will handle BOMs using encoding \code{"UCS-2"} but many
will not.

Exactly what happens when the requested translation cannot be done is
in general undocumented.  Requesting a conversion that is not supported is
an error, reported when the connection is opened.  On output the
result is likely to be that up to the error, with a warning.  On
input, it will most likely be all or some of the input up to the
error.
\end{Section}
\begin{Section}{Blocking}
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 \code{\LinkA{readLines}{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 \code{options("timeout")}.  Note that this is a timeout
for no response at all, not for the whole operation.  The timeout is
set at the time the connection is opened (more precisely, when the
last connection of that type -- \code{http:}, \code{ftp:} or socket --
was opened).
\end{Section}
\begin{Section}{Fifos}
Fifos default to non-blocking.  That follows S version 4 and is 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 \code{file()}.
\end{Section}
\begin{Section}{Clipboard}
\code{file} can also be used with \code{description = "clipboard"}
in mode \code{"r"} only.  This reads the X11 primary selection (see
\url{http://standards.freedesktop.org/clipboards-spec/clipboards-latest.txt}),
which can also be specified as \code{"X11\_primary"} and the secondary
selection as \code{"X11\_secondary"}.  On most systems the clipboard
selection (that used by \sQuote{Copy} from an \sQuote{Edit} menu) can
be specified as \code{"X11\_clipboard"}.

When a clipboard is opened for reading, the contents are immediately
copied to internal storage in the connection.

Unix users wishing to \emph{write} to one of the selections may be
able to do so via \code{xclip}
(\url{http://people.debian.org/~kims/xclip/}), for example by
\code{pipe("xclip -i", "w")} for the primary selection.

MacOS X users can use \code{pipe("pbpaste")} and
\code{pipe("pbcopy", "w")} to read from and write to that system's
clipboard.
\end{Section}
\begin{Note}\relax
\R's connections are modelled on those in S version 4 (see Chambers,
1998).  However \R{} goes well beyond the S model, for example in
output text connections and URL, \code{gzfile}, \code{bzfile} and socket
connections.

The default mode in \R{} is \code{"r"} except for socket connections.
This differs from S, where it is the equivalent of \code{"r+"},
known as \code{"*"}.

On (rare) platforms where \code{vsnprintf} does not return the needed length
of output there is a 100,000 character output limit on
the length of line for \code{fifo}, \code{gzfile} and \code{bzfile}
connections: longer lines will be truncated with a warning.
\end{Note}
\begin{References}\relax
Chambers, J. M. (1998)
\emph{Programming with Data.  A Guide to the S Language.} Springer.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{textConnection}{textConnection}}, \code{\LinkA{seek}{seek}},
\code{\LinkA{showConnections}{showConnections}}, \code{\LinkA{pushBack}{pushBack}}.

Functions making direct use of connections are \code{\LinkA{readLines}{readLines}},
\code{\LinkA{readBin}{readBin}}, \code{\LinkA{readChar}{readChar}},
\code{\LinkA{writeLines}{writeLines}}, \code{\LinkA{writeBin}{writeBin}},
\code{\LinkA{writeChar}{writeChar}}, \code{\LinkA{cat}{cat}}, \code{\LinkA{sink}{sink}},
\code{\LinkA{scan}{scan}}, \code{\LinkA{parse}{parse}}, \code{\LinkA{read.dcf}{read.dcf}},
\code{\LinkA{load}{load}}, \code{\LinkA{save}{save}}, \code{\LinkA{dput}{dput}} and
\code{\LinkA{dump}{dump}}.

\code{\LinkA{capabilities}{capabilities}} to see if \code{url},
\code{fifo} and \code{socketConnection} are supported by this build of \R.

\code{\LinkA{gzcon}{gzcon}} to wrap gzip (de)compression around a connection.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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(zz <- gzfile("ex.gz"))
close(zz)
unlink("ex.gz")

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(zz <- bzfile("ex.bz2")))
close(zz)
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)

## fifo example -- may fail, e.g. on Cygwin, even with OS support for fifos
if(capabilities("fifo")) {
  zz <- fifo("foo-fifo", "w+")
  writeLines("abc", zz)
  print(readLines(zz))
  close(zz)
  unlink("foo-fifo")
}


## Unix examples of use of pipes

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

# remove trailing commas. Suppose

## Not run: 
% 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
## End(Not run)
# Then read this by
scan(pipe("sed -e s/,$// data2_"), sep=",")


# convert decimal point to comma in output: see also write.table
# both R strings and (probably) the shell need \ doubled
zz <- pipe(paste("sed s/\\\\./,/ >", "outfile"), "w")
cat(format(round(stats::rnorm(48), 4)), fill=70, file = zz)
close(zz)
file.show("outfile", delete.file=TRUE)

## example for a 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)


## Not run: 
## 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)

## examples of use of encodings
cat(x, file = (con <- file("foo", "w", encoding="UTF-8"))); close(con)
# read a 'Windows Unicode' file
A <- read.table(con <- file("students", encoding="UCS-2LE")); close(con)
## End(Not run)\end{ExampleCode}
\end{Examples}

