1

I am trying to run a series of input files (located in my C:/GenSoftware/Colony/datFiles/ directory) through an executable Colony2.exe that is located in my C:/GenSoftware/Colony/ directory. I attempt to rename file 1, copy it to the same directory as the executable, run Colony2.exe using the run.colony function (pasted at the bottom) in the package rcolony, delete the file, and proceed to file 2.

However, the code attempts to continue before the executable is finished. How can I get my loop to wait until the Colony2.exe is finished before it proceeds to the next line of code and then re-run the loop. run.colony invokes the system command (pasted at bottom).

Here is my code thus far...

rm(list=ls())

setwd("C:/GenSoftware/Colony/")
getwd()
datFiles <- list.files("datFiles")
library(rcolony)



d <- 0
for (d in 1:length(datFiles))
{
  d <- d+1
  setwd("C:/GenSoftware/Colony/datFiles/")
  file.rename(datFiles[d],"Colony2.DAT")
  file.copy(from = "C:/GenSoftware/Colony/datFiles/Colony2.DAT",to = "C:/GenSoftware/Colony/")
  datPath <- "C:/GenSoftware/Colony/Colony2.DAT"
  setwd("C:/GenSoftware/Colony/")
  run.colony(colonyexecpath = "Colony2.exe", datPath, wait = TRUE, monitor = FALSE)
  unlink(x = "C:/GenSoftware/Colony/Colony2.DAT", recursive = FALSE, force = TRUE)
  setwd("C:/GenSoftware/Colony/datFiles/")
  file.rename("Colony2.DAT",datFiles[d])
}
######## END OF MY CODE, START OF run.colony CODE
run.colony
function (colonyexecpath = "prompt", datfilepath = "prompt", 
    wait = FALSE, monitor = TRUE) 
{
    if (colonyexecpath == "prompt") {
        cat("Please click to select your Colony2 executable (probably called Colony2.exe or Colony2).\n\n")
        flush.console()
        colonyexecpath <- file.choose()
    }
    if (datfilepath == "prompt") {
        cat("Please click to select your DAT file.\n\n")
        flush.console()
        datfilepath <- file.choose()
    }
    datadir <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\1", datfilepath)
    filename <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\2", datfilepath)
    colonyexec <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)", 
        "\\2", colonyexecpath)
    current.wd <- getwd()
    x <- readLines(paste(datadir, filename, sep = ""), n = 2)
    outputfilename <- substring(x[2], 1, 20)
    outputfilename <- sub("^[\t\n\f\r ]*", "", outputfilename)
    outputfilename <- sub("[\t\n\f\r ]*$", "", outputfilename)
    outputfilename
    if (file.exists(paste(datadir, outputfilename, ".MidResult", 
        sep = ""))) {
        stop("\nThere are output files already in the directory. \nColony has already run. \nTry deleting (or moving) these files and starting again.\n")
    }
    setwd(datadir)
    if (monitor == TRUE & wait == TRUE) {
        stop("If you want to monitor the output, you must set wait as FALSE. Otherwise you cannot run other functions in the same R console.")
    }
    cat("Be aware: this may take several minutes, hours, or even weeks to run, depending on the settings used.\n")
    platform <- .Platform
    if (platform$OS.type == "unix") {
        if (file.exists("Colony2") == FALSE) {
            system(paste("cp", colonyexecpath, datadir, sep = " "))
        }
        if (filename != "Colony2.DAT") {
            system(paste("mv", paste(datadir, filename, sep = ""), 
                paste(datadir, "Colony2.DAT", sep = ""), sep = " "))
        }
        if (filename != "Colony2.DAT") {
            system(paste("cp", paste(datadir, "Colony2.DAT", 
                sep = ""), paste(datadir, filename, sep = ""), 
                sep = " "))
        }
        cat("#! /bin/sh\necho Running Colony2\nexport G95_MEM_SEGMENTS=0\n./Colony2", 
            file = paste(datadir, "Colony2.sh", sep = ""), append = FALSE)
        if (monitor == TRUE) {
            system("sh Colony2.sh | tee temp.txt", wait = wait)
        }
        else {
            system("sh Colony2.sh", wait = wait)
        }
        system(paste("rm", colonyexec))
        if (file.exists("Colony2.sh")) {
            system(paste("rm Colony2.sh"))
        }
        else {
        }
        if (filename != "Colony2.DAT") {
            system("rm Colony2.DAT")
        }
    }
    else {
        if (platform$OS.type == "windows") {
            shell(paste("copy", colonyexecpath, datadir, sep = " "))
            if (filename != "Colony2.DAT") {
                shell(paste("rename", paste(datadir, filename, 
                  sep = ""), paste(datadir, "Colony2.DAT", sep = ""), 
                  sep = " "))
            }
            shell.exec("Colony2.exe")
            if (filename != "Colony2.DAT") {
                shell(paste("rename", paste(datadir, "Colony2.DAT", 
                  sep = ""), paste(datadir, filename, sep = ""), 
                  sep = " "))
            }
            shell("del Colony2.exe")
        }
        else {
            stop(paste("This function is not correctly configured to run on", 
                platform$OS.type, "systems."))
        }
    }
    setwd(current.wd)
}
mackerman
  • 11
  • 1
  • 2
  • I'm not sure your overall approach is a good one, but you could put in a tight loop near the end of your main loop. Presumably your "colony2.exe" produces some output file, so `while( !file.exists('outputfile')) {do nothing}` – Carl Witthoft Feb 04 '14 at 20:03
  • I made an error in my original post. I am trying this in windows...run.colony is invoking shell() to run Colony2.ext – mackerman Feb 04 '14 at 20:32
  • @CarlWitthoft can you please elaborate on your {do nothing}? I like your suggestion of looking for the output file (or identifying the process in a tasklist), but how do you tell R to {do nothing} or wait? – mackerman Feb 06 '14 at 23:48
  • Well, if your OS has a 'sleep' command, then `system('sleep 1')` is a good "do nothing" . Otherwise, `now<-as.numeric(Sys.time()); howlong<-1; delt<-0; while(delt < howlong) { delt<-as.numeric(Sys.time())-now }` – Carl Witthoft Feb 07 '14 at 01:06

0 Answers0