Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中写入Ubuntu/Linux上的剪贴板?_R - Fatal编程技术网

如何在R中写入Ubuntu/Linux上的剪贴板?

如何在R中写入Ubuntu/Linux上的剪贴板?,r,R,我正在运行Ubuntu11.10,我希望能够写入剪贴板(或主选项)。下面给出了一个错误 > x <- 1:10 > dput(x, 'clipboard') Error in file(file, "wt") : 'mode' for the clipboard must be 'r' on Unix >x dput(x,‘剪贴板’) 文件(文件,“wt”)中出错:在Unix上,剪贴板的“模式”必须为“r” 如何写入剪贴板/主选择? 请注意,我已经看到了,但我仍然不清楚我应

我正在运行Ubuntu11.10,我希望能够写入剪贴板(或主选项)。下面给出了一个错误

> x <- 1:10
> dput(x, 'clipboard')
Error in file(file, "wt") : 'mode' for the clipboard must be 'r' on Unix
>x dput(x,‘剪贴板’)
文件(文件,“wt”)中出错:在Unix上,剪贴板的“模式”必须为“r”
如何写入剪贴板/主选择?

请注意,我已经看到了,但我仍然不清楚我应该做什么

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")'.
“文件”也可以在模式中与“description=”clipboard“一起使用
仅限“r”。它读取X11主选择,也可以是
指定为“X11_primary”,辅助选择指定为
““X11_次要””。
打开剪贴板进行读取时,将显示内容
立即复制到连接中的内部存储。
希望写入主选项的Unix用户可以
通过'xclip'()执行此操作,例如
"pipe("xclip-i","w"),。
所以RTFM应用了。写入X11选择需要多个线程 我认为这不值得我们付出很大的努力 实现(与Windows不同)

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

不确定这是否是最好的方法,但我可以这样做:

  • 安装xclip:
    sudo apt get安装xclip
  • 阅读手册:
    manxclip
  • 在R:
    Write.table(1:10,管道(“xclip-i”,“w”))中写入X11主节点。

  • 更新:

    请注意,在管道关闭之前,传递给
    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, ...)
    }
    
    write.xclip
    剪贴板版本:

    • 薄荷18.1,肉桂
    • xclip 0.12
    • R 3.4.0(2017-04-21)
    我找不到其他的解决办法,所以我只好放弃了。这种方法适合我(基于其他人的解决方案)

    write_剪贴板=函数(x,.rownames=F){
    #决定如何写作
    #windows很容易!
    如果(Sys.info()['sysname']%在%c(“Windows”)中){
    #照常写就行了
    write.table(x,“剪贴板”,sep=“\t”,na=“,row.names=F)
    }否则{
    #对于非窗口,请尝试xclip方法
    #https://stackoverflow.com/a/10960498/3980197
    write.xclip=函数(x){
    #如果未安装xclip
    如果(!isTRUE(file.exists)(Sys.which(“xclip”)[1L])){
    停止(“找不到xclip”)
    }
    
    conclipr软件包使这变得非常简单

    x <- 1:10
    clipr::write_clip(x)
    

    x+1谢谢。我很感激。我仍然有兴趣看看还有什么其他策略。我偶尔喜欢粘贴到剪贴板的主要原因是为了节省几秒钟,而不是写入文件并从文件中复制。上述策略似乎假设我可以提前预测我需要剪贴板。我还无法在R Studio中使用它。我只能在控制台中使用它。@JeromyAnglim我还注意到这是一个RStudio问题,所以你最好向RStudio开发人员报告。我不知道他们对剪贴板做了什么。这在ubuntu中对我不起作用,无论是在R Studio中还是在R的终端版本中。我承认我没有完全理解我阅读了xclip的手册(第2步),但我认为这不会影响结果。对我来说也不起作用(Linux Mint 18,基于Ubuntu 16.04)。@KeithHughitt:看起来这是因为在连接关闭之前管道不会被刷新。你可以通过调用
    gc()强制关闭它
    ,但是在.exit上使用函数和
    更健壮。我已经相应地更新了我的答案。这是迄今为止最简单的解决方案。现在我们在讨论!!在我的包中完全使用它(geneorama/geneorama)看看你的,还有,喜欢你有一个.rownames参数,但是把rownames硬编码为false。可能是因为没有人在他们正常的头脑中会想要它们做任何事情。很有趣,以前没有人注意到这个bug。
    
    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")
          })
        }
    }
    
    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))
        })
      }
    }
    
    x <- 1:10
    clipr::write_clip(x)