Re: [R] Read data.frame from clipboard

From: Thaler, Thorn, LAUSANNE, Applied Mathematics <Thorn.Thaler_at_rdls.nestle.com>
Date: Tue, 08 Mar 2011 11:45:22 +0100

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



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. Received on Tue 08 Mar 2011 - 10:50:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Mar 2011 - 11:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive