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条回答
Rolldiameter
2楼-- · 2019-01-08 09:17

If you want to read in tabular data from a spreadsheet, I have used the following code

read.table(file = "clipboard", sep = "\t", header=TRUE)
查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 09:18

Look at the documentation for ?file, section Clipboard:

Clipboard file can be used with description = "clipboard" in mode "r" only. This reads the X11 primary selection (see http://standards.freedesktop.org/clipboards-spec/clipboards-latest.txt), which can also be specified as "X11_primary" and the secondary selection as "X11_secondary". On most systems the clipboard selection (that used by ‘Copy’ from an ‘Edit’ menu) can be specified as "X11_clipboard". When a clipboard is opened for reading, the contents are immediately copied to internal storage in the connection. Unix users wishing to write to one of the X11 selections may be able to do so via xclip (http://sourceforge.net/projects/xclip/) or xsel (http://www.vergenet.net/~conrad/software/xsel/), for example by pipe("xclip -i", "w") for the primary selection. macOS users can use pipe("pbpaste") and pipe("pbcopy", "w") to read from and write to that system's clipboard.

so, eg with magrittr:

base::file(description='clipboard') %>% readLines
查看更多
Bombasti
4楼-- · 2019-01-08 09:19

There's an R package / RStudio plugin called datapasta that does this very neatly - see https://CRAN.R-project.org/package=datapasta. Image below is a demonstration of its simplicity

enter image description here

查看更多
唯我独甜
5楼-- · 2019-01-08 09:22

A method that I've tested and works on both Windows and MacOS is to use textConnection() with read.table().

First, paste your data into a variable as text:

density_water_str <-   "T_/K Density_g/mL D2O
273 0.999841 1.10469
274 0.999900 NA
275 0.999941 NA
276 0.999965 NA
277 0.999973 1.1057
278 0.999965 1.10562
279 0.999941 NA
280 0.999902 NA
281 0.999849 NA
282 0.999781 NA
281 0.999700 NA"

Then, read the text string using read.table()

density_water <- read.table(textConnection(
                                object = density_water_str), 
                            header = TRUE, 
                            sep = "", 
                            stringsAsFactors = FALSE)

Not tested on Linux or other Unix systems, but I believe it should work cross-platform.

查看更多
一夜七次
6楼-- · 2019-01-08 09:23

Assuming you have data in the Windows clipboard (for example, copied data from Excel), to put that data into a variable named copdat in R use:

copdat <- read.delim("clipboard")

If you want to copy data from an R variable named rdat into the Windows clipboard (for example, to copy into Excel) use:

write.table(rdat, "clipboard", sep="\t", row.names=FALSE, col.names=FALSE)
查看更多
做个烂人
7楼-- · 2019-01-08 09:23

Type in data = as.numeric(read.table(text = "125 140 200 200 190 ", sep = " ")) where your numbers go in between the text = " " quotation marks.

查看更多
登录 后发表回答