Re: [R] Easy cut & paste from Excel to R?

From: Don MacQueen <macq_at_llnl.gov>
Date: Sat 19 Feb 2005 - 04:03:47 EST

I tried Ken's suggestion

    read.table(pipe("pbpaste"),header=TRUE) on my Mac OS X system and it worked *without* generating any warning message.

If my experience represents the norm, and Ken's is the exception, it is so simple that no further contribution to R is needed, I would say. Thank you, Ken.

The method can also be to go the other way, using pbcopy instead of pbpaste. Emulating an example found in R's help under ?'pipe':

> test <- data.frame(a=1:3,b=letters[1:3])
> zz <- pipe('pbcopy','w')
> write.table(test,file=zz,sep='\t',row.names=FALSE)
> close(zz)

Then in Excel (or any other Mac-native application) use the Paste command.

In the past I would get the warning message that Ken reports when I used read.delim() on tab-delimited files created using Excel's "Save as tab delimited" option. Excel does not put a newline at the
end of the last line, and as a result R would generate that error message. Excel still does not.
However, R now reads such files correctly, without generating the warning message.

pbpaste and pbcopy are included in the OS as distributed by Apple

[163]% which pbpaste
/usr/bin/pbpaste

(though perhaps only if one of the optional developer-related packages has been installed)
so these methods should be available to all Mac users of R, without any extra work on their part
(other than learning about them, that is).

By the way, there doesn't appear to be open connection left behind:

> bah <- read.table(pipe('pbpaste'),header=TRUE)
> dim(bah)

[1] 21 5
> showConnections(all=TRUE)

   description class      mode text   isopen   can read can write
0 "stdin"     "terminal" "r"  "text" "opened" "yes"    "no"    
1 "stdout"    "terminal" "w"  "text" "opened" "no"     "yes"   
2 "stderr"    "terminal" "w"  "text" "opened" "no"     "yes"   



> version
_ platform powerpc-apple-darwin6.8.5 arch powerpc os darwin6.8.5 system powerpc, darwin6.8.5 status major 2 minor 0.1 year 2004 month 11 day 15 language R

Mac OS 10.3.8

Excel 2004, version 11.1 (040909)

-Don

At 11:09 PM +0100 2/17/05, Ken Knoblauch wrote:
>Here is something quick & dirty for Mac that may be serviceable in
>some cases, while awaiting someone with greater understanding of
>programming connections than I have currently.
>
>With the following copied to the clipboard from Excell:
>H T Q F
>1 2 3.3 a
>3 5 10.2 b
>5 9 11 A
>
>I tried in R:
>
>read.table(pipe("pbpaste"),header=TRUE)
> H T Q F
>1 1 2 3.3 a
>2 3 5 10.2 b
>3 5 9 11.0 A
>Warning message:
>incomplete final line found by readTableHeader on `pbpaste'
>> str(read.table(pipe("pbpaste"),header=TRUE))
>`data.frame': 3 obs. of 4 variables:
> $ H: int 1 3 5
> $ T: int 2 5 9
> $ Q: num 3.3 10.2 11
> $ F: Factor w/ 3 levels "A","a","b": 2 3 1
>Warning message:
>incomplete final line found by readTableHeader on `pbpaste'
>
>I haven't been able to track down readTableHeader yet. The warning
>occurs even without headers in the data.
>
>
>Quoting Prof Brian Ripley <ripley@stats.ox.ac.uk>:
>
>> On Thu, 17 Feb 2005, Uwe Ligges wrote:
>>
>> > Ken Knoblauch wrote:
>> >
>> >> I tried the interesting suggestion below, discussed in several postings
>> >> yesterday on the help-list, on my Mac (0S 10.3.7) but could not get it
>> to
>> >> work, as shown in the tests indicated below.
>> >>
>> >>
>> >>>> read.table(file("clipboard"), sep="\t", dec=",")
>> >
>> > Connections to the clipboard are only available on Windows.
>>
>> Ken is of course welcome to contribute them for MacOS X (or indeed for
>> X11).
>> People do take for granted the work the developers do to provide such
>> things ....
>>
>> --
>> Brian D. Ripley, ripley@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
>>
>
>
>
>____________________
>Ken Knoblauch
>Inserm U 371
>Cerveau et Vision
>18 avenue du Doyen Lepine
>69675 Bron cedex
>France
>tel: +33 (0)4 72 91 34 77
>fax: +33 (0)4 72 91 34 61
>portable: 06 84 10 64 10
>
>______________________________________________
>R-help@stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Feb 19 03:26:36 2005

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 01:31:32 EST