从Ubuntu Linux中的剪贴板复制R [英] R Copy from Clipboard in Ubuntu Linux

查看:154
本文介绍了从Ubuntu Linux中的剪贴板复制R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Ubuntu Linux剪贴板复制到R Studio.我的工作流程包括在R Studio和LibreOffice Calc之间来回移动.我已经找到了以下用于写入Linux X11剪贴板的代码,但是我不知道如何读取它.

I want to copy from the Ubuntu Linux clipboard into R Studio. My workflow consists of moving back and forth between R Studio and LibreOffice Calc. I've found the following code for writing to a Linux X11 clipboard, but I don't know how to read from it.

写入X11 Linux剪贴板:

Write to X11 Linux clipboard:

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

# Examples
vec <- c(1,2,3,4)

clipboard(vec)
clipboard(vec, ",", col.names=FALSE)
clipboard(vec, " ", row.names=TRUE)

如果我在LibreOffice Calc中突出显示一个选择,我想将其直接粘贴到R Studio中.我如何完成这项任务?我已经在Ubuntu中安装了xclip.

If I highlight a selection in LibreOffice Calc I'd like to paste it directly into R Studio. How do I accomplish this task? I have installed xclip in Ubuntu.

sudo apt-get install xclip

推荐答案

使用xclip

您只需要撤销某些选项和功能即可.

Using xclip

You just have to reverse some of the options and functions.

需要将xclip命令的选项更改为输出,并将函数write.table更改为read.table.

The options for the xclip command need to be changed to output and the function write.table needs to be changed into read.table.

例如:

read.table(pipe("xclip -selection clipboard -o",open="r"))

使用file()

您可以使用Anando提供的解决方案,但是在该解决方案的当前说明中,省略了一些详细信息.

Using file()

You can use the solution provided by Anando but in the present description of that solution some details were left out.

read.table("clipboard")命令有效地使用了.Internal(file(description, open, blocking, encoding, method, raw)命令,该命令分为多个选项

The command read.table("clipboard") is effectively using the command .Internal(file(description, open, blocking, encoding, method, raw) which splits up in several options

  • "X11_primary"(选定的文本)
  • "X11_secondary"(某些程序仅使用某些辅助副本字段)
  • "X11剪贴板"(已复制文本)
  • "X11_primary" (selected text)
  • "X11_secondary" (some auxiliary copy field only used by some programs)
  • "X11_clipboard" (copied text)

Ubuntu 16.04或更通用的Linu:x

我无法轻松地在源代码中对其进行跟踪,但是基于该行为,"clipboard"选项似乎默认为"X11_primary"(至少在Ubuntu 16.04中具有相同的行为).

I could not track it down easily in the source code but based on the behaviour it seems like the "clipboard" option defaults to "X11_primary" (at least it has the same behavior in Ubuntu 16.04).

如果使用read.delim("X11_clipboard")代替read.delim("clipboard"),则会得到复制的文本,而不是所选的文本.

If you use read.delim("X11_clipboard") in place of read.delim("clipboard") then you get the copied text instead of the selected text.

请注意,使用X11_clipboard时可能会出现错误,例如:

Note, that you may get an error when using X11_clipboard such as:

> read.table("X11_clipboard")
Error in file(file, "rt") : 
  X11 clipboard selection is not supported on this system

在这种情况下,您必须在系统(即操作系统,例如Ubuntu)上安装Xmu头文件.我遇到此错误,并使用

In that case you must install the Xmu header files on your system (that is the operating system, e.g. Ubuntu). I had this error in my case and resolved it by using

sudo apt-get install libxmu-dev
sudo apt-get install xorg-dev

我不知道两个解决了哪一个.但是之后,当我从源代码重新编译R-base时,read.table("X11_clipboard")起作用了. (我无法通过从Ubuntu存储库进行安装来使它正常工作)

I do not know which of the two solved it. But after this, when I recompiled the R-base from the source code, then the read.table("X11_clipboard") worked. (I could not get it working by installing from the Ubuntu repository)

这篇关于从Ubuntu Linux中的剪贴板复制R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆