我正在运行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 saysClipboard:
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.
不知道这是否是最好的方法,但是我可以这样做:
原文链接:https://www.f2er.com/ubuntu/349775.html>安装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,...) }