[R-SIG-Mac] reading from clipboard error

Mark Cowley m.cowley at garvan.org.au
Tue Mar 20 02:48:01 CET 2012


Alan Kelly <AKELLY <at> tcd.ie> writes:

> 
> Hi Mark, I confirm your findings with readLines().
> I expect this behaviour is due to the explicit intention of readLine() to:
"Read some or all text lines from a
> connection" so the result is a character string.
> 
> To import data via the clipboad from a text editor or from and Excel file I
use read.delim(pipe("pbpaste"))
> rather than readLines() as I always intend that the copied material would
result in a data frame and not a
> character string.
> I tried your example using read.delim() but added  "header=F" as your example
did not have a variable name
> (note R adds V1, V2, etc) - see below.  Note that I do get a warning message
but the result still contains all 4
> letters whether I include the trailing end or line character or not.
> Why not use read.delim() instead or readLines()? And if your Excel or text
file has variables names (as is
> normal) just drop "header=F".
> Best wishes,
> Alan
> 
> # without trailing newline
> t1=read.delim(pipe("pbpaste"), header=F)
> Warning message:
> In read.table(file = file, header = header, sep = sep, quote = quote,  :
>   incomplete final line found by readTableHeader on 'pbpaste'
> # with trailing newline
> t1
>   V1
> 1  A
> 2  B
> 3  C
> 4  D
> 
> # with trailing newline
> t2=read.delim(pipe("pbpaste"), header=F)
> t2
>   V1
> 1  A
> 2  B
> 3  C
> 4  D

Thanks for all the replies, and thanks Alan for finding the workaround!

For silent operation with.without the final EOL:
suppressWarnings(read.delim(pipe("pbpaste"), header=FALSE))

+ a bit of R-fu to convert 1D data.frame's into vector's when appropriate

cheers,
Mark



More information about the R-SIG-Mac mailing list