Run pyDarwin Model Search
run_pyDarwin.RdThis function executes a pyDarwin model search by calling the specified Python
interpreter and the darwin.run_search module.
Usage
run_pyDarwin(
InterpreterPath,
Flags = c("-u", "-m"),
DirectoryPath = ".",
TemplatePath = "template.txt",
TokensPath = "tokens.json",
OptionsPath = "options.json",
Wait = TRUE
)Arguments
- InterpreterPath
Character string. The full path to the Python interpreter executable (e.g.,
python.exeorpython).- Flags
Character vector. Optional flags passed directly to the Python interpreter. Defaults to
c("-u", "-m").-uforces unbuffered binary stdout and stderr streams.-mruns a library module as a script and is essential for callingdarwin.run_search.- DirectoryPath
Character string. Optional path to the directory containing the
template.txt,tokens.json, andoptions.jsonfiles. If provided, this path is used to locate these files, overriding any directory information in theTemplatePath,TokensPath, andOptionsPatharguments (a warning will be issued). Defaults to the current R working directory.- TemplatePath
Character string. Path to the pyDarwin template file (typically
template.txt). IfDirectoryPathis specified, only the basename ofTemplatePathis used, combined withDirectoryPath.- TokensPath
Character string. Path to the pyDarwin tokens JSON file (typically
tokens.json). IfDirectoryPathis specified, only the basename ofTokensPathis used, combined withDirectoryPath.- OptionsPath
Character string. Path to the pyDarwin options JSON file (typically
options.json). This file defines run settings likeworking_dir,output_dir, etc. IfDirectoryPathis specified, only the basename ofOptionsPathis used, combined withDirectoryPath.- Wait
Logical. If
TRUE(default), R waits for the pyDarwin process to complete before proceeding. IfFALSE, R launches the pyDarwin process in the background and returns immediately. See the 'Background Execution' section for important details when usingWait = FALSE.
Value
If
Wait = TRUE: A list containing the search results read from theoutput_dir(specified inoptions.json). This typically includes:results: A data frame (results.csv).FinalResultFile: Character vector containing lines from the final model's result file (e.g.,.lst,.txt).FinalControlFile: Character vector containing lines from the final model's control file (e.g.,.mod,.mmdl).
If result files are not found, warnings are issued. If no results are found but
messages.txtexists, its content might be returned with a warning. If the Python call fails (non-zero exit code), the function stops with an error.If
Wait = FALSE: A character string giving the full path to the main pyDarwin log file (messages.txt) within theworking_dir.
Background Execution (Wait = FALSE)
When Wait is set to FALSE, the pyDarwin process is launched in the
background, and the R function returns immediately. This allows R to continue
processing while pyDarwin runs.
Output Redirection: Standard output (stdout) and standard error (stderr) from the Python process are redirected to files named
stdout.logandstderr.logrespectively, within theworking_dir. These files are crucial for diagnosing issues if the background process fails or behaves unexpectedly.Primary Log: The main pyDarwin log file (
messages.txt, located in theworking_dir) remains the primary source for detailed run information, but thestdout.logandstderr.logcapture console output and errors directly.
Examples
if (FALSE) { # \dontrun{
# Example: Running pyDarwin and waiting for results
# Assuming python is in the PATH and input files are in 'my_project'
results <- run_pyDarwin(
InterpreterPath = "python",
DirectoryPath = "my_project",
Wait = TRUE
)
print(results$results)
# Example: Launching pyDarwin in the background
log_file_path <- run_pyDarwin(
InterpreterPath = "python",
DirectoryPath = "my_project",
Wait = FALSE
)
} # }