R - Connection (Stream)

> Procedural Languages > R

1 - About

Data are read in using connection interfaces:

  • file,
  • gzfile, (a file compressed with gzip)
  • bzfile, (a file compressed with bzip2)
  • url (an HTTP location)

2 - showConnections

showConnections(all = TRUE)
  description class            mode text   isopen   can read can write
0 "stdin"     "terminal"       "r"  "text" "opened" "yes"    "no"     
1 "stdout"    "terminal"       "w"  "text" "opened" "no"     "yes"    
2 "stderr"    "terminal"       "w"  "text" "opened" "no"     "yes"    
3 "letters"   "textConnection" "r"  "text" "opened" "yes"    "no"     


  • stdin, stdout and stderr are the standard streams
  • class can be terminal or textConnection

3 - getConnection



4 - closeAllConnections

5 - stdin(), stdout() and stderr()

stdin(), stdout() and stderr() return connection objects.

stdin() refers to the “console” and not to the C-level “stdin” of the process. To access to the C-level file stream “stdin”, use file("stdin").

6 - How to

6.1 - know if the connection is connected to a terminal

The isatty function returns true if the connection is one of the class “terminal” connections and it is apparently connected to a terminal, otherwise false. This may not be reliable in embedded applications, including GUI consoles.

> c(isatty(stdin()), isatty(stdout()), isatty(stderr()))