[R] calling external .EXE file in R macOSX

Alex Anderson complicado79 at yahoo.com.au
Mon Mar 22 02:00:24 CET 2010

Hi All,
I am currently working on an analysis which requires a call to an 
external FORTRAN routine contained within a file called MCDS.EXE.  This 
file is usually called from within a WINDOWS program called DISTANCE.  I 
have some R script from the developers of the original software which 
apparently makes a call to this  .EXE file which i need to re-engineer 
slightly,  but i am having difficulty interpreting how the script makes 
this call and passes to the EXE file the relevant input command file and 
data file for processing.  I am also unsure if making such a call will 
work within the OSX environment for a (windows?) executable file.  Below 
is an example from the script which is supposed to make the call. Any 
suggestions would be much appreciated.

cds <- function (key, adj, L, w, A=NA, xi, zi, 
file.base,ext.files=F,bootstrap=F) {
#Purpose: Driver function to run the mcds.exe engine from R
#Updated by Tiago on 19/12/2004 so that it can take an external data and 
command input files
#Usefull if you want to do a non-standard bootstrap analysis of some 
data analised in distance
#and for wich distance is not capable of producing variance estimates
#This way, you can call this function inside a bootstrap procedure, as 
long as you update the
#data file at each resample
#  key    - vector string containing key functions
#  adj    - vector string containing adjustment terms
#  L - total survey effort
#  A - survey area
#  xi - vector of perpendicular distances
#  zi - vector of cluster sizes
#  file.base - if specified and engine is cds, then the cds input and
#          output files are written into the current directory, with the
#          cds.file.base as a prefix and '.txt' as a suffix.  E.g., setting
#          cds.file.base to 'cds' produces 'cds.cmd.txt', 'cds.data.txt',
#          'cds.log.txt','cds.stat.txt','cds.plot.txt' and 'cds.boot.txt'
#          If not specified, these files are created in a temp location and
#          are deleted at the end.
#  ext.files - If true, then the funtion uses the external files 
'file.base'+'.cms.'+txt' as command file
#          and 'file.base'+'.data.'+.txt' as data file. Typicaly these 
files would be the result of
#          runing Distance in debug mode, and should be placed in the 
working directory for R.
#          By default ext.files is false, so the function looks for the 
files produced  by functions
#          'create.data.file' and  'create.command.file'
#          You need to change the input comand file in order for the 
mcds engine to produce the files
#          that the function 'read.stats.file' expects, and that means 
that inside the command file you should define
#          file names with prefix = file.base
#  bootstrap - If true, procedure is being called inside a bootstrap 
routine, and intermediate files
#           are deleted at each loop step, except the command file
#Outputs: list, containing
# Nhat.grp, Nhat.ind, mu, nL, Es, LnL, AIC, status
# Note - status is an integer:
#  1=OK, 2=warnings, 3=errors, 4=file errors, 5=some other problem 
(e.g., program crash)

run.cds<-function(cmd.file.name) {
#Purpose: runs the MCDS.exe engine and waits for it to finish
#  *Note* that mcds.exe needs to be in the working directory, or in the
#  PATH windows environment
#  variable for this to work, as it makes no attempt to find the
#  location of the file
#  cmd.file.name - name of the command file to run
#  A status integer - 1=OK, 2=warnings, 3=errors, 4=file errors, 5=some 
other problem (e.g., program crash)

More information about the R-help mailing list