Not sure if this is the best way, but here's how I could get it to work:
- Install xclip:
sudo apt-get install xclip
- Read the manual:
man xclip
- 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, ...)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…