[R] Read data.frame from clipboard
Thaler, Thorn, LAUSANNE, Applied Mathematics
Thorn.Thaler at rdls.nestle.com
Tue Mar 8 11:45:22 CET 2011
Helas, it is Windows (Vista). I thought Excel was already a hint, but I
reckon that Excel can run on Mac Os as well ;)
However, read.delim("clipboard") does the trick. Thanks a lot. I was not
successful with read.DIF("clipboard") though. How to use that?
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: mardi 8 mars 2011 10:43
> To: Thaler,Thorn,LAUSANNE,Applied Mathematics
> Cc: r-help at r-project.org
> Subject: Re: [R] Read data.frame from clipboard
>
> You haven't told us your OS. But assuming Windows, why not use
>
> read.delim("clipboard")
>
> or
>
> read.DIF("clipboard")
>
> ?
>
>
> On Tue, 8 Mar 2011, Thaler, Thorn, LAUSANNE, Applied Mathematics
wrote:
>
> > 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]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list