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




From: Mark Cowley <[email protected]<mailto:[email protected]>>
Date: 14 March 2012 00:25:21 GMT
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: [R-SIG-Mac] reading from clipboard error


Dear List,
You can read from the OSX clipboard using readLines(pipe("pbpaste"))

However, If you don't have a trailing newline, the 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?

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
-----------------------------------------------------


[[alternative HTML version deleted]]


        [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Mac mailing list
[email protected]
https://stat.ethz.ch/mailman/listinfo/r-sig-mac

Reply via email to