Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

How to write to clipboard on Ubuntu/Linux in 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.

question from:https://stackoverflow.com/questions/10959521/how-to-write-to-clipboard-on-ubuntu-linux-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure if this is the best way, but here's how I could get it to work:

  1. Install xclip: sudo apt-get install xclip
  2. Read the manual: man xclip
  3. Write to X11 primary in R: write.table(1:10, pipe("xclip -i", "w"))

Update:

Note that the object passed to write.table will not be present in the clipboard until the pipe is closed. You can force the pipe to close by calling gc(). For example:

write.table(1:10, pipe("xclip -i", "w"))  # data may not be in clipboard
gc()                                      # data written to primary clipboard

A better way to manage the connection is to use a function with on.exit(close(con)), which will close the pipe even if the write.table call throws an error. Note that you need to ensure you're writing to the clipboard you intend to use (primary is the default), based on your system setup.

write.xclip <- function(x, selection=c("primary", "secondary", "clipboard"), ...) {
  if (!isTRUE(file.exists(Sys.which("xclip")[1L])))
    stop("Cannot find xclip")
  selection <- match.arg(selection)[1L]
  con <- pipe(paste0("xclip -i -selection ", selection), "w")
  on.exit(close(con))
  write.table(x, con, ...)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...