[R] Read data.frame from clipboard
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Mar 8 13:30:34 CET 2011
On 11-03-08 6:16 AM, Prof Brian Ripley wrote:
> On Tue, 8 Mar 2011, Thaler,Thorn,LAUSANNE,Applied Mathematics wrote:
>
>> Helas, it is Windows (Vista). I thought Excel was already a hint, but I
>> reckon that Excel can run on Mac Os as well ;)
>
> It can, and I was writing that message on Mac OS X, where "clipboard"
> does not work.
>
>> However, read.delim("clipboard") does the trick. Thanks a lot. I was not
>> successful with read.DIF("clipboard") though. How to use that?
>
> You need to send DIF to the clipboard. I'm not an Excel user
> (although I do have it on this Mac), so I don't know how to do that --
> but as the author of read.DIF mentions support for "clipboard", some
> programs presumably can.
In the old days Excel offered lots of different formats when you copy to
the clipboard, including DIF. read.DIF looks for that if you're reading
from the clipboard. Perhaps Excel has changed.
Duncan Murdoch
>
>>
>>> -----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