Skip to content

pipe and binary i/o

3 messages · Huntsinger, Reid, Brian Ripley

#
I often want to use "pipe" with "readBin" to read (binary) data from the
standard output of a Perl or C program. For that I need "pipe" to work with
the "open='rb'" option. It never worked for me, and after a look at the
connections.c file, I see that in "pipe_open" the mode gets passed directly
to "popen" (I'm using Linux on an Intel machine). Popen doesn't know "rb"
and fails. I modified the pipe function in connection.c to pass only the
first character of the mode. This seems to work (fingers crossed). Then I
noticed gzfile has the same problem, but bzfile doesn't, and uses the same
solution as I did for pipe. Can I assume that this is a correct fix? Or were
there reasons that pipe and gzfile wouldn't allow "rb"?

Thanks,

Reid Huntsinger


-----Original Message-----
From: Thomas Lumley [mailto:tlumley at u.washington.edu]
Sent: Monday, August 05, 2002 12:17 PM
To: Jim Lemon
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] filename of source file
On Mon, 5 Aug 2002, Jim Lemon wrote:

            
That will work only when run at the top level.

If it isn't sufficient to know the directory

 	source("/path/name/filename.r",chdir=TRUE)

in which case magic.command() is getwd(), there isn't a nice way to do it.

Two not-so-nice ways

1) modify source() so it does something like

    assign(".Last.source.file",file,envir=.GlobalEnv)

   giving
    magic.command<-function() get(".Last.source.file",envir=.GlobalEnv)


2) Get the file name from the environment inside source

     magic.command<-function(){
	n<-match("source",rev(as.character(sapply(sys.calls(),"[[",1))))
	get("file", parent.frame(n-1)
	}



The problem with the first is that it writes in the global workspace and
that it fails if a source()d file source()s another file before you use
magic.command().

The problem with the second is that it will break if the source() function
is
renamed or if the name of the variable "file" changes.


You also need to be aware that `file' need not be a filename. It could be
a URL [eg source("http://www.bioconductor.org/getBioC.R") ] or a
connection (either a file connection or some other sort).


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message.  If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.

==============================================================================

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
pipe predates readBin, and no one has seen a reason to do this before you.

It's an adequate fix.
On Mon, 5 Aug 2002, Huntsinger, Reid wrote:

            
[irrelevant message deleted]
#
On Mon, 5 Aug 2002 ripley at stats.ox.ac.uk wrote:

            
However, gzfile should work, and bzfile *always* opens with "rb".  Here's
an example on RH7.2:
[1] 1 2 3 4
[1]  5  6  7  8  9 10
Error in pipe("gzcat testbin.gz", "rb") : unable to open connection
In addition: Warning message:
cannot open cmd `gzcat testbin.gz'

so pipe has problems, as on Linux popen does not respect "rb" (although on
other platforms it seems to).
On Linux, that is: Windows needs the "b".  I've put a full fix in R-devel.