R data.table fread from clipboard

2019-06-20 07:44发布

问题:

I want to create an interface excel-R::data.table. I would like to ask how it is possible to use fread function with clipboard. Following code is working well but I would prefer to use fread instead of read.table (to reproduce copy some table in excel file and run above command in R):

data.table(read.table("clipboard",sep="\t",header=TRUE))

I tried declaring connection to clipboard but so far cannot get it work. Also as stated in fread function documentation it is going to change and some things might be deprecated so it could be nice to have a solution which is not going to be deprecated in close future. Is there any limits for clipboard/fread? like 65000 rows, or some other memory limit?

I also would like to suggest to extend data.table::fread function to accept 'clipboard' connect by default as it is works currently with read.table.

Thanks

回答1:

fread doesn't seem to have this feature, but for limited use, you can easily write your own wrapper for it. Something along the lines of the following might help you get started:

freadClip <- function(...) {
  X <- tempfile()
  writeLines(readLines("clipboard"), X)
  fread(X, ...)
}

Usage would just be copying the cells from your Excel sheet, switching back to R, and typing freadClip().

Note: I'm assuming that this is more of a convenience function than anything--mostly for quickly getting a small dataset from Excel into R. I don't know if I would personally use "clipboard" for a file in the range of 65k rows as you describe in your question.