How do I copy and paste data into R from the clipboard?
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
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.
Comments
-
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 over 11 years
read.table(text = readClipboard(), sep = " ")
may work even better. -
GSee over 11 yearsThis is similar: stackoverflow.com/a/10004019. Using that code, you can do
data=as.numeric(qw('125 140 200 200 190 '))
-
isomorphismes over 10 yearsIn Ubuntu you would replace
pbpaste
withxsel -bo
or possiblyxclip
in another *nix. -
Blaszard about 8 yearsI wish this were an accepted answer... (well, I use OS X).
-
Fábio about 7 yearsIt seems nice, but it doesn't work on my Ubuntu 16.04 and R version 3.4.0
-
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 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 likexsel
. -
isomorphismes almost 7 years@Reilstein looks like in
?pipe
, you can also usefile(description='clipboard')
-
Devon about 5 yearsMacOSX pbpaste seems to work for text but not images.
-
Francisco about 4 yearsI does only works for text, I even struggle changing it to numbers with as.numeric.
-
tim about 4 yearsyou're using the windows command on a mac. Use pipe(“pbpaste”) in place of "clipboard"
-
iamericfletcher over 3 yearsMac users, please follow these instructions. They work for me using read.table(pipe("pbpaste"), header=TRUE)
-
den2042 over 2 yearsSAS-approach-like reproducible cross-platform solution. Thank you!