[R] use of "input" in system()
Mike Miller
mbmiller+l at gmail.com
Thu Apr 30 18:54:58 CEST 2009
On Fri, 24 Apr 2009, Duncan Murdoch wrote:
> On 4/24/2009 10:29 AM, Mike Miller wrote:
>
>> First, it looks like there is bug in the documentation...
>>
>> According to the documentation for system():
>>
>> http://stat.ethz.ch/R-manual/R-patched/library/base/html/system.html
>>
>> input if a character vector is supplied, this is copied one string per
>> line to a temporary file, and the standard input of command is
>> redirected to the file
>>
>> This seems to mean that the standard input of command is redirected
>> *from* the file. From the file, to the command. Example:
>
> The redirection is done *to* the file handle. The fact that input is
> read from that handle is a different issue.
Thanks very much for the reply. After seeing your response, I'm sure the
document isn't wrong (not a bug) but it is a very terse explanation that I
think many people will find hard to follow, as I did.
First, a temporary file is created. Where is it? What is it called? If
the name/location don't matter, does it even matter that a temporary file
is created? Is this just info about the internal workings of R that the
user doesn't need to know?
Then "the standard input of command is redirected to the file". I think I
get this now. I think it means that this is being done behind the scenes:
command < file
Would it help users to tell them that, if that is correct? It would have
helped me. I think this is basically what it is doing:
# For input to the system command we'll need a command and a vector:
command <- "any command string"
v <- c("some", "vector")
# make a temp file:
filename <- tempfile( tmpdir=tempdir() )
write.table(v, file=filename, sep="\n", row.names=FALSE, col.names=FALSE, quote=FALSE)
# Then I think these two system commands do the same thing:
system( paste(command, "<", filename, sep=" ") )
system( command, input=v )
It would be nice if there were more documentation so that I could
understand this better. I wasn't understanding why system() hangs
sometimes as in the simple examples below, but I think I get it now: The
stdout goes only to the last command in a string of semi-colon-separated
commands. If that's right, it makes sense.
Mike
This works:
> system( "cat -", input="foo" )
foo
This hangs until I hit ctrl-c:
> system( "cat - ; echo bar", input="foo" )
This works:
> system( "echo bar; cat -", input="foo" )
bar
foo
This hangs until I hit ctrl-c:
> system( "cat - ; cat -", input="foo" )
Best,
Mike
More information about the R-help
mailing list