Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R data.table fread from clipboard

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

like image 374
jangorecki Avatar asked Nov 05 '13 09:11

jangorecki


2 Answers

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.

like image 133
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 15 '22 04:10

A5C1D2H2I1M1N2O1R2T1


fread is the absolute coolest R function because you can directly use system commands like clipboard or pbpaste.

dt_clip <- function(...){
  # returns which operating system being used
  os_type <- .Platform$OS.type
  
  # allows the function to be agnostic to the OS. 
  switch(os_type,
         unix = data.table::fread(cmd = 'pbpaste', ...),
         windows = data.table::fread(cmd = 'clipboard', ...)
         )
}

This makes it substantially faster.

# using a 1e6x2 data table from excel
> system.time( freadClip())
   user  system elapsed 
  2.156   0.151   2.509 

> system.time( dt_clip())
   user  system elapsed 
  0.141   0.060   0.221 
like image 39
Dewey Brooke Avatar answered Oct 15 '22 02:10

Dewey Brooke