# 9 Connections

In R, every time you read data in or write data out, you are using a connection behind the scenes. Connections abstract away the underlying implementation so that you can read and write data the same way, regardless of whether you’re writing to a file, an HTTP connnection, a pipe, or something more exotic.

## 9.1 Basics

• default connections: stdin, stderr, stdout
• cat() + cat_line()
• survey of base connections: file, compressed file, url, pipe, socket, text
• important packages: curl
• blocking vs non-blocking
• pattern: close() with on.exit() if you opened

## 9.2 Reading and writing binary data

• raw()
• readBin() vs writeBin()
• text vs binary (newlines and nulls)

## 9.3 Reading and writing text data

Reading and writing text is more complicated than reading and writing binary data because as soon as you move beyond regular ASCII characters (e.g. a-z, 0-9) there are many different ways of representing the same text. The way in which text data is stored in binary is known as the encoding.