R and System calls R and System calls r r

R and System calls


You need to issue all commands in one system() call:

system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))

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:

system("bgame -y 2010 2010bos.eva > 2010bos.txt")

UPDATE:

And you should probably heed this advice in the "Differences between Unix and Windows" section of ?system that says you should use shell:

    • The most important difference is that on a Unix-alike      ‘system’ launches a shell which then runs ‘command’.  On      Windows the command is run directly - use ‘shell’ for an      interface which runs ‘command’ _via_ a shell (by default the      Windows shell ‘cmd.exe’, which has many differences from the      POSIX shell).      This means that it cannot be assumed that redirection or      piping will work in ‘system’ (redirection sometimes does, but      we have seen cases where it stopped working after a Windows      security patch), and ‘system2’ (or ‘shell’) must be used on      Windows.


Has no-one else found that system("dir", intern = T) for example doesn't work, but that you need system("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:

#the subject is an input file that a programme might requireexecute <- function(programme, subject.spec = "", intern = FALSE, wait = FALSE){  if(!identical(subject.spec, "")){subject.spec <- paste0(" ", subject.spec)} #put space before the subject if it exists  system(paste0("cmd.exe /c ", programme, subject.spec), intern = intern, wait = wait)}command <- function(command, intern = TRUE, wait = FALSE){  system(paste("cmd.exe /c", command), intern = T, wait = wait)}


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)."