[R-SIG-Mac] reading from clipboard error

Ken knoblauch ken.knoblauch at inserm.fr
Mon Mar 19 11:03:57 CET 2012


Mark Cowley <m.cowley <at> garvan.org.au> writes:
> You can read from the OSX clipboard using 
readLines(pipe("pbpaste"))
> However, If you don't have a trailing newline, t
he last line is silently skipped. Note
> readLines(warn=TRUE)'s doesn't produce a 
waning, thus I think it doesn't even see the final line.
> Simple example:
> Select the letters with or without a trailing 
new line, copy to clipboard, then
> A
> B
> C
> D
> # without trailing newline
> readLines(pipe("pbpaste"))
> [1] "A" "B" "C"
> # with trailing newline
> readLines(pipe("pbpaste"))
> [1] "A" "B" "C" "D"
> Note that flush()ing the connection does not quite fix the problem
> In the more verbose form:
>     OUT <- pipe("pbpaste")
>     readLines(OUT)
> [1] "A" "B" "C"
>     isIncomplete(OUT)
> [1] TRUE
>     flush(OUT)
>     readLines(OUT)
> [1] "DA" "B"  "C" 
> The last element gets pre-pended to the first element, 
so a hack could fix this.
> 
> Is this a limitation of pipe, pbpaste, or just a bug?

It looks like it is documented depending on the blocking 
behavior of your connection.

>From ?readLines

"If the final line is incomplete (no final EOL marker) the behaviour 
depends on whether the connection is blocking or not. 
For a non-blocking text-mode connection the incomplete
 line is pushed back, silently. For all other connections the 
line will be accepted, with a warning.


Whatever mode the connection is opened in, any of LF, CRLF 
or CR will be accepted as the EOL marker for a line."

> 
> This is an issue, because i often copy from Excel to 'paste' into R, 
and a selection in Excel /never/ adds the
> trailing new line, even if you select blank cell(s) below 
your selection.
> 
> cheers,
> Mark
> -----------------------------------------------------
> Mark Cowley, PhD
> 
> Pancreatic Cancer Program | Peter Wills Bioinformatics Centre
> Garvan Institute of Medical Research, Sydney, Australia
> -----------------------------------------------------



More information about the R-SIG-Mac mailing list