How do I copy and paste data into R from the clipboard?
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?
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:
I needed to copy a composite url into the Windows clipboard, while read.table() outputted a character vector with quotation marks around my URL.
Instead, I used writeClipboard(URL,format=1) from package utils, and it did the trick.
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
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.
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
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):