如何写在R ubuntu/Linux上的剪贴板?

我正在运行Ubuntu 11.10,我想要能够写入剪贴板(或主要选择)。以下给出错误
> 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 does not have a clipboard but an X11 session has primary and
secondary selections. ?file says

Clipboard:

06001

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.

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

>安装xclip:sudo apt-get install xclip
>阅读手册:man xclip
>写入X中的X11初级: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,...)
}

相关文章

目录前言一、创建Hadoop用户二、更新apt和安装Vim编辑器三、...
原文连接:https://www.cnblogs.com/yasmi/p/5192694.html ...
电脑重启后,打开VirtualBox,发现一直用的虚拟机莫名的消失...
参见:https://blog.csdn.net/weixin_38883338/article/deta...
Ubuntu 18.04 LTS 已切换到 Netplan 来配置网络接口。Netpla...
介绍每个 Web 服务都可以通过特定的 URL 在 Internet 上访问...