Reputation: 34957
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.
Upvotes: 45
Views: 12659
Reputation: 1466
the clipr package makes this really easy
x <- 1:10
clipr::write_clip(x)
Upvotes: 28
Reputation: 6679
Versions:
I could not get the other solutions to work, so I man
ed up. This approach works for me (based on others' solutions).
write_clipboard = function(x, .rownames = F) {
#decide how to write
#windows is easy!
if (Sys.info()['sysname'] %in% c("Windows")) {
#just write as normal
write.table(x, "clipboard", sep = "\t", na = "", row.names = F)
} else {
#for non-windows, try xclip approach
#https://stackoverflow.com/a/10960498/3980197
write.xclip = function(x) {
#if xclip not installed
if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) {
stop("Cannot find xclip")
}
con <- pipe("xclip -selection c", "w")
on.exit(close(con))
write.table(x, con, sep = "\t", na = "", row.names = F)
}
tryCatch({
write.xclip(x)
}, error = function(e) {
message("Could not write using xclip")
})
}
}
It's a watered down version of a function in my personal R package.
Reading is equally difficult. Here's a companion function for the above.
read_clipboard = function(header = T,
sep = "\t",
na.strings = c("", "NA"),
check.names = T,
stringsAsFactors = F,
dec = ".",
...) {
#decide how to read
#windows is easy!
if (Sys.info()['sysname'] %in% c("Windows")) {
#just read as normal
read.table(file = con, sep = sep, header = header, check.names = check.names, na.strings = na.strings, stringsAsFactors = stringsAsFactors, dec = dec, ...)
} else {
#for non-windows, try xclip approach
#https://stackoverflow.com/a/10960498/3980197
read.xclip = function(x) {
#if xclip not installed
if (!isTRUE(file.exists(Sys.which("xclip")[1L]))) {
stop("Cannot find xclip")
}
con <- pipe("xclip -o -selection c", "r")
on.exit(close(con))
read.table(file = con, sep = sep, header = header, check.names = check.names, na.strings = na.strings, stringsAsFactors = stringsAsFactors, dec = dec, ...)
}
tryCatch({
read.xclip(x)
}, error = function(e) {
message(sprintf("error: %s", e$message))
})
}
}
Upvotes: 3
Reputation: 176718
Not sure if this is the best way, but here's how I could get it to work:
sudo apt-get install xclip
man xclip
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, ...)
}
Upvotes: 19
Reputation: 261
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.
Upvotes: 26