[R] command prompt shell suppressing stdout?

Michael Neely mneely at usc.edu
Tue Mar 22 19:26:30 CET 2011


After four years of using R, I finally have run into a problem to which I can't find a solution in the guides or forums and thus I am making my first post.

Our lab has Fortran code for population modeling.  I have been using R as a wrapper to process the raw data, generate a batch script that compiles the Fortran code, executes the code in a shell, and then extracts the output of the program for further analysis and plotting in R.

Because the Fortran modeling program can sometimes take hours or days to complete, depending on the complexity of the data and model, I want it to run outside of R to avoid tying it up.  In Mac/Unix, I have been able to accomplish with a function that creates the batch script and then executes it with this command:

	system(paste("open -a Terminal.app ",shQuote(paste(getwd(),"/npscript",sep="")),sep=""))

This opens an independent instance of Terminal and executes the batch script. The user can provide input to the Fortran executables created/called by the batch script as required, and see the output in real time as the progress of the modeling is continually updated.

In Windows, I have been unable to accomplish this.  I can generate the batch script code of course, and it runs perfectly in a command prompt window if I manually double-click it to execute.  However, if I try to execute it from R, I appear to have no stdin capability (i.e. no user input possible) to the compiled Fortran executable and no stdout from the executable, i.e. no way to tell if the Fortran code is hung up, or simply churning away without updating the command prompt window.  It does complete successfully with the expected output files, but for a long run, it is imperative to view progress normally reported by the executable.

Here are the first few lines of the batch script that I generate in R, simplified and with added line numbers for clarity:

1) C:\bin\prep.exe DOS < control  #run the prep program with command line arg to specify OS and input from control (also generated in R with user options to the function) to automate; no output visible in command prompt window; if control not specified, then executable waits for user keyboard input, resulting in a frozen blank window
2) gfortran  -o C:\bin\run.exe file1 file2 file 3 prepout...  #compile "engine" files to run model with output of prep program; output visible in command prompt window
3) C:\bin\run.exe  #execute engine to run the model; no output visible

I have tried the following without success:

system("npscript.bat", wait=F, invisible=F) and all permutations
system2("npscript.bat", wait=F, invisible=F) and all permutations
shell("npscript.bat")
shell("start npscript.bat")

Is there any way to replicate the Unix behavior in Windows and launch a shell that runs the batch script with the same I/O capability as if I had manually launched it?  Yes, I could have users manually launch the batch script, but it would be tidier if R could successfully complete the process.

Thanks very much.




More information about the R-help mailing list