Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to clipboard on Ubuntu/Linux in R?

Tags:

r

I'm running Ubuntu 11.10 and I would like to be able to write to the clipboard (or primary selection). The following gives an error

> x <- 1:10 > dput(x, 'clipboard') Error in file(file, "wt") : 'mode' for the clipboard must be 'r' on Unix 

How can I write to the clipboard/primary selection?

Note that I have seen this old R-Help post, but I'm still not clear what I should be doing.

Linux does not have a clipboard but an X11 session has primary and secondary selections. ?file says

Clipboard:

  'file' can also be used with 'description = "clipboard"' in mode   '"r"' only.  It reads the X11 primary selection, which can also be   specified as '"X11_primary"' and the secondary selection as   '"X11_secondary"'.    When the clipboard is opened for reading, the contents are   immediately copied to internal storage in the connection.    Unix users wishing to _write_ to the primary selection may be able   to do so via 'xclip' (<URL:   http://people.debian.org/~kims/xclip/>), for example by   'pipe("xclip -i", "w")'. 

so RTFM applied. Writing to an X11 selection needs multiple threads and I did not think it worth the very considerable effort of implementing (unlike for Windows).

Note that window managers may have other clipboards, and for example the RGtk2 package has interfaces to gtk clipboards.

like image 211
Jeromy Anglim Avatar asked Jun 09 '12 08:06

Jeromy Anglim


People also ask

Where is clipboard in Linux?

The system-wide clipboard (or system clipboard) is accessible across all applications. In particular, the X Window System, via its X Server component, provides the system clipboard in Linux.

How does clipboard work Linux?

The clipboard provides an application programming interface by which programs can specify cut, copy and paste operations. It is left to the program to define methods for the user to command these operations, which may include keybindings and menu selections.

Does Ubuntu have a clipboard?

For those doing copy & paste actions frequently in Ubuntu Linux, GPaste remembers the clipboard history and allows to find them easily via few click. The clipboard is the place to store the text, image, file path and all the stuff that you last copied (or cut).


2 Answers

clipboard <- function(x, sep="\t", row.names=FALSE, col.names=TRUE){      con <- pipe("xclip -selection clipboard -i", open="w")      write.table(x, con, sep=sep, row.names=row.names, col.names=col.names)      close(con) }  vec <- c(1,2,3,4)  clipboard(vec) clipboard(vec, ",", col.names=FALSE) clipboard(vec, " ", row.names=TRUE) 

You can paste back anything you write to the clipboard after creating the function as such. Default returns tab separated values with column but no row names. Specify other separators, include row names, or exclude column names to your liking as shown.

Edit: To clarify, you still need to install xclip. You don't need to start it separately first, though.

like image 160
Freecube Avatar answered Sep 21 '22 17:09

Freecube


the clipr package makes this really easy

x <- 1:10 clipr::write_clip(x) 
like image 28
Thomas Luechtefeld Avatar answered Sep 24 '22 17:09

Thomas Luechtefeld