Using xclip
You just have to reverse some of the options and functions.
The options for the xclip command need to be changed to output and the function write.table needs to be changed into read.table.
For instance:
read.table(pipe("xclip -selection clipboard -o",open="r"))
Using file()
You can use the solution provided by Anando but in the present description of that solution some details were left out.
The command read.table("clipboard")
is effectively using the command .Internal(file(description, open, blocking, encoding, method, raw)
which splits up in several options
- "X11_primary" (selected text)
- "X11_secondary" (some auxiliary copy field only used by some programs)
- "X11_clipboard" (copied text)
The case of Ubuntu 16.04 and maybe more general Linu:x
I could not track it down easily in the source code but based on the behaviour it seems like the "clipboard" option defaults to "X11_primary" (at least it has the same behavior in Ubuntu 16.04).
If you use read.delim("X11_clipboard")
in place of read.delim("clipboard")
then you get the copied text instead of the selected text.
Note, that you may get an error when using X11_clipboard such as:
> read.table("X11_clipboard")
Error in file(file, "rt") :
X11 clipboard selection is not supported on this system
In that case you must install the Xmu header files on your system (that is the operating system, e.g. Ubuntu). I had this error in my case and resolved it by using
sudo apt-get install libxmu-dev
sudo apt-get install xorg-dev
I do not know which of the two solved it. But after this, when I recompiled the R-base from the source code, then the read.table("X11_clipboard")
worked. (I could not get it working by installing from the Ubuntu repository)