How do I copy and paste data into R from the clipb

2019-01-08 08:45发布

The title says it all: I have my data open in another application (e.g., a spreadsheet, like Excel, or a text editor). If I copy that data to my operating system clipboard, how can I read it into R as a data.frame?

标签: r io clipboard
8条回答
太酷不给撩
2楼-- · 2019-01-08 09:24

The name and exact connection used for the 'clipboard' varies depending on the OS.

for Windows:

x <- read.delim("clipboard")

for Mac OS:

x <- read.delim(pipe(“pbpaste”))

This works because read.delim, like many functions, will accept a range of connection types beyond just a file. For Macs we're actually using a pipe. help(connections) is pretty informative.

The psych package has a function read.clipboard() that makes this a little easier by testing for your OS.

As noted by others here, you can also write to the clipboard. There is normally a 32 K limit, which can be raised by using adding a hyphen and number after clipboard as in, for example, passing up to 256 K worth of data from object df with:

write.table(df, "clipboard-256")
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-08 09:26

I needed to copy a composite into the Windows , while read.table() outputted a character vector with quotation marks around my URL. Instead, I used writeClipboard(URL,format=1) from package , and it did the trick.

查看更多
登录 后发表回答