Loading and storing data with the keyboard and clipboard

Problem

You want to enter data using input from the keyboard (not a file).

Solution

Data input

Suppose this is your data:

  1. size weight cost
  2. small 5 6
  3. medium 8 10
  4. large 11 9

Loading data from keyboard input or clipboard

One way enter from the keyboard is to read from standard input (stdin()).

  1. # Cutting and pasting using read.table and stdin()
  2. data <- read.table(stdin(), header=TRUE)
  3. # You will be prompted for input; copy and paste text here
  4. # Or:
  5. # data <- read.csv(stdin())

You can also load directly from the clipboard:

  1. # First copy the data to the clipboard
  2. data <- read.table('clipboard', header=TRUE)
  3. # Or:
  4. # data <- read.csv('clipboard')

Loading data in a script

The previous method can’t be used to load data in a script file because the input must be typed (or pasted) after running the command.

  1. # Using read.table()
  2. data <- read.table(header=TRUE, text='
  3. size weight cost
  4. small 5 6
  5. medium 8 10
  6. large 11 9
  7. ')

For different data formats (e.g., comma-delimited, no headers, etc.), options to read.table() can be set. See ../Loading data from a file for more information.

Data output

By default, R prints row names. If you want to print the table in a format that can be copied and pasted, it may be useful to suppress them.

  1. print(data, row.names=FALSE)
  2. #> size weight cost
  3. #> small 5 6
  4. #> medium 8 10
  5. #> large 11 9

Writing data for copying and pasting, or to the clipboard

It is possible to write delimited data to terminal (stdout()), so that it can be copied and pasted elsewhere. Or it can be written directly to the clipboard.

  1. write.csv(data, stdout(), row.names=FALSE)
  2. # "size","weight","cost"
  3. # "small",5,6
  4. # "medium",8,10
  5. # "large",11,9
  6. # Write to the clipboard (does not work on Mac or Unix)
  7. write.csv(data, 'clipboard', row.names=FALSE)

Output for loading in R

If the data has already been loaded into R, the data structure can be saved using dput(). The output from dput() is a command which will recreate the data structure. The advantage of this method is that it will keep any modifications to data types; for example, if one column consists of numbers and you have converted it to a factor, this method will preserve that type, whereas simply loading the text table (as shown above) will treat it as numeric.

  1. # Suppose you have already loaded data
  2. dput(data)
  3. #> structure(list(size = structure(c(3L, 2L, 1L), .Label = c("large",
  4. #> "medium", "small"), class = "factor"), weight = c(5L, 8L, 11L
  5. #> ), cost = c(6L, 10L, 9L)), .Names = c("size", "weight", "cost"
  6. #> ), class = "data.frame", row.names = c(NA, -3L))
  7. # Later, we can use the output from dput to recreate the data structure
  8. newdata <- structure(list(size = structure(c(3L, 2L, 1L), .Label = c("large",
  9. "medium", "small"), class = "factor"), weight = c(5L, 8L, 11L
  10. ), cost = c(6L, 10L, 9L)), .Names = c("size", "weight", "cost"
  11. ), class = "data.frame", row.names = c(NA, -3L))