2012-06-09 30 views
24

我正在运行Ubuntu 11.10,我希望能够写入剪贴板(或主要选择)。下面给出了一个错误如何在R/Ubuntu上写入到Ubuntu/Linux上的剪贴板?

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

我怎么能写到剪贴板/主选择?

请注意,我看过this old R-Help post,但我仍然不清楚我应该做什么。

Linux没有剪贴板,但X11会话具有主要和 次要选择。 ?文件说

剪贴板:

'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")'. 

所以RTFM应用。写入X11选择需要多个线程 ,我并不认为它值得执行(与Windows不同)的非常大的努力。

请注意,窗口管理器可能有其他剪贴板,例如 RGtk2软件包具有接口到gtk剪贴板。

回答

12

不知道这是最好方式,但这里是我怎么能得到它的工作:

  1. 安装XCLIP:sudo apt-get install xclip
  2. 阅读手册:man xclip
  3. 写入X11初级R:write.table(1:10, pipe("xclip -i", "w"))

更新:

注意,直到管道关闭传递给write.table对象将不会出现在剪贴板中。您可以通过致电gc()来强制关闭管道。例如:

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

一种更好的方式来管理连接是使用功能与on.exit(close(con)),即使write.table调用抛出一个错误,将关闭该管道。请注意,根据您的系统设置,您需要确保您正在写入您打算使用的剪贴板(主要是默认设置)。

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, ...) 
} 
+0

+1谢谢。我很感激。我仍然很想看看还有其他什么策略。我偶尔喜欢粘贴到剪贴板的主要原因是保存几秒钟,而不是写入文件并从文件中复制。上述策略似乎假设我可以提前预测我需要剪贴板。我也无法在R Studio中使用它。我只能让它和控制台一起工作。 –

+0

@JeromyAnglim我也注意到这是一个RStudio问题,所以你最好向RStudio开发者报告。我不知道他们用剪贴板做了什么。 –

+1

这对我来说并不适用于ubuntu,无论是在R Studio还是在R的终端版本中。我承认我没有完全阅读xclip的人(第2步),但我不认为应该会影响结果。 – geneorama

16
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) 

您可以创建功能,例如后粘贴回所写的任何东西到剪贴板。默认返回制表符分隔的列,但没有列名。指定其他分隔符,包含行名称,或按照您的喜好排除列名称,如图所示。

编辑:为了澄清,你仍然需要安装xclip。不过,你不需要首先单独启动它。

+0

这些解决方案都不适用于我:-( –

0

版本:

  • 薄荷18.1,肉桂
  • XCLIP 0.12
  • [R 3.4.0(2017年4月21日)

我不能让其他的解决方案工作,所以我man编辑。这种方法适用于我(基于他人的解决方案)。

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") 
     }) 
    } 
} 

这是在my personal R package中减弱功能的版本。

从剪贴板中读取

读书同样困难。这是上述的伴侣功能。

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)) 
    }) 
    } 
} 
相关问题