On 30/04/2009 12:54 PM, Mike Miller wrote:
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?
I'd say the latter.
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's not quite correct. It does the same thing as that does, but it
doesn't do that: the < sign is interpreted by the shell to tell it to
redirect stdin.
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 )
Those are different on Windows at least. The first one will probably
fail, because very few commands other than shells know how to interpret
"<". You need the shell to interpret the "<" sign as a signal to
redirect input.
Check the man page for a description of this on other systems.
Duncan Murdoch
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
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.