I have used R in the past to do very basic calls to the commmand line. The example can be found here.
This time around, I am looking to mimic this code which runs successfully from the command line in Windows:
> cd C:\Documents and Settings\BTIBERT\My Documents\My Dropbox\Eclipse\Projects\R\MLB\retrosheet\rawdata
> bgame -y 2010 2010bos.eva >2010bos.txt
This is the code I am trying to run inside of R. I have already set the working directory inside of R.
dir <- paste("cd", getwd(), sep=" ")
system(dir)
system("bgame -y 2010 2010bos.eva >2010bos.txt")
I am sure this is user error, but what am I doing wrong? It appears to work initially, but returns the following error. I very well could be doing something wrong, but I believe I am using the same commands.
Expanded game descriptor, version 109(185) of 05/08/2008.
Type 'bgame -h' for help.
Copyright (c) 2001 by DiamondWare.
[Processing file 2010bos.eva.]
>2010bos.txt: can't open.
Warning message:
running command 'bgame -y 2010 2010bos.eva >2010bos.txt' had status 2
Any help you can provide will be appreciated.
Has no-one else found that
system("dir", intern = T)
for example doesn't work, but that you needsystem("cmd.exe /c dir", intern = T)
? Only the latter works for me. I found this at the discussion site here (William Dunlap's post, about a third of the way down).Also, it doesn't work with the "cd" command, but you can use the
setwd()
function within R and then the command will be executed within that directory.I created the following functions for convenience, for executing programmes and running commands:
Does it break your code when you get error 1 or does execution continue?
Whenever executing system commands through another language it is useful to print the system call before you call it to see exactly what is happening, pull up the shell you are intending to use and check for the same error. As the command is executed correctly this could be a hickup in bgame or R.
If you look at http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html you can see the variable flag passed to the system call."flag the switch to run a command under the shell. If the shell is bash or tcsh the default is changed to "-c"."
Also "the shell to be used can be changed by setting the configure variable R_SHELL to a suitable value (a full path to a shell, e.g. /usr/local/bin/bash)."
You need to issue all commands in one
system()
call:You should already be in your working directory, so I'm not sure the
cd getwd()
is necessary. And you may need quotes around your path because it contains spaces. The error may be resolved by putting spaces around>
.If I were in your shoes, I would try this:
UPDATE:
And you should probably heed this advice in the "Differences between Unix and Windows" section of
?system
that says you should useshell
: