[R-SIG-Mac] A problem with read.fwf when using clipboard

William Revelle lists at revelle.net
Tue Mar 9 18:58:29 CET 2010


Dear Mac R folks,

I am not sure if this is a Mac specific problem or not, but it 
certainly does not work on a Mac

Consider the data set

12345
23456
34567
45678
56789

copy this to the clipboard and then attempt to read it back in using 
the read.fwf  function

x <- read.fwf(file=pipe("pbpaste"),width=rep(1,5),header=FALSE)

The error is
Error in readLines(file, n = thisblock) : 'con' is not a connection
Error in UseMethod("close") :
   no applicable method for 'close' applied to an object of class "NULL"

But if we try

y <- read.table(file=pipe("pbpaste"),header=FALSE)

That works (but of course does not separate the variables.


The help file for read.fwf   says

file: the name of the file which the data are to be read from.

Alternatively, file can be a connection, which will be opened if 
necessary, and if so closed at the end of the function call.

read.table says the same thing about file (but is a bit more wordy).

##


The problem seems to be in the lines 22 to 29 of read.fwf

   if (is.character(file)) {
         file <- file(file, "r")
         on.exit(close(file), add = TRUE)
     }
     else if (!isOpen(file)) {
         file <- open(file, "r")
         on.exit(close(file), add = TRUE)
     }

If we replace those lines with lines 10-21 of read.table  then 
read.fwf works as expected and will read from pipe("pbpaste")

     if (is.character(file)) {
         file <- if (nzchar(fileEncoding))
             file(file, "rt", encoding = fileEncoding)
         else file(file, "rt")
         on.exit(close(file))
     }
     if (!inherits(file, "connection"))
         stop("'file' must be a character string or connection")
     if (!isOpen(file, "rt")) {
         open(file, "rt")
         on.exit(close(file))
     }


I discovered this while trying to extend my read.clipboard function 
to handle fixed format input.
The problem seems to happen on 2.10.1 as well as 2.11.0
R version 2.10.1 Patched (2009-12-16 r50763)
i386-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] psych_1.0-86

loaded via a namespace (and not attached):
[1] tools_2.10.1


R version 2.11.0 Under development (unstable) (2010-02-12 r51122)
i386-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] psych_1.0-86


Best wishes,

Bill

-- 
William Revelle		http://personality-project.org/revelle.html
Professor			http://personality-project.org
Department of Psychology             http://www.wcas.northwestern.edu/psych/
Northwestern University	http://www.northwestern.edu/
Use R for psychology                       http://personality-project.org/r
It is 6 minutes to midnight	http://www.thebulletin.org



More information about the R-SIG-Mac mailing list