Hi everybody,

 

I find myself quite often in the situation that I want to copy data from
Excel to R on the fly. If the source consists only of a single column, I
usually do something like

 

<copy column in Excel>

x <- as.numeric(readClipboard())

 

If I have a matrix, I usually export this matrix to a csv file first.
This approach works fine. However, sometimes I want to do some quick
checks and for these cases I don't like the file approach, as I do not
want to clutter up my working directory with temporary  files.

 

If you copy a matrix to the clipboard, you get a text file, separated by
tabs (at least in my locale here). So I wrote this wrapper in order to
alleviate copying btw Excel and R. Since I want to rely on the nifty R
ability to transform text columns to factors while leaving numerical
columns as they are, I first of all write the data to a file connection,
from where I read using read.table.

 

readClipboardDf <- function(token = "\t", ...) {

  text <- readClipboard()

  mat <- t(as.matrix(do.call(rbind, strsplit(text, token))))

  fh <- file()

  write(mat, fh, nrow(mat))

  mat <- read.table(fh, ...)

  close(fh)

  mat

}

 

However, this approach uses a file connection as well, so it does not
really change things (besides that it does things in one single step),
so any comments appreciated of how I could do this Excel to R thing
quickly preferably without any file transactions.

 

Thanks for your help.

 

BR Thorn


	[[alternative HTML version deleted]]

