How do I copy and paste data into R from the clipboard?

62,478

Solution 1

Assuming you have data in the Windows clipboard (for example, copied data from Excel), to put that data into a variable named copdat in R use:

copdat <- read.delim("clipboard")

If you want to copy data from an R variable named rdat into the Windows clipboard (for example, to copy into Excel) use:

write.table(rdat, "clipboard", sep="\t", row.names=FALSE, col.names=FALSE)

Solution 2

The name and exact connection used for the 'clipboard' varies depending on the OS.

for Windows:

x <- read.delim("clipboard")

for Mac OS:

x <- read.delim(pipe("pbpaste"))

This works because read.delim, like many functions, will accept a range of connection types beyond just a file. For Macs we're actually using a pipe. help(connections) is pretty informative.

The psych package has a function read.clipboard() that makes this a little easier by testing for your OS.

As noted by others here, you can also write to the clipboard. There is normally a 32 K limit, which can be raised by using adding a hyphen and number after clipboard as in, for example, passing up to 256 K worth of data from object df with:

write.table(df, "clipboard-256")

Solution 3

There's an R package / RStudio plugin called datapasta that does this very neatly - see https://CRAN.R-project.org/package=datapasta. Image below is a demonstration of its simplicity

enter image description here

Solution 4

If you want to read in tabular data from a spreadsheet, I have used the following code

read.table(file = "clipboard", sep = "\t", header=TRUE)

Solution 5

Type in data = as.numeric(read.table(text = "125 140 200 200 190 ", sep = " ")) where your numbers go in between the text = " " quotation marks.

Share:
62,478
Moderat
Author by

Moderat

Data scientist working in Charlotte, NC

Updated on July 05, 2022

Comments

  • Moderat
    Moderat almost 2 years

    I have my data open in another application (e.g. a spreadsheet, like Excel, or a text editor). If I copy that data to my operating system clipboard, how can I read it into R as a data.frame?

  • David J. Harris
    David J. Harris over 11 years
    read.table(text = readClipboard(), sep = " ") may work even better.
  • GSee
    GSee over 11 years
    This is similar: stackoverflow.com/a/10004019. Using that code, you can do data=as.numeric(qw('125 140 200 200 190 '))
  • isomorphismes
    isomorphismes over 10 years
    In Ubuntu you would replace pbpaste with xsel -bo or possibly xclip in another *nix.
  • Blaszard
    Blaszard about 8 years
    I wish this were an accepted answer... (well, I use OS X).
  • Fábio
    Fábio about 7 years
    It seems nice, but it doesn't work on my Ubuntu 16.04 and R version 3.4.0
  • Reilstein
    Reilstein almost 7 years
    @isomorphismes so the ubuntu command would be read.delim(pipe("xsel -bo")) ? I tried that and it didn't work. Is that command supposed to work for LibreOffice copy/paste?
  • isomorphismes
    isomorphismes almost 7 years
    @Reilstein as far as I know, xsel happens at the X level which is "lower" than libreoffice. ?system can invoke bash commands like xsel.
  • isomorphismes
    isomorphismes almost 7 years
    @Reilstein looks like in ?pipe, you can also use file(description='clipboard')
  • Devon
    Devon about 5 years
    MacOSX pbpaste seems to work for text but not images.
  • Francisco
    Francisco about 4 years
    I does only works for text, I even struggle changing it to numbers with as.numeric.
  • tim
    tim about 4 years
    you're using the windows command on a mac. Use pipe(“pbpaste”) in place of "clipboard"
  • iamericfletcher
    iamericfletcher over 3 years
    Mac users, please follow these instructions. They work for me using read.table(pipe("pbpaste"), header=TRUE)
  • den2042
    den2042 over 2 years
    SAS-approach-like reproducible cross-platform solution. Thank you!