91

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?

1

13 Answers 13

94

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)
71

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")
6
  • 1
    In Ubuntu you would replace pbpaste with xsel -bo or possibly xclip in another *nix. Oct 23, 2013 at 23:14
  • @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?
    – Reilstein
    May 18, 2017 at 3:59
  • @Reilstein as far as I know, xsel happens at the X level which is "lower" than libreoffice. ?system can invoke bash commands like xsel. May 25, 2017 at 9:22
  • @Reilstein looks like in ?pipe, you can also use file(description='clipboard') May 25, 2017 at 9:26
  • MacOSX pbpaste seems to work for text but not images.
    – Devon
    Apr 11, 2019 at 21:07
17

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

1
  • 1
    It seems nice, but it doesn't work on my Ubuntu 16.04 and R version 3.4.0
    – Fábio
    May 5, 2017 at 12:59
13

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)
8

A method that I've tested and works on both Windows and MacOS is to use textConnection() with read.table().

First, paste your data into a variable as text:

density_water_str <-   "T_/K Density_g/mL D2O
273 0.999841 1.10469
274 0.999900 NA
275 0.999941 NA
276 0.999965 NA
277 0.999973 1.1057
278 0.999965 1.10562
279 0.999941 NA
280 0.999902 NA
281 0.999849 NA
282 0.999781 NA
281 0.999700 NA"

Then, read the text string using read.table()

density_water <- read.table(textConnection(
                                object = density_water_str), 
                            header = TRUE, 
                            sep = "", 
                            stringsAsFactors = FALSE)

Not tested on Linux or other Unix systems, but I believe it should work cross-platform.

1
  • SAS-approach-like reproducible cross-platform solution. Thank you!
    – den2042
    Nov 3, 2021 at 13:35
6

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

2
  • 6
    read.table(text = readClipboard(), sep = " ") may work even better. Nov 18, 2012 at 1:16
  • This is similar: stackoverflow.com/a/10004019. Using that code, you can do data=as.numeric(qw('125 140 200 200 190 '))
    – GSee
    Nov 18, 2012 at 14:20
3

Another way is by using the clipr package. I leave you the link where I get the example.

library("clipr")
my_data <- read_clip_tbl()
my_data
1

To complement the "wjchulme" answer using "datapasta", if you want to set/interpret the output from datapasta by R in order to set a variable with the clipboard content, one coud do something like:

read.from.clipboard <- function() {
  mydata<-eval(parse(text=paste(capture.output(datapasta::tribble_paste(output_context = datapasta::console_context()), file=NULL), collapse="")))
  str(mydata); View(capture.output(str(mydata), file=NULL)) #Check guessed format is ok
  return(mydata)
}
mydata<-read.from.clipboard() #Tibble format
mydata<-as.data.frame(read.from.clipboard()) #Data.frame format

1

I am using Mojave 10.14

With the input of the command: X<-read.delim("clipboard") I encountered the following warning in Rstudio version Version 1.1.463 for Mac:

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In 
file(file, "rt") : cannot open file 'pbpaste': No such file or directory 

Searching through Google for solution, I have tried and tested countless of solutions, packages and commands for this to run, and with days and nights of trial, finally now it's working.

I hope no one has to go through so much of pain again with this problem, therefore I am sharing this information.

Kindly follow all of the following, as I am unsure as to which particular installation did the magic (the downloads do not need to be in this particular order):

  1. Download R-3.6.1.pkg from https://cran.r-project.org/bin/macosx/

  2. Download Rstudio from https://www.rstudio.com/products/rstudio/download/#download

  3. Install the rcmdr package in Rstudio

    Go to "tools" > "Install Packages" > type rcmdr

  4. Download XQuartz 'X11' from https://www.xquartz.org/

  5. Download all the packages

    Go to https://cran.r-project.org/bin/macosx/tools/, then download the following tools:

    • clang-8.0.0.pkg (OS X 10.11+, signed, 64-bit)
    • gfortran-4.2.3.pkg (OS X 10.5+, signed, 64-bit driver)
    • tcltk-8.5.5-x11.pkg (OS X 10.5+, signed)
  6. Now go back to Rstudio and type:

    X<-read.delim("clipboard") 
    X 
    

    The copied data in excel will be now imported in Rstudio console.

Hope this information will prove to be helpful.

2
  • 1
    you're using the windows command on a mac. Use pipe(“pbpaste”) in place of "clipboard"
    – tim
    May 5, 2020 at 10:12
  • Mac users, please follow these instructions. They work for me using read.table(pipe("pbpaste"), header=TRUE)
    – Eric
    Aug 10, 2020 at 19:22
0

I needed to copy a composite into the Windows , while read.table() outputted a character vector with quotation marks around my URL. Instead, I used writeClipboard(URL,format=1) from package , and it did the trick.

0

Look at the documentation for ?file, section Clipboard:

Clipboard file can be used with description = "clipboard" in mode "r" only. This reads the X11 primary selection (see http://standards.freedesktop.org/clipboards-spec/clipboards-latest.txt), which can also be specified as "X11_primary" and the secondary selection as "X11_secondary". On most systems the clipboard selection (that used by ‘Copy’ from an ‘Edit’ menu) can be specified as "X11_clipboard". When a clipboard is opened for reading, the contents are immediately copied to internal storage in the connection. Unix users wishing to write to one of the X11 selections may be able to do so via xclip (http://sourceforge.net/projects/xclip/) or xsel (http://www.vergenet.net/~conrad/software/xsel/), for example by pipe("xclip -i", "w") for the primary selection. macOS users can use pipe("pbpaste") and pipe("pbcopy", "w") to read from and write to that system's clipboard.

so, eg with magrittr:

base::file(description='clipboard') %>% readLines
0

On macOS, none of the other solutions would work for me.

What worked was:

system("pbpaste")

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.